vrishank

← Blog

Lecture 5: GDA and Naive Bayes

Stanford CS229 lecture 5 notes: GDA and Naive Bayes.

· 3 min read

  • cs229
  • machine-learning
  • course-notes
On this page

Discriminative vs Generative#

Everything so far (linear regression, logistic regression) has been discriminative. It learns p(y∣x)p(y \mid x) directly, or learns a mapping from xx to yy directly.

Generative learning algorithms flip it. They learn p(x∣y)p(x \mid y), which is "what do the features look like, given the class?", and also p(y)p(y), the prior. Then they use Bayes' rule to classify:

p(y∣x)=p(x∣y)p(y)p(x)p(y \mid x) = \frac{p(x \mid y) p(y)}{p(x)}

where p(x)=p(x∣y=1)p(y=1)+p(x∣y=0)p(y=0)p(x) = p(x \mid y=1)p(y=1) + p(x \mid y=0)p(y=0). To predict, we just pick whichever class has the higher p(x∣y)p(y)p(x \mid y)p(y), and we don't even need to compute p(x)p(x).

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 x∈Rnx \in \mathbb{R}^n is continuous, and that p(x∣y)p(x \mid y) is Gaussian:

y∼Bernoulli(ϕ)y \sim \text{Bernoulli}(\phi)
x∣y=0∼N(μ0,Σ)x \mid y=0 \sim \mathcal{N}(\mu_0, \Sigma)
x∣y=1∼N(μ1,Σ)x \mid y=1 \sim \mathcal{N}(\mu_1, \Sigma)

Note that both classes share the same covariance Σ\Sigma but have different means. The parameters are ϕ,μ0,μ1,Σ\phi, \mu_0, \mu_1, \Sigma.

We fit them by maximizing the joint likelihood ∏ip(x(i),y(i))\prod_i p(x^{(i)}, y^{(i)}). The MLE comes out as exactly what you'd guess:

ϕ=1m∑i=1m1{y(i)=1}\phi = \frac{1}{m}\sum_{i=1}^{m} 1\{y^{(i)} = 1\}
μ0=∑i1{y(i)=0}x(i)∑i1{y(i)=0},μ1=∑i1{y(i)=1}x(i)∑i1{y(i)=1}\mu_0 = \frac{\sum_i 1\{y^{(i)}=0\} x^{(i)}}{\sum_i 1\{y^{(i)}=0\}}, \quad \mu_1 = \frac{\sum_i 1\{y^{(i)}=1\} x^{(i)}}{\sum_i 1\{y^{(i)}=1\}}
Σ=1m∑i=1m(x(i)−μy(i))(x(i)−μy(i))T\Sigma = \frac{1}{m}\sum_{i=1}^{m} (x^{(i)} - \mu_{y^{(i)}})(x^{(i)} - \mu_{y^{(i)}})^T

So ϕ\phi is the fraction of positives, μ\mu's are the class averages, and Σ\Sigma 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 p(y=1∣x)p(y=1 \mid x) under GDA, it turns out to be exactly a sigmoid of a linear function of xx. So GDA gives a logistic-regression-shaped boundary. But the reverse isn't true. Logistic regression doesn't assume anything about p(x∣y)p(x \mid y).

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 x∈{0,1}50000x \in \{0,1\}^{50000}, where xj=1x_j = 1 if word jj appears in the email.

We want p(x∣y)p(x \mid y), but xx can take 2500002^{50000} values, so modelling that directly is hopeless.

The Naive Bayes assumption#

Assume that the words are conditionally independent given yy:

p(x1,...,xn∣y)=∏j=1np(xj∣y)p(x_1, ..., x_n \mid y) = \prod_{j=1}^{n} p(x_j \mid y)

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:

  • ϕj∣y=1=p(xj=1∣y=1)\phi_{j|y=1} = p(x_j = 1 \mid y=1)
  • ϕj∣y=0=p(xj=1∣y=0)\phi_{j|y=0} = p(x_j = 1 \mid y=0)
  • ϕy=p(y=1)\phi_y = p(y=1)

The MLE is just counting:

ϕj∣y=1=∑i1{xj(i)=1,y(i)=1}∑i1{y(i)=1}\phi_{j|y=1} = \frac{\sum_i 1\{x_j^{(i)}=1, y^{(i)}=1\}}{\sum_i 1\{y^{(i)}=1\}}

(of all the spam emails, what fraction contain word jj?)

Laplace smoothing#

Here's the problem. Say the word "Stanford" never appeared in a training set spam email. Then ϕj∣y=1=0\phi_{j|y=1} = 0. Now if a new email contains that word, p(x∣y=1)=0p(x \mid y=1) = 0, and you'd get 00\frac{0}{0}. Also, saying something has zero probability just because you haven't seen it is a bad idea.

Fix: add 1 to the numerator and 22 to the denominator (for a binary feature):

ϕj∣y=1=∑i1{xj(i)=1,y(i)=1}+1∑i1{y(i)=1}+2\phi_{j|y=1} = \frac{\sum_i 1\{x_j^{(i)}=1, y^{(i)}=1\} + 1}{\sum_i 1\{y^{(i)}=1\} + 2}

For a feature with kk possible values, add kk 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.