All articles
Machine Learning
ml-features

Detecting and Handling Outliers in Machine Learning

Master the essential techniques for identifying and managing outliers in your datasets. Learn how IQR and Z-scores impact model performance and accuracy.

Math Instructor AI 22 September 2026 8 min read

Introduction to Outliers in Machine Learning

In the field of machine learning, an outlier is an observation that deviates significantly from the rest of the data, appearing as an anomaly that does not fit the general pattern. For students and practitioners, understanding how to identify and manage these points is critical. If left unaddressed, outliers can skew statistical measures like the mean and standard deviation, leading to biased models that fail to generalise well to new, unseen data.

This article explores the mathematical foundations of outlier detection. You will learn how to apply statistical methods such as the Interquartile Range (IQR) and Z-scores to clean your datasets effectively. Mastering these techniques is essential for your coursework and for building robust, high-performance machine learning pipelines.

The Impact of Outliers on Model Performance

Outliers act as noise in your dataset. Many machine learning algorithms, particularly those sensitive to scale or distance, are heavily influenced by extreme values. For instance, linear regression models attempt to minimise the sum of squared residuals; a single extreme outlier can pull the regression line towards itself, significantly degrading the model's predictive accuracy. Similarly, distance-based algorithms like K-Nearest Neighbours (KNN) may produce incorrect classifications if the distance metrics are distorted by anomalous points.

Detecting Outliers with the Interquartile Range (IQR)

The IQR method is a robust, non-parametric approach to outlier detection. It relies on the spread of the middle 50% of your data, making it less sensitive to extreme values than the mean.

Step-by-Step Calculation:

  1. Calculate the first quartile ($Q_1$), which is the 25th percentile.
  2. Calculate the third quartile ($Q_3$), which is the 75th percentile.
  3. Compute the IQR: $IQR = Q_3 - Q_1$.
  4. Define the bounds:
    • Lower Bound = $Q_1 - 1.5 \times IQR$
    • Upper Bound = $Q_3 + 1.5 \times IQR$
  5. Any data point outside these bounds is considered an outlier.

Worked Example 1:

Consider the dataset: $[10, 12, 12, 13, 12, 11, 14, 100]$.

  • Sorted: $[10, 11, 12, 12, 12, 13, 14, 100]$.
  • $Q_1$ (median of lower half) = $11.5$.
  • $Q_3$ (median of upper half) = $13.5$.
  • $IQR = 13.5 - 11.5 = 2.0$.
  • Lower Bound = $11.5 - (1.5 \times 2.0) = 8.5$.
  • Upper Bound = $13.5 + (1.5 \times 2.0) = 16.5$.
  • Result: $100$ is an outlier because $100 > 16.5$.

Detecting Outliers with Z-Scores

The Z-score method measures how many standard deviations a data point is from the mean. It assumes a roughly normal distribution.

The Formula:

$$Z = \frac{x - \mu}{\sigma}$$ Where $x$ is the value, $\mu$ is the mean, and $\sigma$ is the standard deviation. Typically, a Z-score threshold of $\pm 3$ is used; any value with an absolute Z-score greater than 3 is flagged as an outlier.

Worked Example 2:

Dataset: $[10, 12, 12, 13, 12, 11, 14]$.

  • Mean ($\mu$) $\approx 12$.
  • Standard Deviation ($\sigma$) $\approx 1.29$.
  • For $x = 14$: $Z = (14 - 12) / 1.29 \approx 1.55$ (Not an outlier).

Handling Outliers: Strategies for Success

Once detected, you must decide how to handle them:

  1. Trimming/Removal: If the outlier is a result of data entry error or sensor failure, removing it is often the best approach.
  2. Winsorisation: Capping the extreme values at a specific percentile (e.g., 95th percentile) to reduce their influence without losing the data point.
  3. Transformation: Applying logarithmic or square root transformations can compress the range of the data, making the distribution more normal and reducing the impact of outliers.
  4. Robust Models: Using algorithms less sensitive to outliers, such as Tree-based models (Random Forest, Gradient Boosting) or Median-based regression.

Common Mistakes

  • Blind Removal: Removing outliers without investigating their cause. Sometimes, an outlier represents a rare but important event (e.g., fraud detection).
  • Ignoring Distribution: Applying Z-scores to highly skewed data. Z-scores assume normality; for skewed data, IQR is safer.
  • Data Leakage: Calculating statistics like the mean or IQR on the entire dataset before splitting into training and testing sets.

Frequently Asked Questions

Q: Should I always remove outliers? No. If the outlier represents a genuine, rare event, removing it will lead to a model that fails to predict such events in production.

Q: Is IQR better than Z-score? IQR is more robust to extreme values because it uses medians and quartiles, whereas Z-score is sensitive to the mean and standard deviation, which are themselves affected by outliers.

Q: Can I use machine learning to detect outliers? Yes. Algorithms like Isolation Forest or Local Outlier Factor (LOF) are excellent for high-dimensional datasets where simple statistical bounds fail.

Conclusion

Detecting and handling outliers is a fundamental skill for any machine learning engineer. By choosing the right statistical method—whether IQR for non-parametric data or Z-scores for normal distributions—you ensure your models remain accurate and reliable. To see these concepts in action, visit MathInstructor AI to generate a free, narrated animated lesson on this topic.

Topics

outliers
anomaly detection
machine learning
iqr
z score
ml-features
data cleaning
statistics
data preprocessing

Want this explained out loud?

Turn any question into a narrated, animated lesson in seconds.

Try the Studio free