SlideShare a Scribd company logo
CS229 Lecture notes 
Andrew Ng 
Part IV 
Generative Learning algorithms 
So far, we’ve mainly been talking about learning algorithms that model 
p(y|x; θ), the conditional distribution of y given x. For instance, logistic 
regression modeled p(y|x; θ) as h(x) = g(θT x) where g is the sigmoid func- 
tion. In these notes, we’ll talk about a different type of learning algorithm. 
Consider a classification problem in which we want to learn to distinguish 
between elephants (y = 1) and dogs (y = 0), based on some features of 
an animal. Given a training set, an algorithm like logistic regression or 
the perceptron algorithm (basically) tries to find a straight line—that is, a 
decision boundary—that separates the elephants and dogs. Then, to classify 
a new animal as either an elephant or a dog, it checks on which side of the 
decision boundary it falls, and makes its prediction accordingly. 
Here’s a different approach. First, looking at elephants, we can build a 
model of what elephants look like. Then, looking at dogs, we can build a 
separate model of what dogs look like. Finally, to classify a new animal, we 
can match the new animal against the elephant model, and match it against 
the dog model, to see whether the new animal looks more like the elephants 
or more like the dogs we had seen in the training set. 
Algorithms that try to learn p(y|x) directly (such as logistic regression), 
or algorithms that try to learn mappings directly from the space of inputs X 
to the labels {0, 1}, (such as the perceptron algorithm) are called discrim- 
inative learning algorithms. Here, we’ll talk about algorithms that instead 
try to model p(x|y) (and p(y)). These algorithms are called generative 
learning algorithms. For instance, if y indicates whether an example is a 
dog (0) or an elephant (1), then p(x|y = 0) models the distribution of dogs’ 
features, and p(x|y = 1) models the distribution of elephants’ features. 
After modeling p(y) (called the class priors) and p(x|y), our algorithm 
1
2 
can then use Bayes rule to derive the posterior distribution on y given x: 
p(y|x) = 
p(x|y)p(y) 
p(x) 
. 
Here, the denominator is given by p(x) = p(x|y = 1)p(y = 1) + p(x|y = 
0)p(y = 0) (you should be able to verify that this is true from the standard 
properties of probabilities), and thus can also be expressed in terms of the 
quantities p(x|y) and p(y) that we’ve learned. Actually, if were calculating 
p(y|x) in order to make a prediction, then we don’t actually need to calculate 
the denominator, since 
argmax 
y 
p(y|x) = argmax 
y 
p(x|y)p(y) 
p(x) 
= argmax 
y 
p(x|y)p(y). 
1 Gaussian discriminant analysis 
The first generative learning algorithm that we’ll look at is Gaussian discrim- 
inant analysis (GDA). In this model, we’ll assume that p(x|y) is distributed 
according to a multivariate normal distribution. Let’s talk briefly about the 
properties of multivariate normal distributions before moving on to the GDA 
model itself. 
1.1 The multivariate normal distribution 
The multivariate normal distribution in n-dimensions, also called the multi- 
variate Gaussian distribution, is parameterized by a mean vector μ ∈ Rn 
and a covariance matrix  ∈ Rn×n, where  ≥ 0 is symmetric and positive 
semi-definite. Also written “N(μ,)”, its density is given by: 
p(x; μ,) = 
1 
(2π)n/2||1/2 exp 
 
− 
1 
2 
(x − μ)T−1(x − μ) 
 
. 
In the equation above, “||” denotes the determinant of the matrix . 
For a random variable X distributed N(μ,), the mean is (unsurpris- 
ingly) given by μ: 
E[X] = 
Z 
x 
x p(x; μ,)dx = μ 
The covariance of a vector-valued random variable Z is defined as Cov(Z) = 
E[(Z − E[Z])(Z − E[Z])T ]. This generalizes the notion of the variance of a
3 
real-valued random variable. The covariance can also be defined as Cov(Z) = 
E[ZZT ]−(E[Z])(E[Z])T . (You should be able to prove to yourself that these 
two definitions are equivalent.) If X ∼ N(μ,), then 
Cov(X) = . 
Here’re some examples of what the density of a Gaussian distribution 
looks like: 
−3 
−2 
−1 
0 
1 
2 
3 
−3 
−2 
−1 
0 
1 
2 
3 
0.25 
0.2 
0.15 
0.1 
0.05 
−3 
−2 
−1 
0 
1 
2 
3 
−3 
−2 
−1 
0 
1 
2 
3 
0.25 
0.2 
0.15 
0.1 
0.05 
−3 
−2 
−1 
0 
1 
2 
3 
−3 
−2 
−1 
0 
1 
2 
3 
0.25 
0.2 
0.15 
0.1 
0.05 
The left-most figure shows a Gaussian with mean zero (that is, the 2x1 
zero-vector) and covariance matrix  = I (the 2x2 identity matrix). A Gaus- 
sian with zero mean and identity covariance is also called the standard nor- 
mal distribution. The middle figure shows the density of a Gaussian with 
zero mean and  = 0.6I; and in the rightmost figure shows one with ,  = 2I. 
We see that as  becomes larger, the Gaussian becomes more “spread-out,” 
and as it becomes smaller, the distribution becomes more “compressed.” 
Let’s look at some more examples. 
−3 
−2 
−1 
0 
1 
2 
3 
−3 
−2 
−1 
0 
1 
2 
0.25 
0.2 
0.15 
0.1 
0.05 
3 
−3 
−2 
−1 
0 
1 
2 
3 
−3 
−2 
−1 
0 
1 
2 
0.25 
0.2 
0.15 
0.1 
0.05 
3 
−3 
−2 
−1 
0 
1 
2 
3 
−3 
−2 
−1 
0 
1 
2 
0.25 
0.2 
0.15 
0.1 
0.05 
3 
The figures above show Gaussians with mean 0, and with covariance 
matrices respectively 
 = 
 
1 0 
0 1 
 
;  = 
 
1 0.5 
0.5 1 
 
; . = 
 
1 0.8 
0.8 1 
 
. 
The leftmost figure shows the familiar standard normal distribution, and we 
see that as we increase the off-diagonal entry in , the density becomes more 
“compressed” towards the 45◦ line (given by x1 = x2). We can see this more 
clearly when we look at the contours of the same three densities:
4 
−3 −2 −1 0 1 2 3 
3 
2 
1 
0 
−1 
−2 
−3 
−3 −2 −1 0 1 2 3 
3 
2 
1 
0 
−1 
−2 
−3 
−3 −2 −1 0 1 2 3 
3 
2 
1 
0 
−1 
−2 
−3 
Here’s one last set of examples generated by varying : 
−3 −2 −1 0 1 2 3 
3 
2 
1 
0 
−1 
−2 
−3 
−3 −2 −1 0 1 2 3 
3 
2 
1 
0 
−1 
−2 
−3 
−3 −2 −1 0 1 2 3 
3 
2 
1 
0 
−1 
−2 
−3 
The plots above used, respectively, 
 = 
 
1 -0.5 
-0.5 1 
 
;  = 
 
1 -0.8 
-0.8 1 
 
; . = 
 
3 0.8 
0.8 1 
 
. 
From the leftmost and middle figures, we see that by decreasing the diagonal 
elements of the covariance matrix, the density now becomes “compressed” 
again, but in the opposite direction. Lastly, as we vary the parameters, more 
generally the contours will form ellipses (the rightmost figure showing an 
example). 
As our last set of examples, fixing  = I, by varying μ, we can also move 
the mean of the density around. 
−3 
−2 
−1 
0 
1 
2 
3 
−3 
−2 
−1 
0 
1 
2 
3 
0.25 
0.2 
0.15 
0.1 
0.05 
−3 
−2 
−1 
0 
1 
2 
3 
−3 
−2 
−1 
0 
1 
2 
3 
0.25 
0.2 
0.15 
0.1 
0.05 
−3 
−2 
−1 
0 
1 
2 
3 
−3 
−2 
−1 
0 
1 
2 
3 
0.25 
0.2 
0.15 
0.1 
0.05 
The figures above were generated using  = I, and respectively 
μ = 
 
