DEV Community

John Martal
John Martal

Posted on

How to Detect Outliers in Python Using the Interquartile Range (IQR)

When cleaning tabular data or preprocessing datasets for machine learning models, identifying outliers is a critical step. Outliers can skew statistical distributions and negatively impact model training performance.

The Interquartile Range (IQR) method is a simple, non-parametric approach to detecting extreme values without assuming a normal distribution.


What is the Interquartile Range (IQR)?

The IQR measures the spread of the middle 50% of your data. It is calculated as the difference between the 75th percentile ($Q_3$) and the 25th percentile ($Q_1$):
IQR Outlier Calculator

$$\text{IQR} = Q_3 - Q_1$$

Outlier Thresholds

Using the 1.5 $\times$ IQR rule, data points are flagged as outliers if they fall outside these boundary limits:

  • Lower Bound: $Q_1 - (1.5 \times \text{IQR})$
  • Upper Bound: $Q_3 + (1.5 \times \text{IQR})$

Python Implementation

You can implement IQR outlier detection in Python using pandas and numpy:


python
import pandas as pd
import numpy as np

def detect_outliers_iqr(data: pd.Series):
    """
    Returns lower bound, upper bound, and flagged outliers in a Series.
    """
    q1 = data.quantile(0.25)
    q3 = data.quantile(0.75)
    iqr = q3 - q1

    lower_bound = q1 - (1.5 * iqr)
    upper_bound = q3 + (1.5 * iqr)

    outliers = data[(data < lower_bound) | (data > upper_bound)]
    return lower_bound, upper_bound, outliers

# Example dataset
scores = pd.Series([12, 14, 15, 18, 19, 19, 21, 23, 24, 105])
lower, upper, outliers = detect_outliers_iqr(scores)

print(f"Lower Bound: {lower}")
print(f"Upper Bound: {upper}")
print(f"Detected Outliers:\n{outliers.tolist()}")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)