"The 344 penguins in this dataset average 4.2 kg" and "penguins of this kind weigh about 4.2 kg" sound almost the same. They're very different claims. The first is descriptive: it summarises the data in front of you, and it's simply true. The second is inferential: it reaches beyond the data to penguins nobody measured, and it could be wrong.
Every statistical question is one or the other, and mixing them up is one of the most common mistakes in data work: a pattern spotted in one sample gets reported as a fact about the world. This hands-on lesson works through both kinds on the real penguins data. You'll describe the data, then test which of its patterns would survive if the scientists went back and measured a fresh set of penguins.
What you need first: types of data and population vs sample.
Key terms
- Descriptive statistics: numbers, tables and charts that summarise the data you have. No claims beyond it.
- Inferential statistics: methods that use a sample to draw conclusions about a population, with a measure of uncertainty.
- Summary statistics: the standard descriptive numbers for a column, such as count, mean, minimum and maximum.
- Generalise: to claim a pattern in a sample also holds in the population.
- Chance pattern: a pattern that appears in one sample by luck and wouldn't appear again.
Part 1: describe the data you have
Download the data: penguins.csv (15 KB), or from the original source, the palmerpenguins project. The code below loads it for you, so you only need the file to open it in a spreadsheet or work offline: load it with
pd.read_csv("penguins.csv").
import numpy as np
import pandas as pd
import seaborn as sns
pd.set_option("display.width", 120)
pd.set_option("display.max_columns", 10)
penguins = sns.load_dataset("penguins").dropna(subset=["body_mass_g"])
print(len(penguins))
print(penguins["body_mass_g"].describe().round(1))
342
count 342.0
mean 4201.8
std 802.0
min 2700.0
25% 3550.0
50% 4050.0
75% 4750.0
max 6300.0
Name: body_mass_g, dtype: float64
.dropna(subset=["body_mass_g"]) removes the 2 penguins whose weight wasn't recorded. describe() gives the standard summary of a numerical column in one call:
count: how many values there are, 342.mean: the average, 4,201.8 g.minandmax: the lightest penguin weighs 2,700 g and the heaviest 6,300 g.std,25%,50%and75%describe the spread; you'll learn them in the next module.
Now describe the weights by species:
by_species = penguins.groupby("species")["body_mass_g"].agg(["count", "mean"]).round(0)
print(by_species)
count mean
species
Adelie 151 3701.0
Chinstrap 68 3733.0
Gentoo 123 5076.0
groupby("species") splits the penguins into one group per species, and .agg(["count", "mean"]) works out both numbers for each group.
These are descriptive statements, and every one is true about this data:
- In this dataset, Gentoo penguins average 5,076 g, about 1,375 g more than Adelie penguins.
- In this dataset, Chinstrap penguins average 3,733 g, 32 g more than Adelie penguins.
Notice the words "in this dataset". Descriptive statistics never need to be uncertain, because they only talk about what was measured.
Part 2: would the pattern survive a new sample?
Now the inferential question. These 342 penguins are a sample of all the penguins of these species. Are Gentoos really heavier than Adelies, as a fact about the species? And are Chinstraps really heavier than Adelies?
Both differences point the same way in this sample. But population vs sample showed that every sample statistic wobbles. A 32 g difference could easily be luck: which particular penguins happened to get caught. Before you go on, predict: which of the two differences would you bet on still being there in a new sample?
You can't go to Antarctica to catch more penguins. But you can imitate "a different sample" by splitting the data you have into random halves, and checking whether each pattern shows up in both halves:
rng = np.random.default_rng(0)
shuffled = penguins.sample(frac=1, random_state=1)
half_a, half_b = shuffled.iloc[:171], shuffled.iloc[171:]
for name, half in [("half A", half_a), ("half B", half_b)]:
means = half.groupby("species")["body_mass_g"].mean()
print(f"{name}: Gentoo − Adelie = {means['Gentoo'] - means['Adelie']:6.0f} g,"
f" Chinstrap − Adelie = {means['Chinstrap'] - means['Adelie']:5.0f} g")
half A: Gentoo − Adelie = 1418 g, Chinstrap − Adelie = -119 g
half B: Gentoo − Adelie = 1333 g, Chinstrap − Adelie = 191 g
sample(frac=1) shuffles all the rows into a random order, and iloc[:171] takes the first 171. Each half is like a smaller, separate sample of penguins.
The Gentoo difference barely moves: about 1,400 g in each half. The Chinstrap difference doesn't even keep its sign: in half A, Chinstraps are 119 g lighter than Adelies, and in half B they're 191 g heavier. One split could be lucky, though, so repeat it a thousand times and count how often each pattern holds:
gentoo_heavier = chinstrap_heavier = 0
for _ in range(1000):
half = penguins.sample(frac=0.5, random_state=rng.integers(1_000_000))
means = half.groupby("species")["body_mass_g"].mean()
gentoo_heavier += means["Gentoo"] > means["Adelie"]
chinstrap_heavier += means["Chinstrap"] > means["Adelie"]
print("Gentoo heavier than Adelie: ", gentoo_heavier, "of 1000 halves")
print("Chinstrap heavier than Adelie:", chinstrap_heavier, "of 1000 halves")
Gentoo heavier than Adelie: 1000 of 1000 halves
Chinstrap heavier than Adelie: 714 of 1000 halves
sample(frac=0.5, ...) picks a random half of the penguins, with a different random seed each time.
In every one of the 1,000 halves, Gentoos come out heavier. That pattern is solid: you'd confidently generalise it. Chinstraps come out heavier in about 70% of halves, and lighter in about 30%. A real but tiny difference and a pure fluke would both look like this, so the data can't tell them apart. The honest inferential statement is: "Chinstrap and Adelie penguins weigh about the same; this data can't say which is heavier."
That's the heart of inferential statistics. The question is never just "is there a difference in my data?" (there almost always is). It's "is the difference bigger than the wobble you'd expect from chance?" Later in this course, confidence intervals, bootstrapping and hypothesis tests answer that question with precise numbers. The random-halves trick you just used is a rough version of the same idea.
Describing vs inferring, side by side
| Descriptive | Inferential | |
|---|---|---|
| Talks about | The data you have | A population beyond the data |
| Example | "In this dataset, Gentoos average 5,076 g" | "Gentoos are heavier than Adelies" |
| Uncertainty | None: it's arithmetic | Always: the answer could be wrong |
| Tools | Mean, median, spread, tables, charts | Confidence intervals, hypothesis tests, models |
| Typical wording | "In this sample…", "of the 342 penguins…" | "We estimate…", "the data suggests…", "about … give or take …" |
| Main risk | Choosing a misleading summary | Treating a chance pattern as real |
Machine learning sits firmly in the inferential column. A model is a pattern learned from a sample, and the whole point is to use it on new cases. That's why ML lessons are so careful about test sets: a model's score on its own training data is descriptive, while its score on unseen data estimates how it will do in the real world.
Common mistakes
- Reporting a sample pattern as a fact. "Chinstraps are heavier than Adelies" goes beyond what the data supports. Say "in this data" or check whether the pattern is bigger than the wobble.
- Being uncertain about descriptive facts. The average of these 342 weights is exactly 4,201.8 g. There's nothing to estimate.
- Generalising to the wrong population. These penguins came from three islands near the Antarctic Peninsula. Claims about penguins elsewhere need data from elsewhere.
- Trusting a pattern because it's in a big dataset. Large datasets contain many small chance patterns. Size reduces wobble but doesn't remove it, and it doesn't fix bias.
Practice
These questions are the hands-on part of the module. Try each in code before opening the answer.
- Label each statement descriptive or inferential: (a) "Of the 342 penguins, 123 are Gentoos." (b) "About a third of penguins in this region are Gentoos." (c) "The heaviest penguin in the data weighs 6,300 g."
Show the answer
(a) Descriptive. (b) Inferential: it generalises from the sample to the region. (c) Descriptive.
- Describe flipper length by species, then write one descriptive sentence about it.
Show the answer
print(penguins.groupby("species")["flipper_length_mm"].mean().round(1))
species
Adelie 190.0
Chinstrap 195.8
Gentoo 217.2
Name: flipper_length_mm, dtype: float64
"In this dataset, Gentoo penguins have the longest flippers, averaging 217.2 mm, about 27 mm longer than Adelies."
- Predict, then check with 1,000 random halves: do Chinstraps have longer flippers than Adelies in almost every half, or only in some? What would you conclude?
Show the answer
longer = 0
for _ in range(1000):
half = penguins.sample(frac=0.5, random_state=rng.integers(1_000_000))
means = half.groupby("species")["flipper_length_mm"].mean()
longer += means["Chinstrap"] > means["Adelie"]
print(longer, "of 1000 halves")
1000 of 1000 halves
The 5.8 mm difference shows up in every half, so it's very unlikely to be chance. You'd generalise it: Chinstraps tend to have longer flippers than Adelies. Unlike body mass, flipper length varies little within each species, so a smaller difference still stands out.
- A friend says: "My model got 99% accuracy on the training data, so it'll be 99% accurate on new customers." What's wrong with this, in the language of this lesson?
Show the answer
The 99% is descriptive: it describes how well the model fits the data it learned from. "It'll be 99% accurate on new customers" is an inferential claim about a population, and the training score doesn't support it, because the model may have memorised chance patterns. You need its score on unseen test data.
- In your own words, why did the 32 g Chinstrap difference fail the random-halves check when the 1,375 g Gentoo difference passed?
Show the answer
Penguin weights within each species vary by hundreds of grams, so which penguins land in a sample moves each species' average by tens of grams. A 32 g difference is smaller than that wobble, so it flips around; a 1,375 g difference is far bigger than the wobble, so it never flips.
Interview questions
Give an example of descriptive and inferential statistics on the same data.
On a sample of customers: "The average order value in our March data was ₹840" is descriptive. "Customers who saw the new checkout page spend more than those who didn't" is inferential, and needs a test or confidence interval to show the difference is larger than sampling variability.
Why isn't a model's training accuracy a good estimate of its real-world accuracy?
Training accuracy describes fit to the sample the model learned from, including noise it may have memorised. Real-world performance is an inference about the population, which must be estimated on held-out data the model hasn't seen, such as a test set or cross-validation.
Key takeaways
- Descriptive statistics summarise the data you have and carry no uncertainty.
- Inferential statistics generalise from a sample to a population, and always carry uncertainty.
- Every sample shows differences; inference asks whether a difference is bigger than the wobble chance would produce.
- Checking whether a pattern holds in many random halves of the data is a rough, intuitive test of that.
- Machine learning is inferential: a model is useful only if its patterns hold on new data.
Frequently asked questions
Which comes first, descriptive or inferential statistics?
Descriptive, always. You need to know what your data looks like, and spot problems such as missing values and odd entries, before drawing conclusions from it.
Is the random-halves check a real statistical method?
It's a simplified relative of real resampling methods such as bootstrapping and permutation tests, which you'll learn later in this course. Those give precise answers such as "the difference is 1,375 g, give or take 100 g", but they rest on the same intuition.
Can I do inference without a random sample?
You can, but your conclusions only hold for a population the sample fairly represents. With a biased sample, inference tells you about the biased group, not the population you care about.
What to learn next
- Start the next module with mean, median and mode.
- Learn how spread is measured in range, variance and standard deviation.
- See why ML holds data back for testing in train, validation and test sets.