1 
0 
 
; μ = 
 
-0.5 
0 
 
; μ = 
 
-1 
-1.5 
 
.
5 
1.2 The Gaussian Discriminant Analysis model 
When we have a classification problem in which the input features x are 
continuous-valued random variables, we can then use the Gaussian Discrim- 
inant Analysis (GDA) model, which models p(x|y) using a multivariate nor- 
mal distribution. The model is: 
y ∼ Bernoulli(φ) 
x|y = 0 ∼ N(μ0,) 
x|y = 1 ∼ N(μ1,) 
Writing out the distributions, this is: 
p(y) = φy(1 − φ)1−y 
p(x|y = 0) = 
1 
(2π)n/2||1/2 exp 
 
− 
1 
2 
(x − μ0)T−1(x − μ0) 
 
p(x|y = 1) = 
1 
(2π)n/2||1/2 exp 
 
− 
1 
2 
(x − μ1)T−1(x − μ1) 
 
Here, the parameters of our model are φ, , μ0 and μ1. (Note that while 
there’re two different mean vectors μ0 and μ1, this model is usually applied 
using only one covariance matrix .) The log-likelihood of the data is given 
by 
ℓ(φ, μ0, μ1,) = log 
Ym 
i=1 
p(x(i), y(i); φ, μ0, μ1,) 
= log 
Ym 
i=1 
p(x(i)|y(i); μ0, μ1,)p(y(i); φ).
6 
By maximizing ℓ with respect to the parameters, we find the maximum like- 
lihood estimate of the parameters (see problem set 1) to be: 
φ = 
1 
m 
Xm 
i=1 
1{y(i) = 1} 
μ0 = 
Pm 
i=1 1{y(i) = 0}x(i) 
Pm 
i=1 1{y(i) = 0} 
μ1 = 
Pm 
i=1 1{y(i) = 1}x(i) 
Pm 
i=1 1{y(i) = 1} 
 = 
