Outlier Detection with the IQR Rule

easy
Unit 4 · Statistics & Experimentation0/3 to milestone

The IQR rule flags values far outside the middle 50% of the data. Unlike z-scores, it isn't thrown off by the outliers themselves.

IQR=Q3−Q1,outlier if x<Q1−k⋅IQR or x>Q3+k⋅IQR\text{IQR} = Q_3 - Q_1, \qquad \text{outlier if } x < Q_1 - k \cdot \text{IQR} \text{ or } x > Q_3 + k \cdot \text{IQR}

Implement iqr_outliers(values, k=1.5) returning (lower, upper, outlier_indices):

  • Q1Q_1 and Q3Q_3 are the 25th and 75th percentiles from np.percentile (its default linear interpolation).
  • outlier_indices is a sorted list of the positions of values strictly outside [lower, upper].
  • Ignore NaN values when computing the quartiles, and never flag them.
#statistics#outliers#numpy2 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Outlier Detection with the IQR Rule · TechGradify