Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
Subscribe to unlock this lesson. ×
← Statistics
1 See it
2 When
3 How
4 Predict
5 Practise
6 Fix it
✓ Finish
0 / 9
📖 Cheat sheet
🧪 Playground
Loading Python, NumPy, pandas and SciPy in your browser… (first time ~35 MB, then cached)
Lesson 01 📏 Describing dataFoundation
Centre: mean, median and the pull of outliers
One number for "typical". The mean balances the data; the median splits it in half. One wild value tells them apart.
You’ll use Series.mean · Series.median · Series.mode · stats.trim_mean
Watch the idea at work
Fifteen deliveries from the courier log. Where is the **typical** time? Two answers fight for the job: the **mean** and the **median**. Drag the slider to add one very late delivery and watch which one moves.
The mental model The **mean** is the balance point: every value pulls on it, so one extreme value pulls hard. The **median** is the middle of the sorted values: it only cares about order, so outliers barely move it.
Why it matters "Typical" drives decisions: staffing, pricing, promises to customers. Quote the mean of a skewed variable and you describe a customer, delivery or house that hardly exists.
🧪 Drag the slider to 160. Then imagine the late delivery were 10,000 minutes: which line would move, and which would stay put?
When to use it →
When to reach for it
The question it answers What is a typical value of this variable?
Use it when You need **one number** to summarise where the values sit. **Mean**: when totals matter (average spend × customers = revenue) and the data are roughly symmetric. **Median**: skewed data or outliers: times, prices, incomes, waiting lists.
Check first Look at the shape first: a long tail drags the mean (lesson 3). Check for impossible values (negative minutes, typos) before averaging anything. The **mode** suits categories and small whole numbers, not continuous measurements.
Reach for something else if… the variable is a category (plan, region) → the mode: `value_counts()` a few extreme values you cannot drop → the median or `stats.trim_mean(x, 0.1)` growth rates or ratios that multiply → the geometric mean: `stats.gmean`
Related: np.meannp.medianstats.trim_meanstats.gmeanSeries.mode
← See it How: the code →
The code, line by line
Tap a note to see its line.
▶ Run ↺ Reset ⌘/Ctrl + Enter · bare expressions show their value
Output
Run to see each result
Gotchas `np.mean` on a list with a NaN returns `nan`; pandas `.mean()` skips NaNs. Know which one you are calling. `.mode()` returns a Series, not a number. Use `.mode()[0]` for the first (smallest) one. The median of an even count is the average of the two middle values, so it may not be a value in the data.
Pro tips Report the mean **and** the median. If they differ a lot, the data are skewed, and that is worth saying out loud. Averages of averages mislead when groups differ in size: average the raw values instead.
← When Predict →
Think before you compute
← How Practise →
Your turn, on the course datasets
Datasets are loaded: load('name').
← Predict Fix it →
Spot the statistical mistake
Run it, spot the trap, repair it.
← Practise Finish →
0/9
Answer everything, then submit
8 of 9 correct passes the lesson and unlocks the next one.
Submit lesson
🧪 Playground — np, pd, stats and the datasets ✕
▶ Run Clear
Output
Just for you — nothing is graded. np, pd and scipy's stats are imported, and every dataset is one call away: load('cafe_sales'), load('ab_test'), load('fills'), load('deliveries'), load('bp_trial'), load('exams'), load('houses'), load('visits'), load('survey'). Bare expressions show their value.
📖 Statistics cheat sheet ✕
Describe
Centre Try it
s = load('cafe_sales').revenue
print(s.mean().round(2), s.median().round(2), stats.trim_mean(s, 0.1).round(2)) Mean, median, 10% trimmed mean: the median and trimmed mean shrug off outliers.
Spread Try it
s = load('fills').fill_ml
print(s.std().round(3), np.std(s).round(3), stats.iqr(s).round(2)) pandas .std() uses ddof=1 (sample); np.std defaults to ddof=0.
Shape Try it
s = load('deliveries').minutes
print(round(stats.skew(s), 3), round(stats.kurtosis(s), 3)) Skew > 0: a long right tail. Kurtosis here is excess (normal = 0).
Quantiles Try it
s = load('houses').price_k
print(s.quantile([0.25, 0.5, 0.75]).round(1).tolist()) Q1, median, Q3. IQR = Q3 − Q1; box-plot outliers lie beyond 1.5 × IQR.
z-scores Try it
s = load('exams').score
z = (s - s.mean()) / s.std()
print(z.abs().gt(2).sum()) How many SDs from the mean; |z| > 2 is unusual, > 3 rare.
Correlation Try it
h = load('houses')
print(round(stats.pearsonr(h.sqm, h.price_k)[0], 3), round(stats.spearmanr(h.sqm, h.price_k)[0], 3)) Pearson: linear. Spearman: ranks, so any monotone trend and robust to outliers.
Probability & simulation
Simulate Try it
rng = np.random.default_rng(42)
rolls = rng.integers(1, 7, size=10_000)
print((rolls == 6).mean()) Always seed the generator so results repeat.
Conditional Try it
a = load('ab_test')
print(a[a.device == 'desktop'].converted.mean().round(4)) P(convert | desktop): filter to the condition, then take the proportion.
Distributions (scipy.stats)
Normal Try it
d = stats.norm(250, 2)
print(round(d.cdf(247), 4), round(d.ppf(0.95), 2)) cdf: P(X ≤ x). ppf: the inverse — the x below which a share falls.
Binomial Try it
print(round(stats.binom(20, 0.1).pmf(3), 4)) pmf: P(X = k) for counts.
Poisson Try it
print(round(stats.poisson(4).sf(7), 4)) sf = 1 − cdf: P(X > k).
Tests
One-sample t Try it
f = load('fills').fill_ml
r = stats.ttest_1samp(f, 250)
print(round(r.statistic, 3), round(r.pvalue, 4)) Is the mean different from a target value?
Welch t Try it
a = load('ab_test')
m = a[a.converted == 1]
r = stats.ttest_ind(m[m.variant == 'B'].order_value, m[m.variant == 'A'].order_value, equal_var=False)
print(round(r.pvalue, 4)) Two independent groups, without assuming equal variances.
Paired t Try it
b = load('bp_trial')
r = stats.ttest_rel(b.bp_before, b.bp_after)
print(round(r.statistic, 2)) The same units measured twice: test the differences.
χ² independence Try it
s = load('survey')
t = pd.crosstab(s.plan, s.recommend)
chi2, p, dof, _ = stats.chi2_contingency(t)
print(round(chi2, 2), dof) Two categorical variables: are they related?
Regression (statsmodels)
OLS (formula) Try it
import statsmodels.formula.api as smf
h = load('houses')
m = smf.ols('price_k ~ sqm + C(district)', data=h).fit()
print(m.params.round(2).head(3).to_dict()) Formulas: C() makes a categorical; m.summary() is the full report.