article cover
Mohssine SERRAJI

Mohssine SERRAJI

Data scientist Expert & Co-founder

How to Deal with Outliers in Python: A Complete Guide

August-19-2024

Outliers can significantly impact the results of your data analysis and subsequent predictive models. Identifying and managing outliers is thus a crucial step in the data preprocessing phase. This guide will help you understand and handle outliers in Python using common libraries such as Pandas and Scikit-learn.

What is an Outlier?

An outlier is a data point that differs significantly from other observations. It could be due to variability in the measurement or experimental errors. In statistical terms, an outlier might lie outside 1.5 times the interquartile range above the third quartile and below the first quartile.

Identifying Outliers

1. Using Graphical Methods:

  • Boxplot:
import seaborn as sns
sns.boxplot(x=data['Column'])
  • Scatter Plot:
import matplotlib.pyplot as plt
plt.scatter(range(data.shape[0]), data['Column'])
plt.title('Scatter plot of Data')
plt.show()


2. Using Z-Score:

A Z-score indicates how many standard deviations an element is from the mean. A Z-score beyond 3 or -3 is typically considered an outlier.

from scipy import stats
z_scores = stats.zscore(data['Column'])
outliers = data[(z_scores < -3) | (z_scores > 3)]


3. Using IQR (Interquartile Range):

Q1 = data['Column'].quantile(0.25)
Q3 = data['Column'].quantile(0.75)
IQR = Q3 - Q1
outliers = data[(data['Column'] < (Q1 - 1.5 * IQR)) | (data['Column'] > (Q3 + 1.5 * IQR))]


Handling Outliers

1. Removing Outliers:

This is a straightforward method but can lead to loss of valuable information.

filtered_data = data[(z_scores > -3) & (z_scores < 3)]

2. Capping and Flooring:

Here, you cap values above a certain threshold.

upper_limit = Q3 + 1.5 * IQR
lower_limit = Q1 - 1.5 * IQR
data['Column'] = np.where(data['Column'] > upper_limit, upper_limit, np.where(data['Column'] < lower_limit, lower_limit, data['Column']))

3. Transforming the Data:

Sometimes, a transformation can reduce the effect of outliers.

data['Log_Column'] = np.log(data['Column'])

4. Using Robust Scaling:

Robust scalers and models that are less sensitive to outliers can also be used.

from sklearn.preprocessing import RobustScaler
scaler = RobustScaler()
data['Scaled_Column'] = scaler.fit_transform(data[['Column']])

Conclusion

Handling outliers appropriately depends significantly on the context and the specific requirements of your data analysis or predictive modeling tasks. It's essential to understand the nature of your data and the reasons why outliers might exist before deciding how to manage them.

Master AI Tools in Just 5 Minutes a Day

Join 1000+ Readers and Learn How to Leverage AI to Boost Your Productivity and Accelerate Your Career

Newsletter language