Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf ·...

36
Introduction to Machine Learning Natural Language Processing: Jordan Boyd-Graber University of Maryland NAÏVE BAYES AND LOGISTIC REGRESSION Slides adapted from Hinrich Schütze and Lauren Hannah Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 1 / 19

Transcript of Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf ·...

Page 1: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Introduction to Machine Learning

Natural Language Processing: JordanBoyd-GraberUniversity of MarylandNAÏVE BAYES AND LOGISTIC REGRESSION

Slides adapted from Hinrich Schütze and Lauren Hannah

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 1 / 19

Page 2: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

By the end of today . . .

� You’ll be able to frame many machine learning tasks as classificationproblems

� Apply logistic regression (given weights) to classify data

� Learn naïve bayes from data

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 2 / 19

Page 3: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Classification

Probabilistic Classification

Given:� A universe X our examples can come from (e.g., English documents

with a predefined vocabulary)

� Examples are represented in this space.� A fixed set of labels y ∈C= {c1,c2, . . . ,cJ}� The classes are human-defined for the needs of an application (e.g., spam

vs. ham).

� A training set D of labeled documents with each labeled document{(x1,y1) . . .(xN ,yN)}

We learn a classifier γ that maps documents to class probabilities:

γ : (x ,y)→ [0,1]

such that∑

y γ(x ,y) = 1

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 3 / 19

Page 4: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Classification

Probabilistic Classification

Given:� A universe X our examples can come from (e.g., English documents

with a predefined vocabulary)� Examples are represented in this space.

� A fixed set of labels y ∈C= {c1,c2, . . . ,cJ}� The classes are human-defined for the needs of an application (e.g., spam

vs. ham).

� A training set D of labeled documents with each labeled document{(x1,y1) . . .(xN ,yN)}

We learn a classifier γ that maps documents to class probabilities:

γ : (x ,y)→ [0,1]

such that∑

y γ(x ,y) = 1

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 3 / 19

Page 5: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Classification

Probabilistic Classification

Given:� A universe X our examples can come from (e.g., English documents

with a predefined vocabulary)� Examples are represented in this space.

� A fixed set of labels y ∈C= {c1,c2, . . . ,cJ}

� The classes are human-defined for the needs of an application (e.g., spamvs. ham).

� A training set D of labeled documents with each labeled document{(x1,y1) . . .(xN ,yN)}

We learn a classifier γ that maps documents to class probabilities:

γ : (x ,y)→ [0,1]

such that∑

y γ(x ,y) = 1

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 3 / 19

Page 6: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Classification

Probabilistic Classification

Given:� A universe X our examples can come from (e.g., English documents

with a predefined vocabulary)� Examples are represented in this space.

� A fixed set of labels y ∈C= {c1,c2, . . . ,cJ}� The classes are human-defined for the needs of an application (e.g., spam

vs. ham).

� A training set D of labeled documents with each labeled document{(x1,y1) . . .(xN ,yN)}

We learn a classifier γ that maps documents to class probabilities:

γ : (x ,y)→ [0,1]

such that∑

y γ(x ,y) = 1

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 3 / 19

Page 7: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Classification

Probabilistic Classification

Given:� A universe X our examples can come from (e.g., English documents

with a predefined vocabulary)� Examples are represented in this space.

� A fixed set of labels y ∈C= {c1,c2, . . . ,cJ}� The classes are human-defined for the needs of an application (e.g., spam

vs. ham).

� A training set D of labeled documents with each labeled document{(x1,y1) . . .(xN ,yN)}

We learn a classifier γ that maps documents to class probabilities:

γ : (x ,y)→ [0,1]

such that∑

y γ(x ,y) = 1

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 3 / 19

Page 8: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Classification

Probabilistic Classification

Given:� A universe X our examples can come from (e.g., English documents

with a predefined vocabulary)� Examples are represented in this space.

� A fixed set of labels y ∈C= {c1,c2, . . . ,cJ}� The classes are human-defined for the needs of an application (e.g., spam

vs. ham).

� A training set D of labeled documents with each labeled document{(x1,y1) . . .(xN ,yN)}

We learn a classifier γ that maps documents to class probabilities:

γ : (x ,y)→ [0,1]

such that∑

y γ(x ,y) = 1

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 3 / 19

Page 9: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Classification

Generative vs. Discriminative Models

Generative

Model joint probability p(x ,y)including the data x .

Naïve Bayes

� Uses Bayes rule to reverseconditioning p(x |y)→ p(y |x)

� Naïve because it ignores jointprobabilities within the datadistribution

Discriminative

Model only conditional probabilityp(y |x), excluding the data x .

Logistic regression

� Logistic: A special mathematicalfunction it uses

� Regression: Combines a weightvector with observations to createan answer

� General cookbook for buildingconditional probability distributions

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 4 / 19

Page 10: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Motivating Naïve Bayes Example

A Classification Problem

� Suppose that I have two coins, C1 and C2

� Now suppose I pull a coin out of my pocket, flip it a bunch of times,record the coin and outcomes, and repeat many times:

C1: 0 1 1 1 1C1: 1 1 0C2: 1 0 0 0 0 0 0 1C1: 0 1C1: 1 1 0 1 1 1C2: 0 0 1 1 0 1C2: 1 0 0 0

� Now suppose I am given a new sequence, 0 0 1; which coin is itfrom?

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 5 / 19

Page 11: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Motivating Naïve Bayes Example

A Classification Problem

� Suppose that I have two coins, C1 and C2

� Now suppose I pull a coin out of my pocket, flip it a bunch of times,record the coin and outcomes, and repeat many times:

C1: 0 1 1 1 1C1: 1 1 0C2: 1 0 0 0 0 0 0 1C1: 0 1C1: 1 1 0 1 1 1C2: 0 0 1 1 0 1C2: 1 0 0 0

� Now suppose I am given a new sequence, 0 0 1; which coin is itfrom?

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 5 / 19

Page 12: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Motivating Naïve Bayes Example

A Classification Problem

This problem has particular challenges:

� different numbers of covariates for each observation

� number of covariates can be large

However, there is some structure:

� Easy to get P(C1), P(C2)

� Also easy to get P(Xi = 1 |C1) and P(Xi = 1 |C2)

� By conditional independence,

P(X = 010 |C1) = P(X1 = 0 |C1)P(X2 = 1 |C1)P(X2 = 0 |C1)

� Can we use these to get P(C1 |X = 001)?

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 6 / 19

Page 13: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Motivating Naïve Bayes Example

A Classification Problem

This problem has particular challenges:

� different numbers of covariates for each observation

� number of covariates can be large

However, there is some structure:

� Easy to get P(C1)= 4/7, P(C2)= 3/7

� Also easy to get P(Xi = 1 |C1) and P(Xi = 1 |C2)

� By conditional independence,

P(X = 010 |C1) = P(X1 = 0 |C1)P(X2 = 1 |C1)P(X2 = 0 |C1)

� Can we use these to get P(C1 |X = 001)?

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 6 / 19

Page 14: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Motivating Naïve Bayes Example

A Classification Problem

This problem has particular challenges:

� different numbers of covariates for each observation

� number of covariates can be large

However, there is some structure:

� Easy to get P(C1)= 4/7, P(C2)= 3/7

� Also easy to get P(Xi = 1 |C1)= 12/16 and P(Xi = 1 |C2)= 6/18

� By conditional independence,

P(X = 010 |C1) = P(X1 = 0 |C1)P(X2 = 1 |C1)P(X2 = 0 |C1)

� Can we use these to get P(C1 |X = 001)?

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 6 / 19

Page 15: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Motivating Naïve Bayes Example

A Classification Problem

Summary: have P(data |class), want P(class |data)

Solution: Bayes’ rule!

P(class |data) =P(data |class)P(class)

P(data)

=P(data |class)P(class)∑C

class=1 P(data |class)P(class)

To compute, we need to estimate P(data |class), P(class) for all classes

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 7 / 19

Page 16: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Motivating Naïve Bayes Example

Naive Bayes Classifier

This works because the coin flips are independent given the coinparameter. What about this case:

� want to identify the type of fruit given a set of features: color, shape andsize

� color: red, green, yellow or orange (discrete)

� shape: round, oval or long+skinny (discrete)

� size: diameter in inches (continuous)

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 8 / 19

Page 17: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Motivating Naïve Bayes Example

Naive Bayes Classifier

Conditioned on type of fruit, these features are not necessarilyindependent:

Given category “apple,” the color “green” has a higher probability given“size < 2”:

P(green |size< 2, apple)> P(green |apple)

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 9 / 19

Page 18: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Motivating Naïve Bayes Example

Naive Bayes Classifier

Using chain rule,

P(apple |green, round ,size = 2)

=P(green, round ,size = 2 |apple)P(apple)∑

fruits P(green, round ,size = 2 | fruit j)P(fruit j)

∝ P(green | round ,size = 2,apple)P(round |size = 2,apple)

×P(size = 2 |apple)P(apple)

But computing conditional probabilities is hard! There are manycombinations of (color ,shape,size) for each fruit.

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 10 / 19

Page 19: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Motivating Naïve Bayes Example

Naive Bayes Classifier

Idea: assume conditional independence for all features given class,

P(green | round ,size = 2,apple) = P(green |apple)

P(round |green,size = 2,apple) = P(round |apple)

P(size = 2 |green, round ,apple) = P(size = 2 |apple)

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 11 / 19

Page 20: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Estimating Probability Distributions

How do we estimate a probability?

� Suppose we want to estimate P(wn =“buy ′′|y =SPAM).

buy buy nigeria opportunity viagranigeria opportunity viagra fly money

fly buy nigeria fly buymoney buy fly nigeria viagra

� Maximum likelihood (ML) estimate of the probability is:

β̂i =ni∑

k nk(1)

� Is this reasonable?

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 12 / 19

Page 21: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Estimating Probability Distributions

How do we estimate a probability?

� Suppose we want to estimate P(wn =“buy ′′|y =SPAM).

buy buy nigeria opportunity viagranigeria opportunity viagra fly money

fly buy nigeria fly buymoney buy fly nigeria viagra

� Maximum likelihood (ML) estimate of the probability is:

β̂i =ni∑

k nk(1)

� Is this reasonable?

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 12 / 19

Page 22: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Estimating Probability Distributions

How do we estimate a probability?

� Suppose we want to estimate P(wn =“buy ′′|y =SPAM).

buy buy nigeria opportunity viagranigeria opportunity viagra fly money

fly buy nigeria fly buymoney buy fly nigeria viagra

� Maximum likelihood (ML) estimate of the probability is:

β̂i =ni∑

k nk(1)

� Is this reasonable?

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 12 / 19

Page 23: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Estimating Probability Distributions

How do we estimate a probability?

� Suppose we want to estimate P(wn =“buy ′′|y =SPAM).

buy buy nigeria opportunity viagranigeria opportunity viagra fly money

fly buy nigeria fly buymoney buy fly nigeria viagra

� Maximum likelihood (ML) estimate of the probability is:

β̂i =ni∑

k nk(1)

� Is this reasonable?

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 12 / 19

Page 24: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Estimating Probability Distributions

The problem with maximum likelihood estimates: Zeros (cont)

� If there were no occurrences of “bagel” in documents in class SPAM,we’d get a zero estimate:

P̂( “bagel”| SPAM) =T SPAM, “bagel”∑

w ′∈V T SPAM,w ′= 0

� →We will get P( SPAM|d) = 0 for any document that contains bagel!

� Zero probabilities cannot be conditioned away.

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 13 / 19

Page 25: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Estimating Probability Distributions

How do we estimate a probability?

� For many applications, we often have a prior notion of what ourprobability distributions are going to look like (for example, non-zero,sparse, uniform, etc.).

� This estimate of a probability distribution is called the maximum aposteriori (MAP) estimate:

βMAP = argmaxβ f (x |β)g(β) (2)

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 14 / 19

Page 26: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Estimating Probability Distributions

How do we estimate a probability?

� For a multinomial distribution (i.e. a discrete distribution, like overwords):

βi =ni +αi∑

k nk +αk(3)

� αi is called a smoothing factor, a pseudocount, etc.

� When αi = 1 for all i , it’s called “Laplace smoothing” and corresponds toa uniform prior over all multinomial distributions (just do this).

� To geek out, the set {α1, . . . ,αN} parameterizes a Dirichlet distribution,which is itself a distribution over distributions and is the conjugate priorof the Multinomial (don’t need to know this).

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 15 / 19

Page 27: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Estimating Probability Distributions

How do we estimate a probability?

� For a multinomial distribution (i.e. a discrete distribution, like overwords):

βi =ni +αi∑

k nk +αk(3)

� αi is called a smoothing factor, a pseudocount, etc.

� When αi = 1 for all i , it’s called “Laplace smoothing” and corresponds toa uniform prior over all multinomial distributions (just do this).

� To geek out, the set {α1, . . . ,αN} parameterizes a Dirichlet distribution,which is itself a distribution over distributions and is the conjugate priorof the Multinomial (don’t need to know this).

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 15 / 19

Page 28: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Estimating Probability Distributions

How do we estimate a probability?

� For a multinomial distribution (i.e. a discrete distribution, like overwords):

βi =ni +αi∑

k nk +αk(3)

� αi is called a smoothing factor, a pseudocount, etc.

� When αi = 1 for all i , it’s called “Laplace smoothing” and corresponds toa uniform prior over all multinomial distributions (just do this).

� To geek out, the set {α1, . . . ,αN} parameterizes a Dirichlet distribution,which is itself a distribution over distributions and is the conjugate priorof the Multinomial (don’t need to know this).

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 15 / 19

Page 29: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Naïve Bayes Definition

The Naïve Bayes classifier

� The Naïve Bayes classifier is a probabilistic classifier.� We compute the probability of a document d being in a class c as

follows:

P(c|d)∝ P(c)∏

1≤i≤nd

P(wi |c)

� nd is the length of the document. (number of tokens)� P(wi |c) is the conditional probability of term wi occurring in a document

of class c� P(wi |c) as a measure of how much evidence wi contributes that c is the

correct class.� P(c) is the prior probability of c.� If a document’s terms do not provide clear evidence for one class vs.

another, we choose the c with higher P(c).

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 16 / 19

Page 30: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Naïve Bayes Definition

The Naïve Bayes classifier

� The Naïve Bayes classifier is a probabilistic classifier.� We compute the probability of a document d being in a class c as

follows:

P(c|d)∝ P(c)∏

1≤i≤nd

P(wi |c)

� nd is the length of the document. (number of tokens)� P(wi |c) is the conditional probability of term wi occurring in a document

of class c� P(wi |c) as a measure of how much evidence wi contributes that c is the

correct class.� P(c) is the prior probability of c.� If a document’s terms do not provide clear evidence for one class vs.

another, we choose the c with higher P(c).

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 16 / 19

Page 31: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Naïve Bayes Definition

The Naïve Bayes classifier

� The Naïve Bayes classifier is a probabilistic classifier.� We compute the probability of a document d being in a class c as

follows:

P(c|d)∝ P(c)∏

1≤i≤nd

P(wi |c)

� nd is the length of the document. (number of tokens)� P(wi |c) is the conditional probability of term wi occurring in a document

of class c� P(wi |c) as a measure of how much evidence wi contributes that c is the

correct class.� P(c) is the prior probability of c.� If a document’s terms do not provide clear evidence for one class vs.

another, we choose the c with higher P(c).

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 16 / 19

Page 32: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Naïve Bayes Definition

Maximum a posteriori class

� Our goal is to find the “best” class.

� The best class in Naïve Bayes classification is the most likely ormaximum a posteriori (MAP) class c map :

c map =argmaxcj∈C

P̂(cj |d) = argmaxcj∈C

P̂(cj)∏

1≤i≤nd

P̂(wi |cj)

� We write P̂ for P since these values are estimates from the training set.

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 17 / 19

Page 33: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Naïve Bayes Definition

Naïve Bayes conditional independence assumption

To reduce the number of parameters to a manageable size, recall the NaïveBayes conditional independence assumption:

P(d |cj) = P(⟨w1, . . . ,wnd⟩|cj) =∏

1≤i≤nd

P(Xi =wi |cj)

We assume that the probability of observing the conjunction of attributes isequal to the product of the individual probabilities P(Xi =wi |cj).Our estimates for these priors and conditional probabilities: P̂(cj) =

Nc+1N+|C|

and P̂(w |c) = Tcw+1(∑

w′∈V Tcw′)+|V |

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 18 / 19

Page 34: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Naïve Bayes Definition

Implementation Detail: Taking the log

� Multiplying lots of small probabilities can result in floating pointunderflow.

� From last time lg is logarithm base 2; ln is logarithm base e.

lgx = a⇔ 2a = x lnx = a⇔ ea = x (4)

� Since lg(xy) = lg(x)+ lg(y), we can sum log probabilities instead ofmultiplying probabilities.

� Since lg is a monotonic function, the class with the highest score doesnot change.

� So what we usually compute in practice is:

c map =argmaxcj∈C

[P̂(cj)∏

1≤i≤nd

P̂(wi |cj)]

argmaxcj∈C

[ln P̂(cj)+∑

1≤i≤nd

ln P̂(wi |cj)]

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 19 / 19

Page 35: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Naïve Bayes Definition

Implementation Detail: Taking the log

� Multiplying lots of small probabilities can result in floating pointunderflow.

� From last time lg is logarithm base 2; ln is logarithm base e.

lgx = a⇔ 2a = x lnx = a⇔ ea = x (4)

� Since lg(xy) = lg(x)+ lg(y), we can sum log probabilities instead ofmultiplying probabilities.

� Since lg is a monotonic function, the class with the highest score doesnot change.

� So what we usually compute in practice is:

c map =argmaxcj∈C

[P̂(cj)∏

1≤i≤nd

P̂(wi |cj)]

argmaxcj∈C

[ln P̂(cj)+∑

1≤i≤nd

ln P̂(wi |cj)]

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 19 / 19

Page 36: Introduction to Machine Learning - UMIACSusers.umiacs.umd.edu/~jbg/teaching/CMSC_470/02a_nb.pdf · By the end of today ... — You’ll be able to frame many machine learning tasks

Naïve Bayes Definition

Implementation Detail: Taking the log

� Multiplying lots of small probabilities can result in floating pointunderflow.

� From last time lg is logarithm base 2; ln is logarithm base e.

lgx = a⇔ 2a = x lnx = a⇔ ea = x (4)

� Since lg(xy) = lg(x)+ lg(y), we can sum log probabilities instead ofmultiplying probabilities.

� Since lg is a monotonic function, the class with the highest score doesnot change.

� So what we usually compute in practice is:

c map =argmaxcj∈C

[P̂(cj)∏

1≤i≤nd

P̂(wi |cj)]

argmaxcj∈C

[ln P̂(cj)+∑

1≤i≤nd

ln P̂(wi |cj)]

Natural Language Processing: Jordan Boyd-Graber | UMD Introduction to Machine Learning | 19 / 19