🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #44

Conditional Probability

Conditional probability answers "given that B happened, how likely is A?" — it's the difference between an event's probability in general, and its probability once you already have some relevant information.

Formula

\[ P(A \mid B) = \frac{P(A \cap B)}{P(B)} \]

\(P(A \mid B)\) is read "probability of A given B." \(P(A \cap B)\) is the probability both A and B happen together. \(P(B)\) is the probability of B on its own — this formula effectively "zooms in" on only the cases where B is true, then asks what fraction of those also have A.

Visual Intuition

A B A∩B

P(A|B) asks: of everything inside circle B, what fraction also falls inside circle A?

Numerical Example

Out of 100 emails: 30 contain the word "free" (event B), and of those, 24 are spam (event A∩B). Also, 40 emails total are spam.

\[ P(\text{spam} \mid \text{"free"}) = \frac{P(\text{spam} \cap \text{"free"})}{P(\text{"free"})} = \frac{24/100}{30/100} = \frac{24}{30} = 0.8 \]

Notice this (0.8) is very different from the overall \(P(\text{spam}) = 40/100 = 0.4\) — seeing the word "free" substantially raises the probability of spam. This gap between the conditional and unconditional probability is exactly the signal a classifier like Naive Bayes learns to exploit.

total_emails = 100
contains_free = 30
spam_and_free = 24

p_spam_given_free = spam_and_free / contains_free
print(p_spam_given_free)   # 0.8

Independence — The Special Case Where Conditioning Changes Nothing

\[ \text{A and B are independent if and only if } P(A \mid B) = P(A) \]

If knowing B happened doesn't change the probability of A at all, they're independent — this is the assumption Naive Bayes makes between features (hence "naive"), even though it's rarely exactly true in real data.

Common Mistakes

  • Confusing \(P(A\mid B)\) with \(P(B\mid A)\) — these are generally different numbers (this is the same mix-up covered in Bayes' Theorem).
  • Assuming conditioning always changes the probability — for genuinely independent events, it doesn't, by definition.

Interview Relevance

Q: "How is conditional probability different from joint probability?" Joint probability \(P(A \cap B)\) is the chance both happen out of all outcomes; conditional probability \(P(A\mid B)\) restricts the "universe" to only outcomes where B already happened, then asks what fraction of those also have A.

Practice Question

Out of 200 loan applicants, 50 have a credit score below 600, and of those, 30 defaulted. What is \(P(\text{default} \mid \text{credit score} < 600)\)?

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Conditional Probability – FAQs

Quick answers about learning Conditional Probability in Machine Learning.

This free note from CodingNow 2.0 explains Conditional Probability in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Conditional Probability, is 100% free with no signup required.
With focused practice, most students grasp Conditional Probability in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now