Skip to main content
Welcome — it’s Justyna from KodeKloud. In this lesson we’ll see how Bayes’ theorem turns new evidence into better decisions. We’ll define the theorem, explain how it updates probabilities, and walk through intuitive examples — from recognizing DJs at an expo to how your inbox detects spam.
The image features a woman speaking in front of a presentation slide with a cartoon wolf. The slide outlines three points about Bayes' Theorem, including its definition, real-world applications, and connection to machine learning.
Probability in data science is not guessing — it’s updating. Every new observation refines our belief, like adding pieces to a puzzle.
The image discusses Bayes' Theorem, highlighting its application in data science, machine learning, and AI, with a comparison of guess accuracy illustrated through bar charts.
Why Bayes matters: it gives a principled rule for combining prior knowledge with observed evidence. Common application areas include:
AreaHow Bayes is used
Weather forecastingUpdate rain probability as sensors and satellite data arrive
Security screeningCombine initial risk scores with secondary scan evidence
RecommendationsUpdate user preferences from clicks and browsing behavior
Spam filteringAdjust spam probability based on message features (words, links, sender)
HealthcareCombine symptoms and tests to estimate disease risk
Autonomous vehiclesMerge uncertain sensor readings to estimate hazard probabilities
The image features a bar graph titled "Airport Security Screening," showing "Low Risk" and "Higher Risk" categories with corresponding bar heights, alongside a woman standing on the right.
A concrete illustration: spam filtering. Email classifiers extract features (words, links, sender, formatting), compute how likely those features are under “spam” versus “not spam,” and update the spam probability. If the updated (posterior) probability exceeds a threshold, the email is flagged.
The image shows a presentation slide with the title "How Does Your Email Detect a Spam?" and a woman standing next to it. The slide includes a mock email interface highlighting a spam email titled "Crypt Lottery."
This is often implemented as a simple decision pipeline: extract evidence → compute likelihoods → update posterior → compare with threshold → act.
The image shows a flowchart explaining how systems decide if an email is spam, accompanied by a person speaking. The flowchart categorizes emails into "High Probability" or "Low Probability" of being spam, leading to actions like "Mark as Spam" or "Deliver to Inbox."
The core formula — simple, expressive, and practical:
The image presents Bayes' Theorem with its formula displayed, featuring a person explaining the concept and a cartoon character to the side.
P(A | B) = P(B | A) × P(A) / P(B)
  • P(A) — prior: what you believed about A before seeing B.
  • P(B | A) — likelihood: how probable B is when A is true.
  • P(B) — marginal: how common B is overall.
  • P(A | B) — posterior: updated belief in A after observing B.
Bayes’ theorem is a procedure: start with a prior, measure how likely the evidence is under each hypothesis, and update to get the posterior. This mindset is key for principled decision-making under uncertainty.
Example 1 — DJ vs Developer (intuitive grid) You meet someone at a tech + music expo who looks “DJ-like” (shaved head, music gear). How likely are they actually a DJ? Assumptions:
  • 90% of attendees are developers, 10% are DJs.
  • 70% of DJs behave in a way you’d call “DJ-like.”
  • 10% of developers also behave “DJ-like.”
Visualize 100 people:
  • DJs: 10 total → 70% of those act like DJs → 7 people.
  • Developers: 90 total → 10% act like DJs → 9 people.
  • Total acting like DJs: 7 + 9 = 16.
So the probability someone who looks like a DJ actually is a DJ = 7 / 16 ≈ 0.4375 (≈ 44%). Here’s the same computation expressed as Bayes’ theorem in Python-style code:
# DJ example
p_dj = 0.10
p_style_given_dj = 0.70
p_dev = 0.90
p_style_given_dev = 0.10

p_style = p_style_given_dj * p_dj + p_style_given_dev * p_dev
p_dj_given_style = (p_style_given_dj * p_dj) / p_style

p_style, p_dj_given_style  # (0.16, 0.4375)
The image shows a woman gesturing beside a pie chart, which indicates that 90% are developers and 10% are DJs.
The image shows a diagram with probabilities related to different roles and a woman standing beside it, explaining the content.
The image illustrates probabilities and conditional probabilities related to DJs and developers, possibly using Bayes' Theorem, with a character asking about finding the probability of someone acting like a DJ.
Both the 100-person grid and Bayes’ calculation give the same counterintuitive result: even strong-looking evidence can be outweighed by base rates (priors).
The image illustrates Bayes' Theorem with graphs showing how beliefs update with new evidence, and features a person speaking next to the graphs.
Example 2 — Spam filtering with the word “lottery” Suppose we scan 1,000 emails:
CategoryCountContains “lottery”
Spam1008
Not spam9002
Total100010
From this:
  • P(spam) = 100 / 1000 = 0.10
  • P(“lottery” | spam) = 8 / 100 = 0.08
  • P(“lottery”) = (8 + 2) / 1000 = 0.01
Apply Bayes: P(spam | “lottery”) = P(“lottery” | spam) × P(spam) / P(“lottery”)
= 0.08 × 0.10 / 0.01 = 0.8
So, given the observed frequencies, an email containing “lottery” has an 80% posterior probability of being spam.
The image shows the results of a model scanning 1,000 emails for the word "lottery," with a table categorizing emails as spam or not spam. A person is presenting the information next to the table.
The image shows a person speaking in front of a dark background with Bayes' Theorem and its application to a spam probability problem displayed onscreen.
Decision rule Spam filters then compare the posterior probability to a threshold (e.g., 70%). If the posterior exceeds the threshold, the message is marked as spam. This threshold encodes the operational trade-off between false positives and false negatives.
The image illustrates a decision-making rule for marking emails as spam if the likelihood exceeds a 70% threshold, with a person standing beside the explanation.
Summary
  • Bayes’ theorem converts priors and likelihoods into posteriors — a formal way to update beliefs.
  • It’s fundamental in spam filtering, medical testing, recommender systems, sensor fusion, and many ML tasks.
  • Always consider the prior (base rate): a single piece of evidence can be misleading without it.
The image shows a woman in a KodeKloud T-shirt standing next to a chart labeled "Chance of being a DJ," with the text indicating that only 10% are DJs and 10% of developers act like DJs.
Bayes’ theorem is more than a formula — it’s a disciplined way to combine past knowledge and fresh observations to make better decisions under uncertainty. Links and references

Watch Video