1 
m 
Xm 
i=1 
(x(i) − μy(i))(x(i) − μy(i))T . 
Pictorially, what the algorithm is doing can be seen in as follows: 
1 
0 
−1 
−2 
−3 
−4 
−5 
−6 
−7 
−2 −1 0 1 2 3 4 5 6 7 
Shown in the figure are the training set, as well as the contours of the 
two Gaussian distributions that have been fit to the data in each of the 
two classes. Note that the two Gaussians have contours that are the same 
shape and orientation, since they share a covariance matrix , but they have 
different means μ0 and μ1. Also shown in the figure is the straight line 
giving the decision boundary at which p(y = 1|x) = 0.5. On one side of 
the boundary, we’ll predict y = 1 to be the most likely outcome, and on the 
other side, we’ll predict y = 0. 
1.3 Discussion: GDA and logistic regression 
The GDA model has an interesting relationship to logistic regression. If we 
view the quantity p(y = 1|x; φ, μ0, μ1,) as a function of x, we’ll find that it
7 
can be expressed in the form 
p(y = 1|x; φ,, μ0, μ1) = 
1 
1 + exp(−θT x) 
, 
where θ is some appropriate function of φ,, μ0, μ1.1 This is exactly the form 
that logistic regression—a discriminative algorithm—used to model p(y = 
1|x). 
When would we prefer one model over another? GDA and logistic regres- 
sion will, in general, give different decision boundaries when trained on the 
same dataset. Which is better? 
We just argued that if p(x|y) is multivariate gaussian (with shared ), 
then p(y|x) necessarily follows a logistic function. The converse, however, 
is not true; i.e., p(y|x) being a logistic function does not imply p(x|y) is 
multivariate gaussian. This shows that GDA makes stronger modeling as- 
sumptions about the data than does logistic regression. It turns out that 
when these modeling assumptions are correct, then GDA will find better fits 
to the data, and is a better model. Specifically, when p(x|y) is indeed gaus- 
sian (with shared ), then GDA is asymptotically efficient. Informally, 
this means that in the limit of very large training sets (large m), there is no 
algorithm that is strictly better than GDA (in terms of, say, how accurately 
they estimate p(y|x)). In particular, it can be shown that in this setting, 
GDA will be a better algorithm than logistic regression; and more generally, 
even for small training set sizes, we would generally expect GDA to better. 
In contrast, by making significantly weaker assumptions, logistic regres- 
sion is also more robust and less sensitive to incorrect modeling assumptions. 
There are many different sets of assumptions that would lead to p(y|x) taking 
the form of a logistic function. For example, if x|y = 0 ∼ Poisson(λ0), and 
x|y = 1 ∼ Poisson(λ1), then p(y|x) will be logistic. Logistic regression will 
also work well on Poisson data like this. But if we were to use GDA on such 
data—and fit Gaussian distributions to such non-Gaussian data—then the 
results will be less predictable, and GDA may (or may not) do well. 
To summarize: GDA makes stronger modeling assumptions, and is more 
data efficient (i.e., requires less training data to learn “well”) when the mod- 
eling assumptions are correct or at least approximately correct. Logistic 
regression makes weaker assumptions, and is significantly more robust to 
deviations from modeling assumptions. Specifically, when the data is in- 
deed non-Gaussian, then in the limit of large datasets, logistic regression will 
1This uses the convention of redefining the x(i)’s on the right-hand-side to be n + 1- 
(i) 
0 = 1; see problem set 1. 
dimensional vectors by adding the extra coordinate x
8 
almost always do better than GDA. For this reason, in practice logistic re- 
gression is used more often than GDA. (Some related considerations about 
discriminative vs. generative models also apply for the Naive Bayes algo- 
rithm that we discuss next, but the Naive Bayes algorithm is still considered 
a very good, and is certainly also a very popular, classification algorithm.) 
2 Naive Bayes 
In GDA, the feature vectors x were continuous, real-valued vectors. Let’s 
now talk about a different learning algorithm in which the xi’s are discrete- 
valued. 
For our motivating example, consider building an email spam filter using 
machine learning. Here, we wish to classify messages according to whether 
they are unsolicited commercial (spam) email, or non-spam email. After 
learning to do this, we can then have our mail reader automatically filter 
out the spam messages and perhaps place them in a separate mail folder. 
Classifying emails is one example of a broader set of problems called text 
classification. 
Let’s say we have a training set (a set of emails labeled as spam or non- 
spam). We’ll begin our construction of our spam filter by specifying the 
features xi used to represent an email. 
We will represent an email via a feature vector whose length is equal to 
the number of words in the dictionary. Specifically, if an email contains the 
i-th word of the dictionary, then we will set xi = 1; otherwise, we let xi = 0. 
For instance, the vector 
x = 
 
 
1 
0 
0... 
1... 
0 
 
 
a 
aardvark 
aardwolf 
... 
buy 
... 
zygmurgy 
is used to represent an email that contains the words “a” and “buy,” but not 
“aardvark,” “aardwolf” or “zygmurgy.”2 The set of words encoded into the 
2Actually, rather than looking through an english dictionary for the list of all english 
words, in practice it is more common to look through our training set and encode in our 
feature vector only the words that occur at least once there. Apart from reducing the
9 
feature vector is called the vocabulary, so the dimension of x is equal to 
the size of the vocabulary. 
Having chosen our feature vector, we now want to build a generative 
model. So, we have to model p(x|y). But if we have, say, a vocabulary of 
50000 words, then x ∈ {0, 1}50000 (x is a 50000-dimensional vector of 0’s and 
1’s), and if we were to model x explicitly with a multinomial distribution over 
the 250000 possible outcomes, then we’d end up with a (250000−1)-dimensional 
parameter vector. This is clearly too many parameters. 
To model p(x|y), we will therefore make a very strong assumption. We will 
assume that the xi’s are conditionally independent given y. This assumption 
is called theNaive Bayes (NB) assumption, and the resulting algorithm is 
called the Naive Bayes classifier. For instance, if y = 1 means spam email; 
“buy” is word 2087 and “price” is word 39831; then we are assuming that if 
I tell you y = 1 (that a particular piece of email is spam), then knowledge 
of x2087 (knowledge of whether “buy” appears in the message) will have no 
effect on your beliefs about the value of x39831 (whether “price” appears). 
More formally, this can be written p(x2087|y) = p(x2087|y, x39831). (Note that 
this is not the same as saying that x2087 and x39831 are independent, which 
would have been written “p(x2087) = p(x2087|x39831)”; rather, we are only 
assuming that x2087 and x39831 are conditionally independent given y.) 
We now have: 
p(x1, . . . , x50000|y) 
= p(x1|y)p(x2|y, x1)p(x3|y, x1, x2) · · · p(x50000|y, x1, . . . , x49999) 
= p(x1|y)p(x2|y)p(x3|y) · · · p(x50000|y) 
= 
Yn 
i=1 
p(xi|y) 
The first equality simply follows from the usual properties of probabilities, 
and the second equality used the NB assumption. We note that even though 
the Naive Bayes assumption is an extremely strong assumptions, the resulting 
algorithm works well on many problems. 
number of words modeled and hence reducing our computational and space requirements, 
this also has the advantage of allowing us to model/include as a feature many words 
that may appear in your email (such as “cs229”) but that you won’t find in a dictionary. 
Sometimes (as in the homework), we also exclude the very high frequency words (which 
will be words like “the,” “of,” “and,”; these high frequency, “content free” words are called 
stop words) since they occur in so many documents and do little to indicate whether an 
email is spam or non-spam.
10 
Our model is parameterized by φi|y=1 = p(xi = 1|y = 1), φi|y=0 = p(xi = 
1|y = 0), and φy = p(y = 1). As usual, given a training set {(x(i), y(i)); i = 
1, . . . ,m}, we can write down the joint likelihood of the data: 
L(φy, φj|y=0, φj|y=1) = 
Ym 
i=1 
p(x(i), y(i)). 
Maximizing this with respect to φy, φi|y=0 and φi|y=1 gives the maximum 
likelihood estimates: 
φj|y=1 = 
Pm 
i=1 1{x(i) 
j = 1 ∧ y(i) = 1} Pm 
i=1 1{y(i) = 1} 
φj|y=0 = 
Pm 
i=1 1{x(i) 
j = 1 ∧ y(i) = 0} Pm 
i=1 1{y(i) = 0} 
φy = 
Pm 
i=1 1{y(i) = 1} 
m 
In the equations above, the “∧” symbol means “and.” The parameters have 
a very natural interpretation. For instance, φj|y=1 is just the fraction of the 
spam (y = 1) emails in which word j does appear. 
Having fit all these parameters, to make a prediction on a new example 
with features x, we then simply calculate 
p(y = 1|x) = 
p(x|y = 1)p(y = 1) 
p(x) 
= 
( 
Qn 
i=1 p(xi|y = 1)) p(y = 1) 
( 
Qn 
i=1 p(xi|y = 1)) p(y = 1) + ( 
Qn 
i=1 p(xi|y = 0)) p(y = 0) 
, 
and pick whichever class has the higher posterior probability. 
Lastly, we note that while we have developed the Naive Bayes algorithm 
mainly for the case of problems where the features xi are binary-valued, the 
generalization to where xi can take values in {1, 2, . . . , ki} is straightforward. 
Here, we would simply model p(xi|y) as multinomial rather than as Bernoulli. 
Indeed, even if some original input attribute (say, the living area of a house, 
as in our earlier example) were continuous valued, it is quite common to 
discretize it—that is, turn it into a small set of discrete values—and apply 
Naive Bayes. For instance, if we use some feature xi to represent living area, 
we might discretize the continuous values as follows: 
Living area (sq. feet)  400 400-800 800-1200 1200-1600 1600 
xi 1 2 3 4 5
11 
Thus, for a house with living area 890 square feet, we would set the value 
of the corresponding feature xi to 3. We can then apply the Naive Bayes 
algorithm, and model p(xi|y) with a multinomial distribution, as described 
previously. When the original, continuous-valued attributes are not well- 
modeled by a multivariate normal distribution, discretizing the features and 
using Naive Bayes (instead of GDA) will often result in a better classifier. 
2.1 Laplace smoothing 
The Naive Bayes algorithm as we have described it will work fairly well 
for many problems, but there is a simple change that makes it work much 
better, especially for text classification. Let’s briefly discuss a problem with 
the algorithm in its current form, and then talk about how we can fix it. 
Consider spam/email classification, and let’s suppose that, after complet- 
ing CS229 and having done excellent work on the project, you decide around 
June 2003 to submit the work you did to the NIPS conference for publication. 
(NIPS is one of the top machine learning conferences, and the deadline for 
submitting a paper is typically in late June or early July.) Because you end 
up discussing the conference in your emails, you also start getting messages 
with the word “nips” in it. But this is your first NIPS paper, and until this 
time, you had not previously seen any emails containing the word “nips”; 
in particular “nips” did not ever appear in your training set of spam/non- 
spam emails. Assuming that “nips” was the 35000th word in the dictionary, 
your Naive Bayes spam filter therefore had picked its maximum likelihood 
estimates of the parameters φ35000|y to be 
φ35000|y=1 = 
Pm 
i=1 1{x(i) 
35000 = 1 ∧ y(i) P = 1} m 
i=1 1{y(i) = 1} 
= 0 
φ35000|y=0 = 
Pm 
i=1 1{x(i) 
35000 P = 1 ∧ y(i) = 0} m 
i=1 1{y(i) = 0} 
= 0 
I.e., because it has never seen “nips” before in either spam or non-spam 
training examples, it thinks the probability of seeing it in either type of email 
is zero. Hence, when trying to decide if one of these messages containing 
“nips” is spam, it calculates the class posterior probabilities, and obtains 
p(y = 1|x) = 
Qn 
i=1 Q p(xi|y = 1)p(y = 1) n 
i=1 p(xi|y = 1)p(y = 1) + 
Qn 
i=1 p(xi|y = 0)p(y = 0) 
= 
0 
0 
.
12 
This is because each of the terms “ 
Qn 
i=1 p(xi|y)” includes a term p(x35000|y) = 
0 that is multiplied into it. Hence, our algorithm obtains 0/0, and doesn’t 
know how to make a prediction. 
Stating the problem more broadly, it is statistically a bad idea to estimate 
the probability of some event to be zero just because you haven’t seen it be- 
fore in your finite training set. Take the problem of estimating the mean of 
a multinomial random variable z taking values in {1, . . . , k}. We can param- 
eterize our multinomial with φi = p(z = i). Given a set of m independent 
observations {z(1), . . . , z(m)}, the maximum likelihood estimates are given by 
φj = 
Pm 
i=1 1{z(i) = j} 
m 
. 
As we saw previously, if we were to use these maximum likelihood estimates, 
then some of the φj’s might end up as zero, which was a problem. To avoid 
this, we can use Laplace smoothing, which replaces the above estimate 
with 
φj = 
Pm 
i=1 1{z(i) = j} + 1 
m + k 
. 
Here, we’ve added 1 to the numerator, and k to the P denominator. Note that k 
j=1 φj = 1 still holds (check this yourself!), which is a desirable property 
since the φj’s are estimates for probabilities that we know must sum to 1. 
Also, φj6= 0 for all values of j, solving our problem of probabilities being 
estimated as zero. Under certain (arguably quite strong) conditions, it can 
be shown that the Laplace smoothing actually gives the optimal estimator 
of the φj’s. 
Returning to our Naive Bayes classifier, with Laplace smoothing, we 
therefore obtain the following estimates of the parameters: 
φj|y=1 = 
Pm 
i=1 1{x(i) 
j = 1 ∧ y(i) = 1} + 1 Pm 
i=1 1{y(i) = 1} + 2 
φj|y=0 = 
Pm 
i=1 1{x(i) 
j = 1 ∧ y(i) = 0} + 1 Pm 
i=1 1{y(i) = 0} + 2 
(In practice, it usually doesn’t matter much whether we apply Laplace smooth- 
ing to φy or not, since we will typically have a fair fraction each of spam and 
non-spam messages, so φy will be a reasonable estimate of p(y = 1) and will 
be quite far from 0 anyway.)
13 
2.2 Event models for text classification 
To close off our discussion of generative learning algorithms, let’s talk about 
one more model that is specifically for text classification. While Naive Bayes 
as we’ve presented it will work well for many classification problems, for text 
classification, there is a related model that does even better. 
In the specific context of text classification, Naive Bayes as presented uses 
the what’s called themulti-variate Bernoulli event model. In this model, 
we assumed that the way an email is generated is that first it is randomly 
determined (according to the class priors p(y)) whether a spammer or non- 
spammer will send you your next message. Then, the person sending the 
email runs through the dictionary, deciding whether to include each word i 
in that email independently and according to the probabilities Qp(xi = 1|y) = 
φi|y. Thus, the probability of a message was given by p(y) 
n 
i=1 p(xi|y). 
Here’s a different model, called the multinomial event model. To de- 
scribe this model, we will use a different notation and set of features for 
representing emails. We let xi denote the identity of the i-th word in the 
email. Thus, xi is now an integer taking values in {1, . . . , |V |}, where |V | 
is the size of our vocabulary (dictionary). An email of n words is now rep- 
resented by a vector (x1, x2, . . . , xn) of length n; note that n can vary for 
different documents. For instance, if an email starts with “A NIPS . . . ,” 
then x1 = 1 (“a” is the first word in the dictionary), and x2 = 35000 (if 
“nips” is the 35000th word in the dictionary). 
In the multinomial event model, we assume that the way an email is 
generated is via a random process in which spam/non-spam is first deter- 
mined (according to p(y)) as before. Then, the sender of the email writes the 
email by first generating x1 from some multinomial distribution over words 
(p(x1|y)). Next, the second word x2 is chosen independently of x1 but from 
the same multinomial distribution, and similarly for x3, x4, and so on, until 
all n words of the email have Qbeen generated. Thus, the overall probability of 
a message is given by p(y) 
n 
i=1 p(xi|y). Note that this formula looks like the 
one we had earlier for the probability of a message under the multi-variate 
Bernoulli event model, but that the terms in the formula now mean very dif- 
ferent things. In particular xi|y is now a multinomial, rather than a Bernoulli 
distribution. 
The parameters for our new model are φy = p(y) as before, φk|y=1 = 
p(xj = k|y = 1) (for any j) and φi|y=0 = p(xj = k|y = 0). Note that we have 
assumed that p(xj |y) is the same for all values of j (i.e., that the distribution 
according to which a word is generated does not depend on its position j 
within the email).
14 
If we are given a training set {(x(i), y(i)); i = 1, . . . ,m} where x(i) = 
(x(i) 
1 , x(i) 
2 , . . . , x(i) 
ni ) (here, ni is the number of words in the i-training example), 
the likelihood of the data is given by 
L(φ, φk|y=0, φk|y=1) = 
Ym 
i=1 
p(x(i), y(i)) 
= 
Ym 
i=1 
  
