Hello! In our last lesson, we mastered the art of "signal engineering" for categorical data, learning how to translate categories into numbers without misleading our models. We saw that the choice between methods like one-hot and target encoding involves a careful balance of dimensionality, predictive power, and the risk of data leakage.
Today, we continue our journey through data preprocessing by tackling another common and critical challenge: outliers. These are the extreme values that can lurk in datasets, waiting to skew your analysis and sabotage your model's performance. This lesson is designed to help you master the learning outcome of detecting and treating outliers using statistical methods and domain knowledge. For a fintech interview, your ability to discuss outliers is crucial. An outlier could be a data entry mistake, or it could be a multi-million dollar fraudulent transaction—knowing how to tell the difference and what to do about it is a key engineering skill.
What Are Outliers and Why Do They Matter?
First, let's establish a clear definition. An outlier is a data point that deviates significantly from the majority of the observations in a dataset.
Detect and Remove Outliers in Python: IQR and Z-Score · Subhajit Bhar
This article by Subhajit Bhar provides a great introduction to what outliers are and where they come from.
Read the first two sections, What Are Outliers and Types of Outliers. Pay attention to the distinction between causes like data entry errors and genuine natural variation.
The most important question is: why should we care? The presence of outliers can have a profound impact on machine learning models. Some algorithms are highly sensitive to them, while others are more robust. Understanding this distinction is fundamental.
The following video provides an excellent visual intuition for how outliers can distort the decision boundaries of several common algorithms.
Outliers : Data Science Basics
This video from the ritvikmath channel clearly demonstrates how different models react to the presence of outliers.
Watch the segment explaining the impact on linear and logistic regression. Notice how the decision boundary shifts dramatically to accommodate the outliers, often at the expense of correctly classifying the non-outlier data. Next, see how K-Nearest Neighbors is affected in the section from here. Finally, watch the explanation of why decision trees are robust to outliers.
As the video demonstrates, the impact of outliers varies significantly by model family:
- Sensitive Models: Linear models (like Linear and Logistic Regression) and distance-based models (like K-Nearest Neighbors and SVMs) are highly sensitive. Their objective functions or distance calculations are pulled by extreme values, often resulting in a model that fits neither the main data distribution nor the outliers well.
- Robust Models: Tree-based models (like Decision Trees, Random Forests, and Gradient Boosting) are largely robust. They partition data based on finding the best split points. An outlier, no matter how extreme its value, will typically just be isolated on one side of a split without influencing the entire model structure.
Detecting Outliers: Statistical Methods
Now that we understand the "why," let's focus on the "how." The two most common statistical methods for detecting outliers in a single variable (univariate outliers) are the Z-score and the Interquartile Range (IQR).
Method 1: The Z-Score
The Z-score tells us how many standard deviations a data point is from the mean. It's a powerful tool when your data is normally distributed (bell-shaped).
The formula is:
where is the data point, is the mean of the distribution, and is the standard deviation.
A common rule of thumb is to identify a data point as an outlier if its absolute Z-score is greater than 3. This is because, in a normal distribution, over 99.7% of the data lies within 3 standard deviations of the mean.

Method 2: The Interquartile Range (IQR)
What if your data isn't normally distributed? This is common in the real world, especially in fintech where data like transaction amounts or income can be heavily skewed. The Z-score is less reliable here because the mean and standard deviation are themselves sensitive to outliers.
The Interquartile Range (IQR) method is a more robust alternative. It's based on the median and quartiles, which are not easily skewed by extreme values.
The process is:
- Calculate the first quartile (Q1), the 25th percentile.
- Calculate the third quartile (Q3), the 75th percentile.
- Calculate the IQR: .
- Define the outlier boundaries:
- Lower Bound:
- Upper Bound:
Any data point that falls outside these bounds is considered an outlier. This is the same logic used to draw the "whiskers" on a box plot.

