Probability
Last updated
A classifier doesn’t just say “Gentoo”; it says “Gentoo, with probability 0.92”. To read that number, and to build models that produce it, you need the language of probability: events, joint and conditional probability, independence, and Bayes’ rule, which turns one conditional probability round into the other. This lesson works all of them out by counting real penguins, then meets the most famous trap in the subject.
By the end of this lesson you can
- Work out probabilities as fractions of a count, with pandas
- Tell joint probability from conditional probability, and from
- Check whether two events are independent
- Use Bayes’ rule, and explain why a rare condition makes most positive tests wrong
Events and probability
An event is something that's either true or false for each case: "this penguin is a Gentoo", "this penguin lives on Biscoe". Its probability, written , is a number from 0 (never) to 1 (always). When every case is equally likely to be picked, it's just a fraction:
In pandas an event is a Boolean mask (lesson 0.9), and the mean of a mask is the fraction of Trues, because True counts as 1 and False as 0. So mask.mean() is a probability. Here are the 344 penguins:
Pick a penguin at random and , .
Joint and conditional
Two kinds of "and" and "given" matter.
The joint probability is the chance that both are true: a Gentoo and from Biscoe. That's (gentoo & biscoe).mean(), 0.36 above. (Every Gentoo in the data lives on Biscoe, so it equals .)
The conditional probability , said "p of A given B", is the chance of when you already know is true. You only look at the cases where holds:
In words: of the cases where is true, the fraction where is true as well. In pandas: (a & b).mean() / b.mean().
Notice the two are very different. : about three in four Biscoe penguins are Gentoos. : every Gentoo is from Biscoe. Swapping the two sides of the bar changes the question, and usually the answer.
A classifier's output is a conditional probability: . Stage 3's logistic regression is built to produce exactly that.
Independence
Two events are independent when knowing one tells you nothing about the other. Then the joint probability is just the product:
In words: if they don't affect each other, the chance of both is one chance times the other. Equivalently, : knowing doesn't change the odds of .
Species and island are clearly not independent:
If they were, would be . It's 0.36, twice as much. Independence is a big assumption that models often make on purpose to keep things simple; the naive Bayes classifier (Stage 4) assumes every feature is independent of the others, given the class.
Bayes’ rule: a medical test
Bayes' rule turns a conditional probability round: from to .
In words: the chance of given is the chance of given , times how common was to begin with, divided by how common is overall. It comes straight from step 2's formula written both ways round.
The classic use is a medical test. You know:
- the prevalence : how common the condition is;
- the sensitivity : how often the test catches someone who has it;
- the specificity : how often it clears someone who doesn't.
You want : the chance you're sick, given a positive test. The bottom of the fraction, , counts every way to test positive: sick and caught, or well and wrongly flagged. The box below counts it for 1,000 people:
60 people test positive: 10 who have the condition and 50 who don’t. So if you test positive, the chance you have it is 10 ÷ 60 = 16.7%. The false positives outnumber the true ones, because there are so many more well people to make mistakes on.
The same calculation in code, for a condition 1 in 1,000 people have and a test that's 99% right both ways:
Wrong way: 99% accurate, 9% likely
A test that's right 99% of the time comes back positive. The tempting answer is "99% chance I'm sick". The notebook says 9%. Set the box to 1 in 1,000, with sensitivity and specificity both 99%, and count: about 1 sick person tests positive, but about 10 of the 999 well people do too. So only 1 positive in 11 is real.
The mistake is mixing up , which is 0.99, with , which is what you want: swapping the sides of the bar from step 2. It's called the base-rate mistake, because it ignores how rare the condition is (its base rate). When the condition is common, 1 in 10, the same test gives 0.9167 (second cell in step 4).
If the formula feels slippery, count. Simulate a million people and look:
0.0894, close to the formula's 0.0902 (a random sample never lands exactly). The same mistake turns up in machine learning as class imbalance (lesson 2.8): a fraud detector that's "99% accurate" on data where 1 payment in 1,000 is fraud can still be wrong about most of the payments it flags.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 3: Probability and Information Theory: conditional probability (3.5), independence (3.7) and Bayes’ rule (3.11)
- Horst, Hill and Gorman, palmerpenguins: Palmer Archipelago (Antarctica) penguin data, 2020: the penguins dataset, released under CC0
- NumPy: Random Generator: default_rng and random, used to simulate a million people