The Z-Score Rule
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 26 of 64.
A farm weighed 300 eggs, then typed two by hand: 6.2 g and 94.6 g, both typos. Their z-scores (distances from the mean, in std) give them away:
np.abs drops minus signs, so one comparison catches both sides:
rng = np.random.default_rng(3)
eggs = np.concatenate([rng.normal(60, 4, size=300), np.array([6.2, 94.6])])
z = (eggs - eggs.mean()) / eggs.std()
print(np.abs(np.array([-4, 2, -1])))
print(eggs[np.abs(z) > 3])This prints:
[4 2 1]
[ 6.2 94.6]In bell-shaped data, 99.7% of values lie within 3 std, so np.abs(z) > 3 flags the rare values to check before training.
But an outlier inflates the std it is measured with, so small samples hide it: use the IQR rule there.
Challenge
MediumA bakery's scale weighed 250 loaves into loaves, and 3 more were typed in by hand. The program reads a cutoff.
- Compute every loaf's z-score with the array's
.mean()and.std(). - In position order, print each loaf whose z-score is more than the cutoff from 0: its position, weight (1 decimal) and z-score (2 decimals).
- Print how many you listed.
For another day and the cutoff 3:
Loaf 200: 702.5 g, z = -5.38
Loaf 201: 889.3 g, z = 4.91
Loaves to re-weigh: 2Try it yourself
import numpy as np
# Don't change the lines below: 250 loaves weighed by the scale, then 3 weights typed in by hand
rng = np.random.default_rng(25)
loaves = np.concatenate([rng.normal(800, 15, size=250), np.array([731.4, 868.9, 912.3])])
cutoff = float(input())
# 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