Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 16

Outlier Handling Strategies

Recommended resources

Hello! Welcome back to our course.

In our last lesson, we explored feature scaling and saw firsthand how outliers can significantly distort the results, especially when using MinMaxScaler. This observation provides the perfect motivation for today's topic: what to do about these extreme values.

Today's lesson addresses the next step in our Data Preprocessing and Feature Engineering module. Your learning outcome is to apply common strategies for handling outliers identified during EDA (e.g., clipping, removal). We will explore the different types of outliers and learn the two primary methods for treating them: removing them entirely or adjusting their values to be less extreme.

1. To Treat or Not to Treat? A Question of Origin

Before we jump into Python code, it's crucial to understand that handling outliers is a subjective task that requires careful consideration. The right strategy depends entirely on the source of the outlier.

To build this foundational knowledge, please read the following article. It introduces a critical distinction that will guide all of our decisions.

Detecting and Treating Outliers In Python - Part 3

This article from Towards Data Science explains the different sources of outliers and the main options for handling them.

Read the sections titled 'Sources of outliers', 'Handling error Outliers', and 'Handling non-error outliers'. Focus on the difference between an 'error outlier' and a 'non-error outlier,' and the three main options for treatment: keep, delete, and recode.

As you've just read, we can categorize outliers into two main types:

  • Error Outliers: These are mistakes. They could be from data entry errors (e.g., a person's age entered as 200) or measurement failures. These points are not representative of the population you are studying.
  • Non-Error Outliers: These are legitimate but extreme values (e.g., the salary of a CEO in a dataset of company employees). They are part of the population and can sometimes contain valuable information about the data's true variability or unique events.

This distinction directly informs our strategy:

  • Error outliers should almost always be corrected (if possible) or removed.
  • Non-error outliers require a more nuanced decision: we might keep them, remove them, or recode them.

2. The Main Strategies: Removal vs. Recoding

Now, let's get practical. The two most common strategies you will apply are removal (trimming) and recoding (clipping/capping). We will explore how to implement these using several popular detection methods.

This video provides an excellent hands-on walkthrough of three methods for identifying outliers and then applying both trimming and capping to handle them.

How to Detect and Remove Outliers in the Data | Python

This video from Hackers Realm will be our primary guide. It covers the Z-score, IQR, and Percentile methods for detecting outliers and demonstrates both removal and capping for each.

Watch from timestamp 01:55 to 23:50. For each of the three methods covered (Z-score, IQR, Percentile), pay close attention to: How the upper and lower limits are calculated. The code for 'trimming' (removing outliers). The code for 'capping' (recoding outliers).

Let's consolidate what you've just seen.

Strategy 1: Removal (Trimming)

This is the most straightforward approach: you identify the outliers and simply delete the entire row from your dataset.

  • When to use: Best suited for clear error outliers where you are certain the data point is invalid.
  • Implementation: As shown in the video, you calculate your bounds (e.g., using IQR) and then filter the DataFrame to keep only the rows within those bounds.



# Example using the IQR method
Q1 = df['feature'].quantile(0.25)
Q3 = df['feature'].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR




# Trimming the outliers
df_trimmed = df[(df['feature'] >= lower_bound) & (df['feature'] <= upper_bound)]
Dataset After Outlier Removal: Scatter and Box Plots
This image shows a dataset after outliers have been removed. Notice in the box plot on the right that there are no data points beyond the whiskers, indicating a clean, trimmed dataset.

The Big Risk of Removal

While simple, removal can be dangerous. If you have many outliers or if they occur across multiple features, you could end up deleting a significant portion of your data, losing valuable information.

The next video provides a stark warning about this.

Complete Hands-on Outlier Treatment | Multiple Approaches Covered | Data Preprocessing in Python

This video from Six Sigma Pro SMART demonstrates how quickly you can lose data when removing outliers, especially in a dataset with many features.

Watch from timestamp 07:59 to 10:50. The key takeaway is seeing how removing outliers from just one column can lose over 10% of the data, and removing them from all columns could discard nearly half the dataset.

Strategy 2: Recoding (Clipping or Capping)

This is often a safer and more popular strategy. Instead of deleting the outlier, you change its value to be at a predefined upper or lower limit. This process is also known as Winsorization.

  • When to use: Excellent for non-error outliers. You acknowledge the data point is extreme but reduce its ability to skew your model or analysis, all while retaining the rest of the information in that row.
  • Implementation: You use the same bounds as before, but instead of filtering, you use a function like np.where() to replace values outside the bounds with the boundary values themselves.



# Example using the same IQR bounds
# Clipping the outliers
df['feature_clipped'] = np.where(df['feature'] > upper_bound, upper_bound, 
                           np.where(df['feature'] < lower_bound, lower_bound, df['feature']))

The video from Hackers Realm (resource LINK) you watched earlier demonstrated this "capping" technique for all three detection methods. It's a robust way to control extreme values without losing data records.


3. A More Advanced Approach: Imputation

For context, it's worth knowing about a more sophisticated recoding technique. This method treats outliers as missing values and then uses an imputation strategy to estimate a replacement value based on other features in the dataset. This can be very powerful but is also more complex.

The video below introduces this idea. Don't worry about mastering the code today; the goal is to understand the concept as it connects back to our earlier lessons on handling missing data.

Complete Hands-on Outlier Treatment | Multiple Approaches Covered | Data Preprocessing in Python

Let's return to the Six Sigma Pro SMART video to see an advanced method that combines outlier detection with imputation.

Watch from timestamp 17:33 to 23:11. Focus on the overall idea: outliers are first replaced with 'NaN' (missing values), and then a K-Nearest Neighbors (KNN) imputer is used to fill in those missing values based on other features.

This method is powerful because the replacement value is context-aware, but it also makes strong assumptions and requires careful implementation, including feature scaling. For now, it's an excellent concept to be aware of for future projects.


Conclusion

You've now learned the essential strategies for handling outliers, a critical step in cleaning your data for machine learning.

Key Takeaways:

  • The first step is diagnosis: Is the outlier an error or a legitimate, extreme value? This determines your strategy.
  • Removal (Trimming): A simple method best used for clear errors. Be very cautious, as it can lead to significant data loss.
  • Recoding (Capping/Clipping): A much safer and more common approach. It reduces the influence of extreme values without deleting entire records. This is often a great default choice for non-error outliers.
  • Methods for Setting Boundaries: You can define what constitutes an "outlier" using several techniques, including the Z-score (for normally distributed data), the IQR method (for skewed data), and percentiles.

Preview of the next lesson:

With our data now cleaned, scaled, and treated for outliers, we are in a great position to start creating new, more powerful predictors. In the next lesson, we will dive into Feature Engineering, where you'll learn to engineer new features from existing ones, including polynomial, interaction, and temporal features.