Yni 
j=1 
p(x(i) 
j |y; φk|y=0, φk|y=1) 
! 
p(y(i); φy). 
Maximizing this yields the maximum likelihood estimates of the parameters: 
φk|y=1 = 
Pm 
i=1 
Pni 
j=1 1{x(i) 
j = k ∧ y(i) = 1} 
Pm 
i=1 1{y(i) = 1}ni 
φk|y=0 = 
Pm 
i=1 
Pni 
j=1 1{x(i) 
j = k ∧ y(i) = 0} 
Pm 
i=1 1{y(i) = 0}ni 
φy = 
Pm 
i=1 1{y(i) = 1} 
m 
. 
If we were to apply Laplace smoothing (which needed in practice for good 
performance) when estimating φk|y=0 and φk|y=1, we add 1 to the numerators 
and |V | to the denominators, and obtain: 
φk|y=1 = 
Pm 
i=1 
Pni 
j=1 1{x(i) 
j = k ∧ y(i) = 1} + 1 Pm 
i=1 1{y(i) = 1}ni + |V | 
φk|y=0 = 
Pm 
i=1 
Pni 
j=1 1{x(i) 
j = k ∧ y(i) = 0} + 1 Pm 
i=1 1{y(i) = 0}ni + |V | 
. 
While not necessarily the very best classification algorithm, the Naive Bayes 
classifier often works surprisingly well. It is often also a very good “first thing 
to try,” given its simplicity and ease of implementation.

More Related Content

PDF
Cs229 notes9
PDF
"reflections on the probability space induced by moment conditions with impli...
PDF
Engineering Mathematics-IV_B.Tech_Semester-IV_Unit-IV
PPTX
Finite difference method
PPTX
Interpolation
PDF
Comparison Results of Trapezoidal, Simpson’s 13 rule, Simpson’s 38 rule, and ...
PDF
Matrix Transformations on Some Difference Sequence Spaces
PDF
Introduction to Decision Making Theory
Cs229 notes9
"reflections on the probability space induced by moment conditions with impli...
Engineering Mathematics-IV_B.Tech_Semester-IV_Unit-IV
Finite difference method
Interpolation
Comparison Results of Trapezoidal, Simpson’s 13 rule, Simpson’s 38 rule, and ...
Matrix Transformations on Some Difference Sequence Spaces
Introduction to Decision Making Theory

What's hot (18)

PDF
Study Material Numerical Differentiation and Integration
PDF
Multivriada ppt ms
DOCX
Unit 5 Correlation
PPTX
Lecture 11 systems of nonlinear equations
PDF
Andrei rusu-2013-amaa-workshop
PPS
Unit iv
PDF
Reduction forumla
PDF
Flip bifurcation and chaos control in discrete-time Prey-predator model
PDF
Numerical Differentiation and Integration
PDF
Study Material Numerical Solution of Odinary Differential Equations
PPTX
Calculas
PDF
AGGREGATION OF OPINIONS FOR SYSTEM SELECTION USING APPROXIMATIONS OF FUZZY NU...
PDF
AGGREGATION OF OPINIONS FOR SYSTEM SELECTION USING APPROXIMATIONS OF FUZZY NU...
PDF
AGGREGATION OF OPINIONS FOR SYSTEM SELECTION USING APPROXIMATIONS OF FUZZY NU...
PDF
Chebyshev Polynomial Based Numerical Inverse Laplace Transform Solutions of L...
PPTX
Complex Variable & Numerical Method
PDF
Statistics symposium talk, Harvard University
PDF
Transformation of random variables
Study Material Numerical Differentiation and Integration
Multivriada ppt ms
Unit 5 Correlation
Lecture 11 systems of nonlinear equations
Andrei rusu-2013-amaa-workshop
Unit iv
Reduction forumla
Flip bifurcation and chaos control in discrete-time Prey-predator model
Numerical Differentiation and Integration
Study Material Numerical Solution of Odinary Differential Equations
Calculas
AGGREGATION OF OPINIONS FOR SYSTEM SELECTION USING APPROXIMATIONS OF FUZZY NU...
AGGREGATION OF OPINIONS FOR SYSTEM SELECTION USING APPROXIMATIONS OF FUZZY NU...
AGGREGATION OF OPINIONS FOR SYSTEM SELECTION USING APPROXIMATIONS OF FUZZY NU...
Chebyshev Polynomial Based Numerical Inverse Laplace Transform Solutions of L...
Complex Variable & Numerical Method
Statistics symposium talk, Harvard University
Transformation of random variables
Ad

Viewers also liked (19)

PDF
Machine learning (11)
PDF
Programming methodology lecture08
PDF
Machine learning (1)
PDF
3016 all-2007-dist
PDF
Computer network (17)
PDF
Computer network (12)
PDF
Programming methodology lecture04
PDF
Machine learning (10)
PDF
Programming methodology lecture12
PDF
Machine learning (5)
PDF
Machine learning (7)
PDF
Programming methodology lecture28
PDF
Computer network (11)
PDF
Programming methodology lecture15
PDF
Fundamentals of c programming
PDF
Programming methodology lecture22
PDF
Machine learning (4)
PDF
Computer network (4)
PDF
Formal language & automata theory
Machine learning (11)
Programming methodology lecture08
Machine learning (1)
3016 all-2007-dist
Computer network (17)
Computer network (12)
Programming methodology lecture04
Machine learning (10)
Programming methodology lecture12
Machine learning (5)
Machine learning (7)
Programming methodology lecture28
Computer network (11)
Programming methodology lecture15
Fundamentals of c programming
Programming methodology lecture22
Machine learning (4)
Computer network (4)
Formal language & automata theory
Ad

Similar to Machine learning (2) (20)

PDF
PDF
Lecture 3 - Linear Regression
DOCX
Whats u need to graphing polynomials
PDF
Logistic-Regression - Machine learning model
PDF
Introduction to Evidential Neural Networks
PDF
Maximum likelihood estimation of regularisation parameters in inverse problem...
PDF
bayesian_statistics_introduction_uppsala_university
PPTX
Statistical Inference Part II: Types of Sampling Distribution
PDF
Multivariate Gaussin, Rayleigh & Rician distributions
PDF
Cheatsheet supervised-learning
PDF
Cs229 notes7b
PPT
Chapter 3
PPT
Polynomials And Linear Equation of Two Variables
PDF
Appendex b
PDF
Application of Graphic LASSO in Portfolio Optimization_Yixuan Chen & Mengxi J...
PDF
Application of the Monte-Carlo Method to Nonlinear Stochastic Optimization wi...
PDF
lec-ugc-sse.pdf
PDF
Linear models for classification
PDF
Generalised Statistical Convergence For Double Sequences
PDF
(DL hacks輪読)Bayesian Neural Network
Lecture 3 - Linear Regression
Whats u need to graphing polynomials
Logistic-Regression - Machine learning model
Introduction to Evidential Neural Networks
Maximum likelihood estimation of regularisation parameters in inverse problem...
bayesian_statistics_introduction_uppsala_university
Statistical Inference Part II: Types of Sampling Distribution
Multivariate Gaussin, Rayleigh & Rician distributions
Cheatsheet supervised-learning
Cs229 notes7b
Chapter 3
Polynomials And Linear Equation of Two Variables
Appendex b
Application of Graphic LASSO in Portfolio Optimization_Yixuan Chen & Mengxi J...
Application of the Monte-Carlo Method to Nonlinear Stochastic Optimization wi...
lec-ugc-sse.pdf
Linear models for classification
Generalised Statistical Convergence For Double Sequences
(DL hacks輪読)Bayesian Neural Network

More from NYversity (20)

PDF
Programming methodology-1.1
PDF
Programming methodology lecture27
PDF
Programming methodology lecture26
PDF
Programming methodology lecture25
PDF
Programming methodology lecture24
PDF
Programming methodology lecture23
PDF
Programming methodology lecture20
PDF
Programming methodology lecture19
PDF
Programming methodology lecture18
PDF
Programming methodology lecture17
PDF
Programming methodology lecture16
PDF
Programming methodology lecture14
PDF
Programming methodology lecture13
PDF
Programming methodology lecture11
PDF
Programming methodology lecture10
PDF
Programming methodology lecture09
PDF
Programming methodology lecture07
PDF
Programming methodology lecture06
PDF
Programming methodology lecture05
PDF
Programming methodology lecture03
Programming methodology-1.1
Programming methodology lecture27
Programming methodology lecture26
Programming methodology lecture25
Programming methodology lecture24
Programming methodology lecture23
Programming methodology lecture20
Programming methodology lecture19
Programming methodology lecture18
Programming methodology lecture17
Programming methodology lecture16
Programming methodology lecture14
Programming methodology lecture13
Programming methodology lecture11
Programming methodology lecture10
Programming methodology lecture09
Programming methodology lecture07
Programming methodology lecture06
Programming methodology lecture05
Programming methodology lecture03

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Lesson notes of climatology university.
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Pre independence Education in Inndia.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
O7-L3 Supply Chain Operations - ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Basic Mud Logging Guide for educational purpose
Lesson notes of climatology university.
TR - Agricultural Crops Production NC III.pdf
Complications of Minimal Access Surgery at WLH
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Computing-Curriculum for Schools in Ghana
human mycosis Human fungal infections are called human mycosis..pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pre independence Education in Inndia.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
102 student loan defaulters named and shamed – Is someone you know on the list?
FourierSeries-QuestionsWithAnswers(Part-A).pdf

Machine learning (2)

  • 1. CS229 Lecture notes Andrew Ng Part IV Generative Learning algorithms So far, we’ve mainly been talking about learning algorithms that model p(y|x; θ), the conditional distribution of y given x. For instance, logistic regression modeled p(y|x; θ) as h(x) = g(θT x) where g is the sigmoid func- tion. In these notes, we’ll talk about a different type of learning algorithm. Consider a classification problem in which we want to learn to distinguish between elephants (y = 1) and dogs (y = 0), based on some features of an animal. Given a training set, an algorithm like logistic regression or the perceptron algorithm (basically) tries to find a straight line—that is, a decision boundary—that separates the elephants and dogs. Then, to classify a new animal as either an elephant or a dog, it checks on which side of the decision boundary it falls, and makes its prediction accordingly. Here’s a different approach. First, looking at elephants, we can build a model of what elephants look like. Then, looking at dogs, we can build a separate model of what dogs look like. Finally, to classify a new animal, we can match the new animal against the elephant model, and match it against the dog model, to see whether the new animal looks more like the elephants or more like the dogs we had seen in the training set. Algorithms that try to learn p(y|x) directly (such as logistic regression), or algorithms that try to learn mappings directly from the space of inputs X to the labels {0, 1}, (such as the perceptron algorithm) are called discrim- inative learning algorithms. Here, we’ll talk about algorithms that instead try to model p(x|y) (and p(y)). These algorithms are called generative learning algorithms. For instance, if y indicates whether an example is a dog (0) or an elephant (1), then p(x|y = 0) models the distribution of dogs’ features, and p(x|y = 1) models the distribution of elephants’ features. After modeling p(y) (called the class priors) and p(x|y), our algorithm 1
  • 2. 2 can then use Bayes rule to derive the posterior distribution on y given x: p(y|x) = p(x|y)p(y) p(x) . Here, the denominator is given by p(x) = p(x|y = 1)p(y = 1) + p(x|y = 0)p(y = 0) (you should be able to verify that this is true from the standard properties of probabilities), and thus can also be expressed in terms of the quantities p(x|y) and p(y) that we’ve learned. Actually, if were calculating p(y|x) in order to make a prediction, then we don’t actually need to calculate the denominator, since argmax y p(y|x) = argmax y p(x|y)p(y) p(x) = argmax y p(x|y)p(y). 1 Gaussian discriminant analysis The first generative learning algorithm that we’ll look at is Gaussian discrim- inant analysis (GDA). In this model, we’ll assume that p(x|y) is distributed according to a multivariate normal distribution. Let’s talk briefly about the properties of multivariate normal distributions before moving on to the GDA model itself. 1.1 The multivariate normal distribution The multivariate normal distribution in n-dimensions, also called the multi- variate Gaussian distribution, is parameterized by a mean vector μ ∈ Rn and a covariance matrix ∈ Rn×n, where ≥ 0 is symmetric and positive semi-definite. Also written “N(μ,)”, its density is given by: p(x; μ,) = 1 (2π)n/2||1/2 exp − 1 2 (x − μ)T−1(x − μ) . In the equation above, “||” denotes the determinant of the matrix . For a random variable X distributed N(μ,), the mean is (unsurpris- ingly) given by μ: E[X] = Z x x p(x; μ,)dx = μ The covariance of a vector-valued random variable Z is defined as Cov(Z) = E[(Z − E[Z])(Z − E[Z])T ]. This generalizes the notion of the variance of a
  • 3. 3 real-valued random variable. The covariance can also be defined as Cov(Z) = E[ZZT ]−(E[Z])(E[Z])T . (You should be able to prove to yourself that these two definitions are equivalent.) If X ∼ N(μ,), then Cov(X) = . Here’re some examples of what the density of a Gaussian distribution looks like: −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3 0.25 0.2 0.15 0.1 0.05 −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3 0.25 0.2 0.15 0.1 0.05 −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3 0.25 0.2 0.15 0.1 0.05 The left-most figure shows a Gaussian with mean zero (that is, the 2x1 zero-vector) and covariance matrix = I (the 2x2 identity matrix). A Gaus- sian with zero mean and identity covariance is also called the standard nor- mal distribution. The middle figure shows the density of a Gaussian with zero mean and = 0.6I; and in the rightmost figure shows one with , = 2I. We see that as becomes larger, the Gaussian becomes more “spread-out,” and as it becomes smaller, the distribution becomes more “compressed.” Let’s look at some more examples. −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 0.25 0.2 0.15 0.1 0.05 3 −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 0.25 0.2 0.15 0.1 0.05 3 −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 0.25 0.2 0.15 0.1 0.05 3 The figures above show Gaussians with mean 0, and with covariance matrices respectively = 1 0 0 1 ; = 1 0.5 0.5 1 ; . = 1 0.8 0.8 1 . The leftmost figure shows the familiar standard normal distribution, and we see that as we increase the off-diagonal entry in , the density becomes more “compressed” towards the 45◦ line (given by x1 = x2). We can see this more clearly when we look at the contours of the same three densities:
  • 4. 4 −3 −2 −1 0 1 2 3 3 2 1 0 −1 −2 −3 −3 −2 −1 0 1 2 3 3 2 1 0 −1 −2 −3 −3 −2 −1 0 1 2 3 3 2 1 0 −1 −2 −3 Here’s one last set of examples generated by varying : −3 −2 −1 0 1 2 3 3 2 1 0 −1 −2 −3 −3 −2 −1 0 1 2 3 3 2 1 0 −1 −2 −3 −3 −2 −1 0 1 2 3 3 2 1 0 −1 −2 −3 The plots above used, respectively, = 1 -0.5 -0.5 1 ; = 1 -0.8 -0.8 1 ; . = 3 0.8 0.8 1 . From the leftmost and middle figures, we see that by decreasing the diagonal elements of the covariance matrix, the density now becomes “compressed” again, but in the opposite direction. Lastly, as we vary the parameters, more generally the contours will form ellipses (the rightmost figure showing an example). As our last set of examples, fixing = I, by varying μ, we can also move the mean of the density around. −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3 0.25 0.2 0.15 0.1 0.05 −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3 0.25 0.2 0.15 0.1 0.05 −3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3 0.25 0.2 0.15 0.1 0.05 The figures above were generated using = I, and respectively μ = 1 0 ; μ = -0.5 0 ; μ = -1 -1.5 .
  • 5. 5 1.2 The Gaussian Discriminant Analysis model When we have a classification problem in which the input features x are continuous-valued random variables, we can then use the Gaussian Discrim- inant Analysis (GDA) model, which models p(x|y) using a multivariate nor- mal distribution. The model is: y ∼ Bernoulli(φ) x|y = 0 ∼ N(μ0,) x|y = 1 ∼ N(μ1,) Writing out the distributions, this is: p(y) = φy(1 − φ)1−y p(x|y = 0) = 1 (2π)n/2||1/2 exp − 1 2 (x − μ0)T−1(x − μ0) p(x|y = 1) = 1 (2π)n/2||1/2 exp − 1 2 (x − μ1)T−1(x − μ1) Here, the parameters of our model are φ, , μ0 and μ1. (Note that while there’re two different mean vectors μ0 and μ1, this model is usually applied using only one covariance matrix .) The log-likelihood of the data is given by ℓ(φ, μ0, μ1,) = log Ym i=1 p(x(i), y(i); φ, μ0, μ1,) = log Ym i=1 p(x(i)|y(i); μ0, μ1,)p(y(i); φ).
  • 6. 6 By maximizing ℓ with respect to the parameters, we find the maximum like- lihood estimate of the parameters (see problem set 1) to be: φ = 1 m Xm i=1 1{y(i) = 1} μ0 = Pm i=1 1{y(i) = 0}x(i) Pm i=1 1{y(i) = 0} μ1 = Pm i=1 1{y(i) = 1}x(i) Pm i=1 1{y(i) = 1} = 1 m Xm i=1 (x(i) − μy(i))(x(i) − μy(i))T . Pictorially, what the algorithm is doing can be seen in as follows: 1 0 −1 −2 −3 −4 −5 −6 −7 −2 −1 0 1 2 3 4 5 6 7 Shown in the figure are the training set, as well as the contours of the two Gaussian distributions that have been fit to the data in each of the two classes. Note that the two Gaussians have contours that are the same shape and orientation, since they share a covariance matrix , but they have different means μ0 and μ1. Also shown in the figure is the straight line giving the decision boundary at which p(y = 1|x) = 0.5. On one side of the boundary, we’ll predict y = 1 to be the most likely outcome, and on the other side, we’ll predict y = 0. 1.3 Discussion: GDA and logistic regression The GDA model has an interesting relationship to logistic regression. If we view the quantity p(y = 1|x; φ, μ0, μ1,) as a function of x, we’ll find that it
  • 7. 7 can be expressed in the form p(y = 1|x; φ,, μ0, μ1) = 1 1 + exp(−θT x) , where θ is some appropriate function of φ,, μ0, μ1.1 This is exactly the form that logistic regression—a discriminative algorithm—used to model p(y = 1|x). When would we prefer one model over another? GDA and logistic regres- sion will, in general, give different decision boundaries when trained on the same dataset. Which is better? We just argued that if p(x|y) is multivariate gaussian (with shared ), then p(y|x) necessarily follows a logistic function. The converse, however, is not true; i.e., p(y|x) being a logistic function does not imply p(x|y) is multivariate gaussian. This shows that GDA makes stronger modeling as- sumptions about the data than does logistic regression. It turns out that when these modeling assumptions are correct, then GDA will find better fits to the data, and is a better model. Specifically, when p(x|y) is indeed gaus- sian (with shared ), then GDA is asymptotically efficient. Informally, this means that in the limit of very large training sets (large m), there is no algorithm that is strictly better than GDA (in terms of, say, how accurately they estimate p(y|x)). In particular, it can be shown that in this setting, GDA will be a better algorithm than logistic regression; and more generally, even for small training set sizes, we would generally expect GDA to better. In contrast, by making significantly weaker assumptions, logistic regres- sion is also more robust and less sensitive to incorrect modeling assumptions. There are many different sets of assumptions that would lead to p(y|x) taking the form of a logistic function. For example, if x|y = 0 ∼ Poisson(λ0), and x|y = 1 ∼ Poisson(λ1), then p(y|x) will be logistic. Logistic regression will also work well on Poisson data like this. But if we were to use GDA on such data—and fit Gaussian distributions to such non-Gaussian data—then the results will be less predictable, and GDA may (or may not) do well. To summarize: GDA makes stronger modeling assumptions, and is more data efficient (i.e., requires less training data to learn “well”) when the mod- eling assumptions are correct or at least approximately correct. Logistic regression makes weaker assumptions, and is significantly more robust to deviations from modeling assumptions. Specifically, when the data is in- deed non-Gaussian, then in the limit of large datasets, logistic regression will 1This uses the convention of redefining the x(i)’s on the right-hand-side to be n + 1- (i) 0 = 1; see problem set 1. dimensional vectors by adding the extra coordinate x
  • 8. 8 almost always do better than GDA. For this reason, in practice logistic re- gression is used more often than GDA. (Some related considerations about discriminative vs. generative models also apply for the Naive Bayes algo- rithm that we discuss next, but the Naive Bayes algorithm is still considered a very good, and is certainly also a very popular, classification algorithm.) 2 Naive Bayes In GDA, the feature vectors x were continuous, real-valued vectors. Let’s now talk about a different learning algorithm in which the xi’s are discrete- valued. For our motivating example, consider building an email spam filter using machine learning. Here, we wish to classify messages according to whether they are unsolicited commercial (spam) email, or non-spam email. After learning to do this, we can then have our mail reader automatically filter out the spam messages and perhaps place them in a separate mail folder. Classifying emails is one example of a broader set of problems called text classification. Let’s say we have a training set (a set of emails labeled as spam or non- spam). We’ll begin our construction of our spam filter by specifying the features xi used to represent an email. We will represent an email via a feature vector whose length is equal to the number of words in the dictionary. Specifically, if an email contains the i-th word of the dictionary, then we will set xi = 1; otherwise, we let xi = 0. For instance, the vector x =   1 0 0... 1... 0   a aardvark aardwolf ... buy ... zygmurgy is used to represent an email that contains the words “a” and “buy,” but not “aardvark,” “aardwolf” or “zygmurgy.”2 The set of words encoded into the 2Actually, rather than looking through an english dictionary for the list of all english words, in practice it is more common to look through our training set and encode in our feature vector only the words that occur at least once there. Apart from reducing the
  • 9. 9 feature vector is called the vocabulary, so the dimension of x is equal to the size of the vocabulary. Having chosen our feature vector, we now want to build a generative model. So, we have to model p(x|y). But if we have, say, a vocabulary of 50000 words, then x ∈ {0, 1}50000 (x is a 50000-dimensional vector of 0’s and 1’s), and if we were to model x explicitly with a multinomial distribution over the 250000 possible outcomes, then we’d end up with a (250000−1)-dimensional parameter vector. This is clearly too many parameters. To model p(x|y), we will therefore make a very strong assumption. We will assume that the xi’s are conditionally independent given y. This assumption is called theNaive Bayes (NB) assumption, and the resulting algorithm is called the Naive Bayes classifier. For instance, if y = 1 means spam email; “buy” is word 2087 and “price” is word 39831; then we are assuming that if I tell you y = 1 (that a particular piece of email is spam), then knowledge of x2087 (knowledge of whether “buy” appears in the message) will have no effect on your beliefs about the value of x39831 (whether “price” appears). More formally, this can be written p(x2087|y) = p(x2087|y, x39831). (Note that this is not the same as saying that x2087 and x39831 are independent, which would have been written “p(x2087) = p(x2087|x39831)”; rather, we are only assuming that x2087 and x39831 are conditionally independent given y.) We now have: p(x1, . . . , x50000|y) = p(x1|y)p(x2|y, x1)p(x3|y, x1, x2) · · · p(x50000|y, x1, . . . , x49999) = p(x1|y)p(x2|y)p(x3|y) · · · p(x50000|y) = Yn i=1 p(xi|y) The first equality simply follows from the usual properties of probabilities, and the second equality used the NB assumption. We note that even though the Naive Bayes assumption is an extremely strong assumptions, the resulting algorithm works well on many problems. number of words modeled and hence reducing our computational and space requirements, this also has the advantage of allowing us to model/include as a feature many words that may appear in your email (such as “cs229”) but that you won’t find in a dictionary. Sometimes (as in the homework), we also exclude the very high frequency words (which will be words like “the,” “of,” “and,”; these high frequency, “content free” words are called stop words) since they occur in so many documents and do little to indicate whether an email is spam or non-spam.
  • 10. 10 Our model is parameterized by φi|y=1 = p(xi = 1|y = 1), φi|y=0 = p(xi = 1|y = 0), and φy = p(y = 1). As usual, given a training set {(x(i), y(i)); i = 1, . . . ,m}, we can write down the joint likelihood of the data: L(φy, φj|y=0, φj|y=1) = Ym i=1 p(x(i), y(i)). Maximizing this with respect to φy, φi|y=0 and φi|y=1 gives the maximum likelihood estimates: φj|y=1 = Pm i=1 1{x(i) j = 1 ∧ y(i) = 1} Pm i=1 1{y(i) = 1} φj|y=0 = Pm i=1 1{x(i) j = 1 ∧ y(i) = 0} Pm i=1 1{y(i) = 0} φy = Pm i=1 1{y(i) = 1} m In the equations above, the “∧” symbol means “and.” The parameters have a very natural interpretation. For instance, φj|y=1 is just the fraction of the spam (y = 1) emails in which word j does appear. Having fit all these parameters, to make a prediction on a new example with features x, we then simply calculate p(y = 1|x) = p(x|y = 1)p(y = 1) p(x) = ( Qn i=1 p(xi|y = 1)) p(y = 1) ( Qn i=1 p(xi|y = 1)) p(y = 1) + ( Qn i=1 p(xi|y = 0)) p(y = 0) , and pick whichever class has the higher posterior probability. Lastly, we note that while we have developed the Naive Bayes algorithm mainly for the case of problems where the features xi are binary-valued, the generalization to where xi can take values in {1, 2, . . . , ki} is straightforward. Here, we would simply model p(xi|y) as multinomial rather than as Bernoulli. Indeed, even if some original input attribute (say, the living area of a house, as in our earlier example) were continuous valued, it is quite common to discretize it—that is, turn it into a small set of discrete values—and apply Naive Bayes. For instance, if we use some feature xi to represent living area, we might discretize the continuous values as follows: Living area (sq. feet) 400 400-800 800-1200 1200-1600 1600 xi 1 2 3 4 5
  • 11. 11 Thus, for a house with living area 890 square feet, we would set the value of the corresponding feature xi to 3. We can then apply the Naive Bayes algorithm, and model p(xi|y) with a multinomial distribution, as described previously. When the original, continuous-valued attributes are not well- modeled by a multivariate normal distribution, discretizing the features and using Naive Bayes (instead of GDA) will often result in a better classifier. 2.1 Laplace smoothing The Naive Bayes algorithm as we have described it will work fairly well for many problems, but there is a simple change that makes it work much better, especially for text classification. Let’s briefly discuss a problem with the algorithm in its current form, and then talk about how we can fix it. Consider spam/email classification, and let’s suppose that, after complet- ing CS229 and having done excellent work on the project, you decide around June 2003 to submit the work you did to the NIPS conference for publication. (NIPS is one of the top machine learning conferences, and the deadline for submitting a paper is typically in late June or early July.) Because you end up discussing the conference in your emails, you also start getting messages with the word “nips” in it. But this is your first NIPS paper, and until this time, you had not previously seen any emails containing the word “nips”; in particular “nips” did not ever appear in your training set of spam/non- spam emails. Assuming that “nips” was the 35000th word in the dictionary, your Naive Bayes spam filter therefore had picked its maximum likelihood estimates of the parameters φ35000|y to be φ35000|y=1 = Pm i=1 1{x(i) 35000 = 1 ∧ y(i) P = 1} m i=1 1{y(i) = 1} = 0 φ35000|y=0 = Pm i=1 1{x(i) 35000 P = 1 ∧ y(i) = 0} m i=1 1{y(i) = 0} = 0 I.e., because it has never seen “nips” before in either spam or non-spam training examples, it thinks the probability of seeing it in either type of email is zero. Hence, when trying to decide if one of these messages containing “nips” is spam, it calculates the class posterior probabilities, and obtains p(y = 1|x) = Qn i=1 Q p(xi|y = 1)p(y = 1) n i=1 p(xi|y = 1)p(y = 1) + Qn i=1 p(xi|y = 0)p(y = 0) = 0 0 .
  • 12. 12 This is because each of the terms “ Qn i=1 p(xi|y)” includes a term p(x35000|y) = 0 that is multiplied into it. Hence, our algorithm obtains 0/0, and doesn’t know how to make a prediction. Stating the problem more broadly, it is statistically a bad idea to estimate the probability of some event to be zero just because you haven’t seen it be- fore in your finite training set. Take the problem of estimating the mean of a multinomial random variable z taking values in {1, . . . , k}. We can param- eterize our multinomial with φi = p(z = i). Given a set of m independent observations {z(1), . . . , z(m)}, the maximum likelihood estimates are given by φj = Pm i=1 1{z(i) = j} m . As we saw previously, if we were to use these maximum likelihood estimates, then some of the φj’s might end up as zero, which was a problem. To avoid this, we can use Laplace smoothing, which replaces the above estimate with φj = Pm i=1 1{z(i) = j} + 1 m + k . Here, we’ve added 1 to the numerator, and k to the P denominator. Note that k j=1 φj = 1 still holds (check this yourself!), which is a desirable property since the φj’s are estimates for probabilities that we know must sum to 1. Also, φj6= 0 for all values of j, solving our problem of probabilities being estimated as zero. Under certain (arguably quite strong) conditions, it can be shown that the Laplace smoothing actually gives the optimal estimator of the φj’s. Returning to our Naive Bayes classifier, with Laplace smoothing, we therefore obtain the following estimates of the parameters: φj|y=1 = Pm i=1 1{x(i) j = 1 ∧ y(i) = 1} + 1 Pm i=1 1{y(i) = 1} + 2 φj|y=0 = Pm i=1 1{x(i) j = 1 ∧ y(i) = 0} + 1 Pm i=1 1{y(i) = 0} + 2 (In practice, it usually doesn’t matter much whether we apply Laplace smooth- ing to φy or not, since we will typically have a fair fraction each of spam and non-spam messages, so φy will be a reasonable estimate of p(y = 1) and will be quite far from 0 anyway.)
  • 13. 13 2.2 Event models for text classification To close off our discussion of generative learning algorithms, let’s talk about one more model that is specifically for text classification. While Naive Bayes as we’ve presented it will work well for many classification problems, for text classification, there is a related model that does even better. In the specific context of text classification, Naive Bayes as presented uses the what’s called themulti-variate Bernoulli event model. In this model, we assumed that the way an email is generated is that first it is randomly determined (according to the class priors p(y)) whether a spammer or non- spammer will send you your next message. Then, the person sending the email runs through the dictionary, deciding whether to include each word i in that email independently and according to the probabilities Qp(xi = 1|y) = φi|y. Thus, the probability of a message was given by p(y) n i=1 p(xi|y). Here’s a different model, called the multinomial event model. To de- scribe this model, we will use a different notation and set of features for representing emails. We let xi denote the identity of the i-th word in the email. Thus, xi is now an integer taking values in {1, . . . , |V |}, where |V | is the size of our vocabulary (dictionary). An email of n words is now rep- resented by a vector (x1, x2, . . . , xn) of length n; note that n can vary for different documents. For instance, if an email starts with “A NIPS . . . ,” then x1 = 1 (“a” is the first word in the dictionary), and x2 = 35000 (if “nips” is the 35000th word in the dictionary). In the multinomial event model, we assume that the way an email is generated is via a random process in which spam/non-spam is first deter- mined (according to p(y)) as before. Then, the sender of the email writes the email by first generating x1 from some multinomial distribution over words (p(x1|y)). Next, the second word x2 is chosen independently of x1 but from the same multinomial distribution, and similarly for x3, x4, and so on, until all n words of the email have Qbeen generated. Thus, the overall probability of a message is given by p(y) n i=1 p(xi|y). Note that this formula looks like the one we had earlier for the probability of a message under the multi-variate Bernoulli event model, but that the terms in the formula now mean very dif- ferent things. In particular xi|y is now a multinomial, rather than a Bernoulli distribution. The parameters for our new model are φy = p(y) as before, φk|y=1 = p(xj = k|y = 1) (for any j) and φi|y=0 = p(xj = k|y = 0). Note that we have assumed that p(xj |y) is the same for all values of j (i.e., that the distribution according to which a word is generated does not depend on its position j within the email).
  • 14. 14 If we are given a training set {(x(i), y(i)); i = 1, . . . ,m} where x(i) = (x(i) 1 , x(i) 2 , . . . , x(i) ni ) (here, ni is the number of words in the i-training example), the likelihood of the data is given by L(φ, φk|y=0, φk|y=1) = Ym i=1 p(x(i), y(i)) = Ym i=1 Yni j=1 p(x(i) j |y; φk|y=0, φk|y=1) ! p(y(i); φy). Maximizing this yields the maximum likelihood estimates of the parameters: φk|y=1 = Pm i=1 Pni j=1 1{x(i) j = k ∧ y(i) = 1} Pm i=1 1{y(i) = 1}ni φk|y=0 = Pm i=1 Pni j=1 1{x(i) j = k ∧ y(i) = 0} Pm i=1 1{y(i) = 0}ni φy = Pm i=1 1{y(i) = 1} m . If we were to apply Laplace smoothing (which needed in practice for good performance) when estimating φk|y=0 and φk|y=1, we add 1 to the numerators and |V | to the denominators, and obtain: φk|y=1 = Pm i=1 Pni j=1 1{x(i) j = k ∧ y(i) = 1} + 1 Pm i=1 1{y(i) = 1}ni + |V | φk|y=0 = Pm i=1 Pni j=1 1{x(i) j = k ∧ y(i) = 0} + 1 Pm i=1 1{y(i) = 0}ni + |V | . While not necessarily the very best classification algorithm, the Naive Bayes classifier often works surprisingly well. It is often also a very good “first thing to try,” given its simplicity and ease of implementation.