AI Hints
Statistics & Math for ML · Lesson 30 of 138

Conditional Probability

Given that.

⏱ 2 min read▶ Video at 33:44🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

What's the chance a customer buys something, given that they opened our email? That word, given, means we only look at part of the data. That's conditional probability.

Conditional Probability
From the video: Conditional Probability
  • Buys, given they opened the email
  • Given narrows the data
  • Conditional probability

Counting within a group

Here are a thousand customers, with whether they opened the email, and whether they bought. To find the chance of buying given opened, we keep only the people who opened, and see what share of them bought. Run it. Overall, nine percent bought. But among openers, it was twenty five percent.

conditional.pypython
import pandas as pd

df = pd.DataFrame({
    "opened": [True] * 200 + [False] * 800,
    "bought": [True] * 50 + [False] * 150 + [True] * 40 + [False] * 760,
})

print("P(bought):         ", df["bought"].mean())
print("P(bought | opened):", df[df["opened"]]["bought"].mean())
Output
P(bought):          0.09
P(bought | opened): 0.25

The formula

The probability of A given B is the probability of both, divided by the probability of B. In words, out of all the times B happens, how often does A happen too? Classifiers in machine learning predict exactly this. The probability of a label, given the features.

keyvalue
P(A | B)P(A and B) ÷ P(B)
In wordsof the B cases, how many are A?
NoteP(A | B) ≠ P(B | A)
ClassifiersP(label | features)

Summary

Conditional probability is the chance of A, given that B happened. Just look inside the B group. Next, Bayes' theorem, which flips it around.

Key takeaways
  • Given = filter first
  • P(A|B) = P(A and B) / P(B)
  • Models predict P(label | features)
Next up: Bayes' Theorem — updating beliefs