liveBook · Manning

Interquartile range

As explained in Outlier Detection in Python

The interquartile range is a measure of statistical dispersion equal to the difference between the third and first quartiles of a distribution.

The interquartile range (IQR) measures the spread of the middle half of a numeric distribution: IQR = Q3 − Q1, where Q1 and Q3 are the 25th and 75th percentiles. Because it uses quartiles and the median rather than the mean and standard deviation, it is more robust to outliers than a z-score. section 2.1.2

Use the IQR method to identify unusually extreme values in a numeric sequence:

  • Calculate Q1 and Q3, then subtract Q1 from Q3 to obtain the IQR. section 2.1.2
  • Set lower and upper outlier limits to Q1 − 2.2 × IQR and Q3 + 2.2 × IQR. Other coefficients are possible; Tukey’s traditional coefficient is 1.5. section 2.1.2
  • Treat values outside the limits as candidates for investigation, not automatically meaningful anomalies. section 2.0

IQR-based anomaly detection

For a pandas Series, calculate the 25th and 75th percentiles, derive their difference, and compute lower and upper limits. In a time series, ADTK provides InterQuartileRangeAD; its coefficient can be increased, such as 20.0, when many extreme values would otherwise be flagged. section 2.1.2 section 17.3.1

import pandas as pd
import numpy as np
 
data = pd.Series(np.random.normal(size=10_000))
q1 = data.quantile(0.25)
q3 = data.quantile(0.75)
iqr = q3 - q1
iqr_lower_limit = q1 - (2.2 * iqr)    
iqr_upper_limit = q3 + (2.2 * iqr)

Inspect the concise pandas and NumPy implementation of quartile calculation and IQR thresholds. section 2.1.2

IQR is primarily a univariate method applied to individual numeric columns. It can quickly expose dominant extreme values before more detailed detection, but it does not capture relationships across a full table. Boxplots use an IQR-based whisker rule, commonly with a 1.5 coefficient, although multimodal data can make quartile-based judgments misleading. section 2.0 section 2.3.2

How Outlier Detection in Python covers it

How deep it goes
Explains quartile calculation, threshold formulas, coefficient choices, implementation, and univariate limitations.
Examples
Uses pandas and NumPy thresholds, Gaussian distribution figures, boxplots, and ADTK detection on Twitter time-series data.
What it assumes
Assumes numeric data and percentile terminology while teaching quartiles, IQR calculation, and threshold selection.
What you can do after
Apply it via pandas or ADTK to calculate robust thresholds and flag candidate extreme values.
How it connects
Connects IQR with quartiles, median, z-scores, standard deviation, IDR, boxplots, and outlier detection.

Also explained in

Compare how 7 books explain interquartile range →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Outlier Detection in Python. The section links point to the parts of the book it draws on.