Conditional Probability
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 17 of 64.
Half of these 10 emails are spam. But once you know an email has a link, only the linked ones matter:
The share inside that group is the conditional probability of spam given a link, P(spam | link). Count both, then divide by the condition:
emails = pd.DataFrame({
"link": ["yes", "no", "yes", "no", "no", "no", "yes", "no", "no", "yes"],
"spam": ["yes", "no", "yes", "no", "no", "yes", "no", "no", "yes", "yes"],
})
spam = emails["spam"] == "yes"
link = emails["link"] == "yes"
print(spam.mean())
print((spam & link).sum() / link.sum())This prints:
0.5
0.75The condition you already know goes below the line: you divide by it. A spam filter works this way: the chance of spam given an email's words.
Challenge
MediumA market logged 50 days in mornings.csv: the morning sky (clear, cloudy or foggy) and rain, yes if it rained later. Which sky warns best?
- For each sky in
skies, print the chance of rain given that sky. - Print the chance of rain on any day.
- Print the best clue: the sky with the top chance.
For a shorter log, to 2 decimals:
Rain after a clear morning: 0.25
Rain after a cloudy morning: 0.33
Rain after a foggy morning: 0.75
Rain on any day: 0.45
Best clue for rain: foggyTry it yourself
import pandas as pd
days = pd.read_csv("mornings.csv")
skies = ["clear", "cloudy", "foggy"]
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler