Lesson 1 of 117 min
Probability
A probability is a share of all the cases; "or" adds shares when both can't happen, and "and" multiplies them when one tells you nothing about the other.
After this lesson you can assign probabilities to events and combine them.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
"Or" when both can happen
In the video, "or" added only when the two never happen together. Otherwise there's a rule: add the two chances, then take away the share that is both, since adding counted those twice. In pandas, | means "or" and & means "and", and .mean() of True and False is the share that is True.
import pandas as pd
df = pd.read_csv("penguins.csv")
gentoo = df["species"] == "Gentoo"
biscoe = df["island"] == "Biscoe"
print((gentoo | biscoe).mean())
print(gentoo.mean() + biscoe.mean() - (gentoo & biscoe).mean())0.4883720930232558 0.4883720930232558
"Not" is one minus
A thing either happens or it doesn't, and the two chances add up to one. So the chance it doesn't happen is one minus the chance it does. The job of ten steps goes right about 0.35 of the time, so it fails about 0.65 of the time.
p_job = 0.9 ** 10
print(1 - p_job)0.6513215599
The score to beat
Before you trust a model that sorts penguins, look at the biggest share. A model that always says "Adelie" is right 0.44 of the time without looking at anything. A real model has to beat that. value_counts(normalize=True) gives the shares in one step.
shares = df["species"].value_counts(normalize=True)
print(shares.idxmax(), shares.max())Adelie 0.4418604651162791
The older way you'll still see
Many tutorials still write np.random.seed and np.random.rand. They work, but NumPy's own manual says this use is discouraged. Today you make a generator with default_rng and draw from it. The two give different numbers, and both land near 0.349.
import numpy as np
np.random.seed(0)
old = np.random.rand(100_000, 10) < 0.9
rng = np.random.default_rng(0)
new = rng.random((100_000, 10)) < 0.9
print(old.all(axis=1).mean(), new.all(axis=1).mean())0.34777 0.34678
Run on Python 3.13, NumPy 2.5.5 and pandas 3.1.1. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
A penguin picked at random is a Chinstrap with a chance of 0.20. It lives on Dream island with a chance of 0.36. Every Chinstrap lives on Dream. What is the chance it's a Chinstrap and lives on Dream?
2 of 2
An AI agent does a job in five steps. Each step goes right 9 times in 10, whatever the other steps did. What is the chance the whole job goes right?
The ideas in this lesson
- Probability
- Count the cases where something happens and divide by all the cases: from 0, never, to 1, always.
- "Or" adds
- When two things can't both happen, the chance of one or the other is their two chances added.
- Independent
- When one thing tells you nothing about another, they're independent, and the chance of both is the two multiplied.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
Probability and Statistics for AI
Why does a chatbot give a different answer each time you ask? It picks every word by chance. Hi, I'm Mohi Fayden. Probability is the math of chance. Statistics uses it to learn from data. This course teaches you the parts AI uses, with pictures and code. By the end, you'll know when a better score is just luck. To start, you need school math and a little Python. Every lesson has its own page on faydenai.com, with a quiz. There's a lab every few lessons, and a project at the end.
The puzzle
Here's a puzzle. An AI agent does a job in ten steps. Each step goes right nine times out of ten. How often does the whole job go right?
Keep your guess
Each step looks safe. Keep your guess in mind. We'll work it out by counting dots.
A share of all the cases
Start with something you can count. Here are the penguins from Python for AI. There are three hundred and forty-four of them, one dot each.
Each penguin is one case. Pick one at random, like a name out of a hat.
Every penguin is as likely as the next. What's the chance it's a Gentoo? Count them.
One hundred and twenty-four, out of three hundred and forty-four. Divide, and you get the chance: about zero point three six.
A share of all the cases... is called a probability.
From zero to one
Your turn. There are sixty-eight Chinstraps. What's the chance of picking one?
Sixty-eight, divided by three hundred and forty-four. That's about zero point two.
A probability runs from zero, never, to one, always.
And the chances of the three species add up to one. That's because every penguin is one of the three, and only one.
"Or" adds
Now, what's the chance of a Gentoo or a Chinstrap? Light up both. One hundred and twenty-four, plus sixty-eight, makes one hundred and ninety-two. That's about zero point five six.
Then add their two chances. Zero point three six, plus zero point two. Zero point five six again. No penguin is both, so every dot is counted once. When two things never happen together, "or" adds.
When both can happen
So does "or" always add?
Look at the islands. One hundred and sixty-eight penguins live on Biscoe. That's about zero point four nine. What about a Gentoo, or a penguin on Biscoe?
Add the two chances, and you get zero point eight five. Something's wrong here. Can you see it?
Only one hundred and sixty-eight dots are lit. That's zero point four nine. Every Gentoo lives on Biscoe, so adding counted those penguins twice.
So "or" adds only when the two never happen together. Otherwise, count the pile once.
"And" multiplies
Now "and": two things both happening. Back to the job. Picture a thousand runs of it, one dot each. A run is one try of the whole job, from the first step to the last. Step one goes right in about nine hundred. How many of those get step two right too?
Say step two goes right nine times in ten, whatever happened in step one. So take nine in ten of those nine hundred. That's eight hundred and ten.
Or zero point nine, times zero point nine: zero point eight one.
When one thing tells you nothing about the other, they're called independent.
Then "and" multiplies.
Not independent
So does "and" always multiply?
Take a Gentoo that lives on Biscoe. Multiply the two chances: zero point three six, times zero point four nine. That's about zero point one eight.
Now look at the pile. One hundred and twenty-four out of three hundred and forty-four: zero point three six. That's twice what multiplying gave you.
Knowing it's a Gentoo tells you it lives on Biscoe. So the two aren't independent. Multiply only when they are. Otherwise, count the penguins that are both.
The job, worked out
So, the whole job. Ten steps, each nine in ten. A thousand runs. Then nine hundred. Eight hundred and ten. Seven hundred and twenty-nine. And so on, down to three hundred and forty-nine. About one job in three.
If you guessed nine in ten, you're not alone. Each step looked safe, but every step loses a few more runs. This assumes the steps are independent. Real steps can depend on each other. An early mistake can make the next steps fail too. Then count real runs.
In code
Now in real code. With pandas, count each species, then divide by how many penguins there are. You get three probabilities, and they add up to one. Then let's draw the job.
NumPy makes a grid of random numbers between zero and one. There's a row for each run, a hundred thousand rows, so the share comes out steady.
And there's a column for each of the ten steps.
Any number between zero and one is as likely as any other.
So nine times in ten, a number lands below zero point nine.
True means that step went right.
A run where all ten steps are True finished the job. Take the share of those runs. Will it print exactly zero point three four nine?
Zero point three four seven. Close, but not exact. A draw lands near the worked-out answer, not on it.
And zero point nine, multiplied by itself ten times, is zero point three four nine. Work out a chance, then draw it and check.
Three rules
So a probability is a share of all the cases.
"Or" adds, when two things never happen together.
"And" multiplies, when they're independent.
When neither is true, count the pile.
A share of all the cases
A probability is a share of all the cases. Now try it on this lesson's page, and take the quiz. Find how sure each step must be, for nine jobs in ten.
You use this lesson in Lab 1: Check an AI detector by drawing, after Random variables and distributions.
Facts last checked on .
