Menu
CoddyTech

The IQR Rule

Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 25 of 64.

A box plot draws far values as dots. To find them in code, turn its rule into two fences, 1.5 IQRs beyond each edge of the box:

12 concert ticket prices as dots on a line. Ten sit inside a narrow box around 37 to 43 euros. Dashed fences stand 1.5 IQRs out from each edge of the box, at 28.1 and 51.1, and the prices 18 and 72 lie beyond them, marked as outliers in red

In code, with masks:

prices = np.array([35, 42, 38, 45, 40, 72, 37, 44, 39, 41, 36, 18])
q1 = np.percentile(prices, 25)
q3 = np.percentile(prices, 75)
iqr = q3 - q1
low = q1 - 1.5 * iqr
high = q3 + 1.5 * iqr
print(low, high)
print(prices[(prices < low) | (prices > high)])

This prints:

28.125 51.125
[72 18]

Quartiles ignore extremes, so an outlier cannot stretch the fences to hide itself. Flag these values before training: one wild value can pull a model toward it.

challenge icon

Challenge

Medium

A teacher timed a quiz, and the program reads each student's minutes into minutes. The starter prints the fences that iqr_fences returns.

  1. Complete iqr_fences(a) so it returns the lower and the upper IQR fence of a.
  2. Below, print Unusual: and the times outside the fences, in their original order.

For 9 students taking 8, 11, 9, 10, 27, 12, 9, 10 and 11 minutes:

Fences: 6.0 to 14.0 minutes
Unusual: [27]

Try it yourself

import numpy as np


# Returns the lower and upper IQR fences of an array
def iqr_fences(a):
    # Write your code here
    return 0.0, 0.0


n = int(input())
values = []
for i in range(n):
    values.append(int(input()))
minutes = np.array(values)

# Don't change the lines below
low, high = iqr_fences(minutes)
print(f"Fences: {np.round(low, 2)} to {np.round(high, 2)} minutes")

# Write your code below
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler