Lecture 5: GDA and Naive Bayes
Stanford CS229 lecture 5 notes: GDA and Naive Bayes.
- cs229
- machine-learning
- course-notes
On this page
Discriminative vs Generative#
Everything so far (linear regression, logistic regression) has been discriminative. It learns directly, or learns a mapping from to directly.
Generative learning algorithms flip it. They learn , which is "what do the features look like, given the class?", and also , the prior. Then they use Bayes' rule to classify:
where . To predict, we just pick whichever class has the higher , and we don't even need to compute .
An analogy: to tell elephants from dogs, a discriminative model learns the boundary between them. A generative model builds a model of what elephants look like and a model of what dogs look like, then checks which one a new animal looks more like.
Gaussian Discriminant Analysis (GDA)#
Assume is continuous, and that is Gaussian:
Note that both classes share the same covariance but have different means. The parameters are .
We fit them by maximizing the joint likelihood . The MLE comes out as exactly what you'd guess:
So is the fraction of positives, 's are the class averages, and is the covariance around the right class mean. No gradient descent needed, it's a closed form.
GDA and logistic regression#
If you work out under GDA, it turns out to be exactly a sigmoid of a linear function of . So GDA gives a logistic-regression-shaped boundary. But the reverse isn't true. Logistic regression doesn't assume anything about .
So which one to use?
- GDA makes stronger assumptions (the data is Gaussian). If they are correct, GDA does better, and needs less data.
- Logistic regression makes weaker assumptions, so it's more robust when you aren't sure of the distribution. E.g., if the data is Poisson, logistic regression still works fine, GDA wouldn't.
With a huge dataset, logistic regression generally wins. With a tiny dataset where you trust the assumptions, GDA can win.
Naive Bayes#
Let's build a spam classifier. Make a vocabulary of, say, 50,000 words. An email becomes a vector , where if word appears in the email.
We want , but can take values, so modelling that directly is hopeless.
The Naive Bayes assumption#
Assume that the words are conditionally independent given :
This is obviously false (the words "Stanford" and "university" are surely correlated). But it works surprisingly well in practice, and that's why it's called naive.
The parameters:
The MLE is just counting:
(of all the spam emails, what fraction contain word ?)
Laplace smoothing#
Here's the problem. Say the word "Stanford" never appeared in a training set spam email. Then . Now if a new email contains that word, , and you'd get . Also, saying something has zero probability just because you haven't seen it is a bad idea.
Fix: add 1 to the numerator and to the denominator (for a binary feature):
For a feature with possible values, add to the denominator.
Multinomial event model#
The above ignores how many times a word appears. A better version represents an email as the sequence of its words, and models each word as drawn from a multinomial given the class. It uses the same independence assumption, but captures counts. This is usually the better model for text.