Skip to main content
LearnMLAcademy
Advanced10 min read

Generative vs Discriminative Models

Compare generative versus discriminative models with joint and conditional math, with diffusion diagrams, prompt experiments, and output-quality checks.

What you will master in this lesson
Intuitive core mechanisms & practical real-world intuition
Step-by-step mathematical breakdown & visual diagrams
Runnable Python implementation & practical trade-offs

Two useful goals, not two competing buzzwords

A discriminative model learns the rule needed to predict a target from an observed input. A generative model learns enough about how data occurs to represent or create possible samples. The best choice depends on the job: deciding, estimating, creating, filling missing information, or combining several of those tasks.

Discriminative does not mean “classification only.” A discriminative regression model can map a house description to a continuous price. Generative does not mean “images only.” A generator may produce text, audio, rows, sequences, or other data.

Generative vs Discriminative in Simple Words

Think of a detective vs an artist: A discriminative model acts like a detective—given a handwritten note (x), it determines whether it belongs to Suspect A or Suspect B ($P(y|x)$). A generative model acts like a master artist—it studies Suspect A’s handwriting style so thoroughly that it can forge brand-new sentences in that exact style ($P(x|y)$)!

Discriminative: P(y | x)

Learns the decision boundary between classes. High predictive accuracy, but cannot sample new data points.

Generative: P(x, y) = P(x | y)P(y)

Learns the full joint probability distribution. Can synthesize realistic samples, handle missing features, and detect out-of-distribution inputs.

Read the probability notation one piece at a time

x — the input or observed data

Example: whether an email contains the word “offer,” plus its other measured features.

y — the target

Example: spam or legitimate. For regression, y could instead be a number such as price.

p(y | x) — target given input

After observing x, how likely is each y? This is the usual discriminative direction.

p(x) and p(x, y) — data and joint patterns

p(x) describes inputs. p(x, y) describes inputs and targets occurring together.

p(x | y) — input given a target

Within a chosen class y, how likely is an input pattern x? A class-conditional generator can use this direction to create an example belonging to y.

Figure 1 — Two learning pathways through the same two classes

Circles and squares mean the same classes on both sides. The discriminative path learns the boundary needed to predict y from x; the generative path learns where each class tends to occur, which supports sampling a new x or comparing p(x | y) values for classification.

A paired diagram uses blue circles and amber squares for the same two classes. The discriminative side draws a visible decision boundary and continues to p of y given x and a predicted class. The generative side draws two class distributions, samples a new x, and also shows that an observed x can be classified by comparing p of x given each y.

DISCRIMINATIVE · LEARN TO SEPARATE

GENERATIVE · LEARN HOW DATA OCCURS

Discriminative mapping
p(y | x)

Read this as “the probability of target y after input x is known.” A spam detector can estimate this quantity directly without modelling every possible way an email might be written.

One generative route to classification
p(y | x) = p(x | y) × p(y) / p(x)

p(y) is the prior frequency of a class. p(x | y) describes the input inside that class. p(x) makes the probabilities normalize correctly. A generative classifier can learn these quantities and then calculate p(y | x). Modern generators may instead learn an implicit sampling process and never expose a tractable numerical likelihood.

Worked example: the word “offer” in 100 emails

The following counts are observed data, not model outputs. Let x mean “the email contains offer,” and let y mean “the email is spam.”

Observed classContains “offer”Does not contain “offer”Row total
Spam301040
Legitimate65460
Column total3664100

Figure 2 — Joint, marginal, and conditional probabilities in the same count grid

A joint probability uses one cell, a marginal probability totals across a row or column, and a conditional probability renormalizes only inside the chosen condition. The values match the 100-email worked example below.

A two by two count grid has spam rows of 30 offer and 10 no offer, legitimate rows of 6 offer and 54 no offer, row totals 40 and 60, column totals 36 and 64, and grand total 100. The joint spam and offer cell is 30. The offer marginal is 36, the spam marginal is 40, and within the spam row p of offer given spam is 30 divided by 40 or 0.75.

1. p(x) = 36 / 100 = 0.36

Thirty spam plus six legitimate emails contain “offer,” so 36 of 100 observed inputs have x.

2. p(y) = 40 / 100 = 0.40

Forty of the 100 emails are spam, so the prior probability of spam is 0.40.

3. p(x, y) = 30 / 100 = 0.30

Thirty emails are both spam and contain “offer.” This is a joint probability.

4. p(x | y) = 30 / 40 = 0.75

Among the 40 spam emails, 30 contain “offer.” This describes x inside the spam class.

5. p(y | x) = 30 / 36 = 0.833

Among the 36 emails containing “offer,” 30 are spam. The discriminative prediction is therefore about 83.3% spam for this one-feature example.

A discriminative model may learn the final 83.3% mapping directly. A simple generative classifier may learn class frequencies and within-class word patterns, then reach the same conditional result using Bayes’ rule. A content generator would need far richer patterns than this one-word table.

Reproduce every probability

This self-contained Python calculation uses only the four counts shown in the table. Each division matches one numbered step above.

Python
total = 100
spam = 40
offer = 36
spam_and_offer = 30

p_x = offer / total
p_y = spam / total
p_x_and_y = spam_and_offer / total
p_x_given_y = spam_and_offer / spam
p_y_given_x = spam_and_offer / offer

print(p_x, p_y, p_x_and_y, p_x_given_y, p_y_given_x)
# 0.36 0.40 0.30 0.75 0.8333333333333334

Discriminative Boundary vs Generative Distribution Contours

Discriminative draws one dividing line; Generative models the complete density cloud for each class.

Discriminative Boundary: P(y=A|x) = 0.5
p(x|y=A) cluster densityp(x|y=B) cluster density
Discriminative: Finds the dividing hyperplane separating classes. Ignores how points inside each class cluster or where outliers lie.
Generative: Fits full probability distributions $P(x|y)$ and priors $P(y)$. Can sample new instances from either class and detect anomalous outliers!

Comparison and decision guide

QuestionDiscriminative modellingGenerative modelling
Primary jobPredict y from observed xRepresent or sample possible x values, optionally conditioned on y or other information
Typical outputClass, probability, score, or continuous valueA new sample, reconstruction, completion, or sometimes a class prediction
When it is enoughThe product only needs an accurate decision or numeric estimateNot necessary merely because the data is complex
What it addsOften simpler and more directly optimized for predictionCreation, simulation, missing-data modelling, representation learning, or multiple downstream uses
Important caveatMay not explain how inputs themselves occurCan be harder to train and evaluate; not every model has an explicit likelihood

Summary / Key Takeaways

  • A discriminative model directly learns the prediction direction p(y | x): the target after the input is known.
  • A generative model represents how data occurs through quantities such as p(x), p(x, y), or p(x | y), or through an implicit sampling process.
  • Discriminative models can output continuous values as well as classes, and generative models can also support classification.
  • In the email example, 30 of the 36 messages containing ‘offer’ are spam, so p(spam | offer) = 30 / 36 ≈ 0.833.
  • Use discriminative modelling when the real requirement is an accurate label, score, or numerical estimate.
  • Choose generative modelling when creation, simulation, missing-data modelling, representation learning, or sampling the data process adds useful value.

Apply this concept in a real hands-on project

You have studied the idea. Now use it in working Python code, see the calculation and output, and explain what changed. Each project includes step-by-step instructions, full source and tests.

Practice challenge: predict an outcome first, run the code, and explain the difference.

Track Your Progress0% curriculum done

Finished "Generative vs Discriminative Models"?

Mark this lesson as completed to update your progress bar and track your milestones across the full curriculum.

Overall Progress0 / 167 lessons

Was this tutorial helpful?

Your feedback helps us continuously refine and improve our open-access tutorials.

Knowledge Check

Progress: 0/5

Test your active recall. Choose one answer for each question, then check it to reveal the step-by-step reasoning.

Question 1 of 5conceptual
Which probability direction is the usual discriminative goal?
Question 2 of 5numerical/formula
In the lesson's email table, 30 of the 36 messages containing ‘offer’ are spam. What is p(spam | offer)?
Question 3 of 5interpretation
Which statement correctly interprets the overlap between generative and discriminative models?
Question 4 of 5practical selection
A product only needs an accurate house-price estimate from observed features. Which model family is the simpler starting point?
Question 5 of 5application/scenario
Which requirement most strongly justifies a generative approach?

Knowledge Check Review & Explanations

Review the questions, verified correct answers, and conceptual explanations for this lesson.

1Which probability direction is the usual discriminative goal?▼

Correct Answer: p(y | x)

A discriminative model directly estimates the target y after observing input x, written p(y | x).

2In the lesson's email table, 30 of the 36 messages containing ‘offer’ are spam. What is p(spam | offer)?▼

Correct Answer: 30 / 36 ≈ 0.833

Conditioning on ‘offer’ restricts the denominator to the 36 messages containing that word. Of those, 30 are spam, giving about 83.3%.

3Which statement correctly interprets the overlap between generative and discriminative models?▼

Correct Answer: Either family can support classification, but they learn or calculate the result in different ways

A discriminative classifier can learn p(y | x) directly, while a generative classifier can model data and class quantities and then derive p(y | x).

4A product only needs an accurate house-price estimate from observed features. Which model family is the simpler starting point?▼

Correct Answer: A discriminative regression model

When the real job is a direct numeric prediction, discriminative modelling targets that mapping without requiring a model of how all possible inputs occur.

5Which requirement most strongly justifies a generative approach?▼

Correct Answer: Create varied samples and model missing-data patterns for several downstream uses

Creation, simulation, missing-data modelling, representation learning, and multiple downstream uses are reasons to model or sample the data-producing process.

Keep moving through the path

Continue Learning

Free Download: ML Interview Cheatsheet

100 essential Machine Learning interview questions and answers covering foundations, algorithms, evaluation, deep learning, Python, system design and interview strategy. Enter your email to download it instantly.

Next