![]() |
VOOZH | about |
Outliers are data points that are very different from most other values in a dataset. They can occur due to measurement errors, unusual events or natural variation in the data. If not handled properly, they can affect analysis results and reduce machine learning model performance.
There are several ways to detect and handle outliers in Python. We can use visualization methods or statistical techniques depending on the type of data. In this section, we will use Pandas and Matplotlib on the Diabetes dataset, which is available in the Scikit-learn library.
A box plot helps visualize how data is distributed using quartiles. Any points outside the whiskers of the box plot are considered outliers. It is a simple way to see where most data values lie and identify unusual values.
Output:
In a box plot, outliers appear as points outside the whiskers. These values are much higher or lower than most of the data. For example, BMI values above 0.12 may be considered outliers
Removing Outliers
To remove outliers, we can define a threshold value and filter the data.
Output:
Scatter plots help show the relationship between two variables. They are useful when working with paired numerical data. In a scatter plot, outliers appear as points that are far away from the main group of data points.
Output:
From the graph, most data points are grouped in the bottom left corner. However, a few points appear in the opposite area, the top right corner of the graph. These points can be considered outliers because they are far from the main cluster of data.
Removing Outliers
Output:
This removes rows where BMI > 0.12 and BP < 0.8 conditions derived from visual inspection.
The Z-score, also called the standard score, shows how far a data point is from the mean in terms of standard deviations. If the Z-score is greater than a chosen threshold , the value is considered an outlier.
Output:
An outlier threshold is usually set to 3.0. This is because about 99.7% of data points in a normal (Gaussian) distribution lie within standard deviations from the mean.
Removing Outliers: Trimming and Capping
After identifying outliers using the Z-score method, we can handle them in two common ways: trimming or capping.
Trimming Outliers
Output:
Original DataFrame Shape: (442, 10)
DataFrame Shape after Removing Outliers: (426, 10)
Capping Outliers
Output:
Original DataFrame Shape: (442, 10)
DataFrame Shape after Capping Outliers: (442, 10)
The IQR (Interquartile Range) method is a common and reliable technique for detecting outliers. It works well even when the data is skewed and identifies extreme values using quartiles. The IQR is calculated as the difference between the third quartile (Q3) and the first quartile (Q1):
Here, we calculate the IQR for the 'bmi' column in df_diabetics. First Q1 and Q3 are found, then IQR = Q3 − Q1, which shows the spread of the middle 50% of the data.
Output:
0.06520763046978838
To identify outliers, we define upper and lower limits using the IQR value. Any value outside this range is treated as an outlier.
Output:
Removing Outliers: Trimming and Capping
Trimming Outliers
Output:
Old Shape: (442, 10)
New Shape: (439, 10)
Capping Outliers
Output:
Shape after Capping: (439, 10)
Download full code from here.