Foundations

Discriminative Versus Generative Classifiers

Compare classifiers that model the boundary with those that model how data arises, using Bayes' rule, a worked spam case and clear trade-offs.

Key takeaways

  • Discriminative models spend their effort on the quantity needed for classification and make no claim about how the inputs are distributed.
  • Generative models describe each class's inputs and combine that with class frequencies through Bayes' rule.
  • Assumptions that are roughly right let a generative model learn from fewer examples; assumptions that are wrong can cap its accuracy.
  • A generative model can handle missing features and changed class frequencies in a principled way, provided its description of the classes holds.
  • Neither kind guarantees trustworthy probabilities, so check calibration on held-out data.

A discriminative classifier learns directly how the label depends on the input: it models p(y | x), the probability of class y given features x, or just a boundary between classes. A generative classifier learns how each class produces inputs, p(x | y), together with how common each class is, p(y), and then uses Bayes' rule to work backward to p(y | x).

Both can label the same case. They differ in what they assume, what else they can do and how they behave when data is scarce, incomplete or shifting.

Two routes to the same answer

Bayes' rule connects the two views:

p(y | x) = p(x | y) · p(y) / p(x)
p(x) = sum over all classes k of p(x | k) · p(k)

The generative route estimates the two factors in the numerator for each class. The denominator is the same for every class, so to pick a label you only compare numerators. The discriminative route skips the factorization and fits the left side directly.

Think of telling two languages apart. One approach learns enough of each language to ask which would more plausibly have produced a sentence. The other learns only the telltale differences. The second is a smaller job, but it leaves you unable to say anything about a sentence in a third language.

What each kind of classifier models
Generative Models p(x | y) and p(y) Applies Bayes' rule to classify Can score how unusual x is Leans on assumptions about inputs Discriminative Models p(y | x) or a boundary Classifies directly Says little about x itself Fewer assumptions about inputs VS Generative Models p(x | y) and p(y) Applies Bayes' rule to classify Can score how unusual x is Leans on assumptions about inputs Discriminative Models p(y | x) or a boundary Classifies directly Says little about x itself Fewer assumptions about inputs

A worked example

Suppose that in some mail stream 20 percent of messages are spam. A particular word appears in 50 percent of spam and 5 percent of legitimate mail. These numbers are invented. A message contains the word.

spam numerator:       0.50 × 0.20 = 0.100
legitimate numerator: 0.05 × 0.80 = 0.040
p(spam | word) = 0.100 / (0.100 + 0.040) ≈ 0.714

Now suppose the share of spam drops to 5 percent, while the way each kind of message is written stays the same:

spam numerator:       0.50 × 0.05 = 0.025
legitimate numerator: 0.05 × 0.95 = 0.0475
p(spam | word) = 0.025 / 0.0725 ≈ 0.345

The same evidence now points the other way. Because the generative model stores the class frequency separately, you update one number and everything follows.

A discriminative model can be adjusted too: if only class frequencies change, you can correct its predicted odds by the ratio of new to old prior odds. That relies on the same assumption, that p(x | y) is unchanged. If spammers rewrite their messages, neither shortcut applies.

Familiar pairs

Generative Discriminative counterpart What links them
Naive Bayes Logistic regression For many common feature models, both give a linear function of features in the log-odds
Linear discriminant analysis Logistic regression Gaussian classes with a shared covariance imply a linear log-odds
Hidden Markov model Linear-chain conditional random field The same chain structure, modeled jointly or conditionally

Other discriminative methods, such as support vector machines, decision trees and most neural network classifiers, have no direct generative twin in this sense.

The first pair is instructive. Naive Bayes assumes features are independent within each class and estimates each feature's distribution separately, often by simple counting. Logistic regression fits all the weights together to make the observed labels likely. With binary features the two produce decision rules of the same shape, yet they generally pick different weights.

What the independence assumption costs

Imagine two features that are really the same signal, say a word and an exact duplicate of its indicator. Naive Bayes counts the evidence twice, because it believes the two are independent. In the spam example with a 20 percent prior, the prior odds are 0.25 and the word multiplies them by 10. Counting it once gives odds of 2.5, or a probability of 0.714. Counting it twice gives odds of 25, or about 0.96.

Logistic regression, fitted to both copies, can split the weight between them and arrive at the same predictions as with one, apart from any effect of regularization.

The ranking of cases by naive Bayes may remain useful even when its probabilities are far too extreme. If you need probabilities for decisions, verify them with a reliability diagram.

Data efficiency

A generative model's assumptions act as extra information. When they are close to true, parameters can be estimated well from modest samples. A discriminative model has to extract everything about the boundary from the labels.

A well-known comparison by Andrew Ng and Michael Jordan, from around 2001, analyzed naive Bayes against logistic regression. The broad finding was that logistic regression tends to reach a lower error with plenty of data, while naive Bayes tends to approach its own, possibly worse, limit with fewer examples. So the better choice can switch as the dataset grows. It is a tendency under particular conditions, and your own learning curves are the way to find out.

What else a generative model offers

  • Missing features. In principle you can sum or integrate over an unobserved input. In naive Bayes this just means leaving that factor out. This is sound only if the reason it is missing carries no information about it or the class beyond what you observed.
  • Unusual inputs. An estimate of p(x) can flag cases unlike the training data. In high dimensions this is less reliable than it sounds, and some flexible density models assign high likelihood to inputs of a very different kind.
  • Unlabeled data. Examples without labels carry information about p(x), which a generative model can use. It can also hurt accuracy when the model's assumptions are wrong.
  • Sampling. You can draw synthetic inputs from each class, which is a good way to see what the model believes. Samples from a naive model often look absurd.
  • Adding a class. A new class can often be added by fitting its own p(x | y) and revising the priors.

What the discriminative route offers

Its main strength is freedom in the features. Because p(x) is never modeled, you can include overlapping, correlated or oddly distributed inputs without describing how they co-occur. For text, images and mixed tabular data this matters enormously, and it explains why discriminative training is the usual default when labels are plentiful.

It also spends no capacity on variation that is irrelevant to the label. A generative model trying to describe every aspect of an image may devote itself to backgrounds that do not affect the class.

The cost is silence about the inputs. Given something wildly unlike its training data, a discriminative network can still report high confidence, since softmax outputs are forced to sum to 1 across the known classes.

Mixing the two

The boundary is porous. You can train a generative model on abundant unlabeled data, then fit a small discriminative classifier on its internal representations. This is a common pattern for tasks with few labels, described further under semi-supervised learning and related methods.

You can also use a large generative language model as a classifier by prompting it to produce a label. It is generative in construction, but that use does not compute Bayes' rule over explicit class priors, so do not assume the properties listed above carry over.

Probabilities, thresholds and shift

Classification often feeds a decision, so the quality of p(y | x) matters as much as the top label. A generative model's probabilities are only as good as its description of the inputs. A discriminative model fitted with log loss targets those probabilities directly, which often helps, but a flexible one can still be overconfident.

Think about which part of the world is likely to move. If the mix of classes changes while each class looks the same, called label shift, the generative factorization shows exactly what to update. If the mix of inputs changes while the relationship p(y | x) holds, called covariate shift, a discriminative model's target is still valid, although a poorly specified or data-starved model may be wrong in the newly busy regions.

Neither assumption comes guaranteed. Collect a small labeled sample after any change you suspect and measure again.

How to choose

  1. If labeled data is plentiful and accuracy is the goal, start discriminative.
  2. If data is very limited and you can describe each class plausibly, try a generative model as well.
  3. If class frequencies will shift in known ways, make sure you can adjust the prior, whichever model you use.
  4. If features go missing at prediction time, consider whether a generative treatment or explicit missing indicators serve better.
  5. Compare on held-out data, scoring both accuracy and probability quality.

Common questions

Is "generative classifier" the same as generative AI?

They share the underlying idea of modeling how data arises. A naive Bayes spam filter is generative in this statistical sense although nobody would use it to write email. Generative AI usually refers to large models built to produce new text, images or audio.

Which one is more interpretable?

It depends on size more than on type. A naive Bayes model with a few features and a small logistic regression can both be read directly. In either case the numbers describe the model's associations and do not show what causes the outcome.

Written for 4MachineLearning with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 42 articles in Foundations →

Latest news on this topic