Machine learning from zero · Lesson 4.1 · 20 of 33 lessons ready · 12 min
Exploration: look before you model
In short
Four data sets with the same mean, correlation 0.82 and line look nothing alike. Draw the data before you trust the numbers and before you build a model.
Warm-up Two questions from earlier lessons. A minute, and it never blocks the lesson.
Module 4 starts before the model. Before you train anything, look at the data. Exploratory data analysis (EDA) means drawing the data and computing simple summaries to see what you are dealing with: the shape, the groups, the points far from the rest. A mean or a correlation is a summary, and a summary can hide exactly what matters. Two examples today: Anscombe's four small data sets and the penguins of lesson 0.1.
Choose an answer and how sure you are.
Answer to unlock the lab.
Interactive lab
This interactive lab runs in a browser with JavaScript enabled. The text around it describes what the lab shows.
Show the answer
Correct answer: Completely different: a cloud, an arc, an outlier and one point far from the rest
Completely different. Set I is a cloud around the line. Set II lies on an arc: a parabola explains R² = 1.000 of the variation of y, the line only 0.666. In set III ten points lie almost exactly on a different, flatter line (slope 0.345), and one outlier pulls the fitted line towards itself, up to a slope of 0.5. In set IV ten points share the same x = 8, and a single point at (19; 12.50) sets the line.
Try it
- Click through sets I–IV. Which numbers under the charts change, and which stay the same?
- In which set does a single point set the line? What would happen to the line without it?
- Switch to the penguins and pick bill length × bill depth. What sign does the correlation have? Then split the penguins by species.
- Check the same pair split by island. Why is the picture different from the split by species?
Once you have checked it in the lab, let's name what you see.
The idea in plain words
Summary statistics, that is the mean, the variance, the correlation and the regression line, condense data into a few numbers. A summary always loses something. In 1973 Francis Anscombe built four sets of 11 points in which these numbers are almost identical: the mean of x is 9, the mean of y 7.50, the variance of x 11, the variance of y 4.12–4.13, the correlation 0.816, the line y = 3.00 + 0.500·x. On a chart set I is a cloud, II an arc, III a line with one outlier, IV a vertical column and one distant point. Linear regression fitted to each of them gives the same line, although it sensibly describes only the first set.
The penguins show the same on real data. Over all 342 penguins the correlation of bill length and bill depth is −0.235: the longer the bill, the shallower it is. Split by species, the sign flips: Adelie 0.391, Chinstrap 0.654, Gentoo 0.643. Inside every species a longer bill is also deeper. One number for the whole set describes a relationship that exists in no group.
Jak to policzono
- Quartet: Anscombe (1973), The American Statistician. 4 sets of 11 points, copy from the seaborn-data repository. Variance divided by n − 1.
- Penguins: Palmer Penguins (Gorman, Williams and Fraser, 2014; palmerpenguins package: Horst, Hill and Gorman, 2020), licence CC0. 342 penguins with bill measurements, 3 species: Adelie 151, Chinstrap 68, Gentoo 123.
- Correlation r: Pearson's coefficient, from −1 to 1; 0 means no linear relationship.
- Lines: least squares, separately for every set and every group.
Why it works
Where does the flipped sign in the penguins come from? Gentoo have long but shallow bills: on average 47.5 mm long and 15.0 mm deep. Adelie have short, deep bills: 38.8 and 18.3 mm. One correlation for everyone mostly compares the species with each other: Gentoo sit in the bottom right of the chart, Adelie in the top left, so the line through all points falls. Inside every species the relationship is positive, because a bigger penguin has a bigger bill in both directions. That is Simpson's paradox: when groups differ in their means, the relationship in the whole can have the opposite sign to the one in every group.
In the quartet the mechanism is even simpler. The mean, the variance and the correlation are sums over the points, and different arrangements of points give the same sums, just as different sets of numbers have the same mean. Points far from the rest pull hardest. In set IV the point at x = 19 alone sets the slope, because the other 10 points share the same x = 8 and say nothing about direction.
Is the penguin result a fluke of this one sample? In the code below we resample the penguins with replacement 1000 times: the same number of penguins, some twice, some not at all. In 1000 of 1000 resamples the correlation over all is negative and positive in every species.
Common mistake
True or false: “summary statistics are enough; if two data sets have the same mean, variance and correlation, they look alike”. False. In the lab Anscombe's four sets share these numbers to two decimal places and look completely different. Numbers summarise: they do not show the shape, the groups or the single points that pull the result. In the penguins one correlation of −0.235 says “longer bill, shallower bill”, and inside every species it is the other way round.
For a model this has concrete consequences. Linear regression fitted to set II predicts a straight line where the data lie on an arc. A penguin model that does not know the species learns a negative relationship that exists in no species. That is why you draw the data first, and only then choose the model and the features.
Quiz
Three questions to check yourself. Pick an answer, or open the answer under a question. Keyboard: arrow keys choose, Enter or Space confirms.
Key takeaways
- Before you build a model, draw the data: the distribution of every feature, pairs of features and groups.
- The same means, variances and correlations can hide a cloud, an arc, an outlier or a leverage point.
- A relationship in the whole data can have the opposite sign to the one in every group (Simpson's paradox); colour your charts by group.
- A single distant point can set the line on its own: check what changes without it.
Going further: formula, code, practice
The math
Pearson correlation: r = Σ(xᵢ − x̄)(yᵢ − ȳ) / √(Σ(xᵢ − x̄)² · Σ(yᵢ − ȳ)²). Least-squares line: b = r · s_y / s_x, a = ȳ − b · x̄. So the line depends only on the means, the standard deviations and the correlation: data sets that share these numbers must share the line, whatever they look like.
Simpson's paradox in a correlation comes from splitting the covariance by group: cov(x, y) = Σ_g p_g · cov_g(x, y) + Σ_g p_g · (x̄_g − x̄)(ȳ_g − ȳ), where p_g is the share of group g. The first term is the relationship inside the groups, the second the relationship between the group means. When the group means lie on a falling line, the second term is negative and can outweigh the positive first one.
In code (Python)
The same computation in pandas: the numbers of the quartet, the bill correlation over all penguins and inside the species, and 1000 resamples with replacement that check whether the flipped sign is a fluke of this sample.
import numpy as np
import pandas as pd
q = pd.read_csv("data/raw/anscombe.csv")
for name, d in q.groupby("dataset"):
slope, intercept = np.polyfit(d.x, d.y, 1)
print(f"set {name}: mean x {d.x.mean():.2f}, mean y {d.y.mean():.2f}, variance x {d.x.var():.2f}, variance y {d.y.var():.2f}, r {d.x.corr(d.y):.3f}, line y = {intercept:.2f} + {slope:.3f}x")
df = pd.read_csv("data/raw/penguins.csv").dropna(subset=["bill_length_mm", "bill_depth_mm"])
r = lambda d: d.bill_length_mm.corr(d.bill_depth_mm)
print(f"{len(df)} penguins, bill length vs bill depth: all r = {r(df):.3f}; " + ", ".join(f"{s} {r(g):.3f}" for s, g in df.groupby("species")))
rng = np.random.default_rng(0)
same = 0
for b in range(1000):
s = df.sample(len(df), replace=True, random_state=rng) # a resample with replacement: as many penguins, some twice
same += r(s) < 0 and all(r(g) > 0 for _, g in s.groupby("species"))
print(f"1000 bootstrap resamples: negative over all and positive in every species in {same}")
Output
set I: mean x 9.00, mean y 7.50, variance x 11.00, variance y 4.13, r 0.816, line y = 3.00 + 0.500x
set II: mean x 9.00, mean y 7.50, variance x 11.00, variance y 4.13, r 0.816, line y = 3.00 + 0.500x
set III: mean x 9.00, mean y 7.50, variance x 11.00, variance y 4.12, r 0.816, line y = 3.00 + 0.500x
set IV: mean x 9.00, mean y 7.50, variance x 11.00, variance y 4.12, r 0.817, line y = 3.00 + 0.500x
342 penguins, bill length vs bill depth: all r = -0.235; Adelie 0.391, Chinstrap 0.654, Gentoo 0.643
1000 bootstrap resamples: negative over all and positive in every species in 1000
The four sets differ only in the third decimal place. In the penguins the flipped sign is no fluke of this sample: it holds in 1000 of 1000 resamples with replacement.
Download the notebook (.ipynb). Open it in Jupyter or upload it to Google Colab; it fetches any missing data itself.
For practitioners
- In pandas:
df.describe(),df.isna().sum()anddf.groupby("species").mean(). In seabornpairplot(df, hue="species")draws every pair of measurements in group colours with one call. - Look at the distribution of every feature (histogram), at pairs of features (scatter plot) and at missing values before you split or train anything.
- Do the EDA that drives decisions (choosing features, thresholds, dropping rows) on the training part. Looking at the test set for such decisions is leakage (lesson 4.5).
- Write down what you saw: groups, outliers, odd codes (e.g. 0 instead of a missing value). It is a list of things to check when the model behaves strangely.
Was this helpful?
Thanks! This helps us improve the Atlas.
Go deeper in the Atlas
Encyclopedia entries on the ideas in this lesson: