Statistics for ML
Statistics is the practice of summarizing data and deciding what it can and can't tell you. In machine learning it is used to describe datasets, measure model error, and test whether a difference, such as a better model or a better email, is real or just chance.
Summaries, Correlation and Tests
| Statistic | Syntax | Description |
|---|---|---|
| Mean | x.mean() | It is used for the average; sensitive to extreme values |
| Median | np.median(x) | It is used for the middle value; robust to extremes |
| Variance | x.var(ddof=1) | It is used to measure spread: the average squared distance from the mean |
| Standard deviation | x.std(ddof=1) | It is used for spread in the data's own units |
| Correlation | np.corrcoef(x, y)[0, 1] | It is used to measure how two quantities move together, from -1 to 1 |
| p-value | From a test, as below | It is used to judge how surprising a result would be if there were no real effect |
Example
import numpy as np
hours = np.array([1, 2, 2, 3, 4, 4, 5, 6, 6, 8]) # study hours before a test
scores = np.array([58, 62, 65, 70, 72, 75, 80, 83, 85, 92])
print(f"Mean score {scores.mean():.1f}, median {np.median(scores):.1f}")
print(f"Variance {scores.var(ddof=1):.1f}, standard deviation {scores.std(ddof=1):.1f}")
print(f"Correlation of hours and scores: {np.corrcoef(hours, scores)[0, 1]:.2f}")
# A/B test: did a new reminder email get more fee payments on time?
old = np.array([1] * 52 + [0] * 48) # 52 of 100 paid on time
new = np.array([1] * 64 + [0] * 36) # 64 of 100 paid on time
observed = new.mean() - old.mean()
# Permutation test: if the email made no difference, how often would shuffling give a gap this big?
rng = np.random.default_rng(0)
both = np.concatenate([old, new])
gaps = []
for _ in range(10_000):
rng.shuffle(both)
gaps.append(both[100:].mean() - both[:100].mean())
p_value = np.mean(np.array(gaps) >= observed)
print(f"Difference: {observed:.2f}, p-value: {p_value:.3f}")Mean score 74.2, median 73.5
Variance 118.2, standard deviation 10.9
Correlation of hours and scores: 0.99
Difference: 0.12, p-value: 0.055Explanation: Scores average 74.2 with a standard deviation of 10.9 points. Hours and scores have a correlation of 0.99: students who studied more scored higher. For the email test, the new reminder got 12 more on-time payments per 100. The permutation test asks: if the email made no difference, how often would randomly shuffling the 200 results produce a gap of 0.12 or more? About 5.5% of the time. That's above the common 5% cut-off, so this test can't yet rule out chance; a larger test is needed.
Sampling
A sample is the part of the data you actually have; the population is everything you care about. Statistics estimates the population from the sample, so samples must be representative: a survey of parents who already use the app says little about those who don't.
Common Mistakes
Reporting only the mean. Add the median and spread; extremes can distort the mean.
Treating p = 0.055 as "almost significant proof". It means the evidence isn't strong enough yet.
Using a biased sample. Results only generalize to the population the sample represents.
Interview Corner
What is a p-value?
The probability of seeing a result at least as extreme as the one observed, if there were really no effect. A small p-value suggests the effect is unlikely to be chance alone; it doesn't measure how big or important the effect is.
Why prefer the median to the mean sometimes?
The median is robust to extreme values, so it describes a typical value better when data has outliers, such as house prices or response times.
Practice
Exercise: a bigger test. Change the A/B test to 400 parents per group with the same rates (208 and 256 paying on time). Rerun the permutation test. What happens to the p-value, and why?
Check your understanding:
- What does the standard deviation measure?
- Why doesn't a correlation of 0.99 prove that studying causes higher scores?
- What did the p-value of 0.055 mean?
Key Takeaways
Quick reference
| Need | Code |
|---|---|
| Average | x.mean() |
| Middle | np.median(x) |
| Spread | x.std(ddof=1) |
| Relationship | np.corrcoef(x, y)[0, 1] |
Was this tutorial helpful?