Choosing Your Method
For an interview, you should be able to articulate which method to use and why.
Outlier Detection: IQR Method vs Z-Score vs Modified Z-Score (Complete Guide) - VrcAcademy
This guide from VrcAcademy provides clear formulas and numerical examples for both the IQR and Z-score methods, and offers practical advice on when to use each.
First, review the IQR Method section for a worked-out example. Next, read the Z-Score Method section to see its application. Finally, read the section Practical Considerations which gives a concise summary of when to choose each method.
Here's the decision framework in a nutshell:
- Use Z-Score when the feature is approximately normally distributed.
- Use IQR when the feature is skewed, or if you don't know the distribution. It is the more robust and generally safer choice.
Treating Outliers: A Matter of Judgment
Once you've detected an outlier, what do you do? This is where statistical rules end and engineering judgment begins. Blindly deleting outliers is often the wrong move and can be a red flag in an interview.
The Golden Rule: Investigate First
Before taking any action, you must understand the outlier's context.
Feature Engineering in Python 5- How to Detect Outliers in Machine Learning
This video from KGP Talkie provides an excellent real-world framing for the decision of whether to keep or remove an outlier, which is highly relevant for your fintech interview.
Watch the segment from here, which contrasts outliers in fuel monitoring versus fraud detection.
As the video highlights, in a fintech context like fraud detection, the outlier is often the signal you care about most! A fraudulent transaction is, by its nature, an anomaly. Deleting it would be throwing away the very thing your model needs to learn. Conversely, an outlier caused by a sensor glitch or data entry error is noise that should be corrected or removed. Domain knowledge is paramount.
Common Treatment Strategies
If you've investigated and decided that an outlier needs to be handled, you have several options.
Outliers : Data Science Basics
Let's return to the ritvikmath video, which clearly explains the two most common treatment strategies and the crucial philosophy behind outlier handling.
First, watch the section explaining trimming and windsorizing. Pay attention to the pros and cons of each. Then, watch the final, most important segment on the general philosophy of handling outliers. This is a senior-level perspective that will impress interviewers.
Here is a summary of your main options:
-
Removal (Trimming): Delete the outlier data points.
- When: Best for outliers that are confirmed data entry or measurement errors.
- Trade-off: Simple, but results in data loss. Can be problematic with small datasets.
-
Capping/Flooring (Winsorization): Replace outliers with the nearest "sensible" value. For example, cap all values above the 95th percentile at the 95th percentile value.
- When: Useful when you want to mitigate the effect of extreme values but cannot afford to lose the entire data record.
- Trade-off: Retains the data point but artificially reduces variance and compresses the distribution at the tails.
-
Transformation: Apply a mathematical function to the feature, such as a log, square root, or Box-Cox transformation.
- When: Often used for skewed data. These transformations can pull in the long tail of a distribution, making the extreme values less impactful.
- Trade-off: Can make model interpretation more complex as the feature is no longer in its original units.
-
Do Nothing: If the outliers are legitimate, rare events and you are using a robust model (like a Random Forest), your best option may be to leave them as they are.
The key takeaway, as emphasized in the video, is to avoid a default, "out-of-the-box" solution. The right way to handle an outlier is to investigate its cause and consider whether that cause could also be present in the new data your model will see in production.
Conclusion
You now have a complete framework for discussing outlier detection and treatment in a machine learning interview. You can move beyond simply stating a rule and instead articulate a thoughtful, context-aware engineering process.
Key Takeaways:
- Why It Matters: Outliers can severely distort statistics and harm the performance of sensitive models like linear regression, but robust models like random forests are less affected.
- Detection Methods: Use the Z-score for normally distributed data and the more robust IQR method for skewed data or when the distribution is unknown.
- Treatment is a Judgment Call: The first and most important step is to investigate the outlier. Is it an error or a valuable signal?
- Domain Knowledge is King: In a field like fintech, an outlier could be noise (a typo in a salary field) or the primary signal (a fraudulent charge). Your strategy must reflect this.
- Articulate Trade-offs: Be prepared to discuss the pros and cons of removing data (trimming) versus altering it (winsorizing/transformation).
In our next lesson, we will build upon our data preparation skills by moving from cleaning features to creating them. We'll explore how to engineer new, powerful features from temporal data, a technique essential for modeling time-dependent phenomena like transaction histories or stock prices.