Lesson 3.7core2 h 30 min

Naive Bayes and generative vs discriminative models

Classify by modelling how the data were made

There are two ways to build a classifier: model the boundary between classes directly, or model how each class generates its data and invert with Bayes' rule. Naive Bayes is the simplest generative classifier — a few lines of counting that still competes on text — and the generative/discriminative distinction it introduces is the one behind the phrase "generative AI": a language model is a generative model of text (6.2 Classical NLP and n-gram language models, 7.6 Decoder-only: GPT and autoregressive generation).

You'll be able to

  • State the Bayes classifier and explain why the evidence term can be ignored
  • Explain the conditional-independence assumption, its parameter savings and why the argmax often survives its violation
  • Fit Bernoulli, multinomial and Gaussian Naive Bayes by maximum likelihood with Laplace smoothing
  • Compute class posteriors in log space and run a complete worked spam example
  • Compare generative and discriminative models on sample efficiency, asymptotic accuracy and capabilities
  • Connect Naive Bayes to n-gram language models and to what 'generative' means in generative AI

Key ideas

  1. Generative classifiers model p(x | y) and p(y) and apply Bayes' rule; discriminative ones model p(y | x) directly. The evidence p(x) cancels in the argmax.
  2. Naive Bayes assumes features are conditionally independent given the class, cutting parameters from exponential to linear in d; the ranking often survives the false assumption even when probabilities are over-confident.
  3. Multinomial NB for counts, Bernoulli for presence/absence, Gaussian for continuous features; all fit by counting (MLE).
Show 5 more key ideas
  1. Laplace smoothing θ̂ = (N_cj + α)/(N_c + αV) prevents a single unseen word from zeroing a class; compute in log space to avoid underflow.
  2. Log-score = log π_c + Σ x_j log θ̂_cj is linear in the features: Naive Bayes is a linear classifier whose weights are set by counting.
  3. Generative models need O(log d) examples to reach their (higher) asymptotic error, discriminative O(d) to reach a lower one — NB wins small data, logistic regression wins big data.
  4. Generative models can marginalize missing features, generate samples and score outliers; a language model is a generative model of text.
  5. Naive Bayes is the baseline a fancier text model must beat.

Suggested path through this lesson