Every classifier so far has looked directly for a boundary. There is a second way to build a classifier, one that tells a story about where each class's data comes from, much as Gaussian mixtures did for clustering. The two approaches have names, and the difference between them runs through the rest of the course.
Two ways to model a classification problem
Think of the data as pairs drawn from some joint distribution . That distribution can be factorised in two ways:
- A discriminative model learns directly, or just a boundary that separates the classes. It does not care how the features themselves are distributed. kNN, decision trees, logistic regression and support vector machines are discriminative.
- A generative model learns , what typical data from each class looks like, and the class proportions . It can then classify by Bayes' rule:
predicting the class with the larger posterior. Because it models the features too, a generative model could in principle generate new examples of each class, hence the name.
An analogy: to tell Hindi text from Tamil text, a discriminative learner looks for whatever cues separate them (the script, a few common words). A generative learner learns to write a little of each language, then asks which language's model would more plausibly have produced the text in front of it.
A generative story for spam
Represent an email by which words it contains. Fix a vocabulary of words; the feature vector has if word appears. The label is 1 for spam and 0 for genuine mail.
The generative story:
- Choose the class. Spam with probability , so .
- Generate the words given the class, from a distribution specific to that class: spam is more likely to contain "winner" and "lottery", genuine mail "meeting" and "invoice".
The parameter is easy to estimate: the fraction of training emails that are spam (its maximum likelihood estimate, as for the coin in Module 5).
The problem: too many parameters
What about ? A binary vector of length has possible values. Describing an arbitrary distribution over all of them needs probabilities per class.
With a vocabulary of just 30 words, that is over a billion parameters per class. With a realistic vocabulary of tens of thousands of words, the number is beyond astronomical, and no amount of email could estimate it: almost every possible combination of words would appear zero times in the training data, so its estimated probability would be zero. A fully general generative model is hopeless, not because the idea is wrong but because it has far too many parameters for any finite data.
The way out: an assumption
The only escape is to assume structure in that cuts the number of parameters down. The boldest useful assumption is that within each class, the features are independent of one another:
Each factor is a coin flip: the probability that word appears in an email of class . That is one parameter per word per class, in all (plus ), instead of . With 30 words, 61 numbers instead of about two billion.
The assumption is clearly false in detail: "lottery" and "winner" tend to appear together, and so do "meeting" and "agenda". It is called naive for that reason. Yet the resulting classifier, naive Bayes, works remarkably well for text and many other problems, because classification only needs to get the larger posterior right, not the exact probabilities. The next chapter builds it in full.
Which approach is better?
Neither always wins.
- Generative models use more assumptions. When those assumptions are roughly right they need less data and train very fast (often just by counting). They handle missing features naturally (just leave the factor out) and can detect inputs that look like neither class.
- Discriminative models spend all their effort on the boundary, which is what classification needs. With enough data they usually achieve lower error, because they are not hurt by a wrong model of .
A classic study (Ng and Jordan, 2002) compared naive Bayes with its discriminative counterpart, logistic regression: naive Bayes reaches its best error with fewer examples, but logistic regression usually reaches a lower error once there is plenty of data.