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:
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
MediumA teacher timed a quiz, and the program reads each student's minutes into minutes. The starter prints the fences that iqr_fences returns.
- Complete
iqr_fences(a)so it returns the lower and the upper IQR fence ofa. - 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler