35
Bayes and Naïve Bayes Classifiers CS434

Bayes and Naïve Bayes Classifiers

  • Upload
    others

  • View
    32

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Bayes and Naïve Bayes Classifiers

Bayes and Naïve Bayes Classifiers

CS434

Page 2: Bayes and Naïve Bayes Classifiers

In this lecture

1. Review some basic probability concepts2. Introduce a useful probabilistic rule -

Bayes rule3. Introduce the learning algorithm based on

Bayes rule (thus the name Bayes classifier) and its extension, Naïve Beyes

Page 3: Bayes and Naïve Bayes Classifiers

Commonly used discrete distributions

Categorical distribution: 𝑥𝑥 can take multiple values, 𝑣𝑣1, … , 𝑣𝑣𝑘𝑘

𝑝𝑝 𝑥𝑥 = �𝑖𝑖=1

𝑘𝑘

𝑝𝑝𝑖𝑖𝐼𝐼(𝑥𝑥=𝑣𝑣𝑖𝑖)

Binary random variable 𝑥𝑥~𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵𝐵(𝑝𝑝)

𝑃𝑃 𝑥𝑥 = 1 = 𝑝𝑝;𝑃𝑃 𝑥𝑥 = 0 = 1 − 𝑝𝑝 𝑝𝑝 𝑥𝑥 = 𝑝𝑝𝑥𝑥 1 − 𝑝𝑝 (1−𝑥𝑥)

𝑃𝑃 𝑥𝑥 = 𝑣𝑣1 = 𝑝𝑝1𝑃𝑃 𝑥𝑥 = 𝑣𝑣2 = 𝑝𝑝2𝑃𝑃 𝑥𝑥 = 𝑣𝑣𝑘𝑘 = 𝑝𝑝𝑘𝑘

𝑝𝑝1 + 𝑝𝑝2 + ⋯+ 𝑝𝑝𝑘𝑘 = 1

Page 4: Bayes and Naïve Bayes Classifiers

Learning the parameters of discrete distributions

• Let 𝑥𝑥 denote the event of getting a head when tossing a coin (𝑥𝑥 ∈ {0,1})

• Given a sequence of 𝐵𝐵 coin tosses 𝑥𝑥1, … , 𝑥𝑥𝑛𝑛, we estimate

𝑃𝑃 𝑥𝑥 = 1 =1𝐵𝐵�𝑖𝑖=1

𝑛𝑛

𝑥𝑥𝑖𝑖

• Let 𝑥𝑥 denote the outcome of rolling a die (𝑥𝑥 ∈ {1, … , 6})

• Given a sequence of 𝐵𝐵 rolls, we estimate 𝑃𝑃 𝑥𝑥 = 𝑗𝑗

=1𝐵𝐵�𝑖𝑖=1

𝑛𝑛

𝐼𝐼(𝑥𝑥𝑖𝑖 = 𝑗𝑗)

Estimation using such simple counting performs what we call “Maximum Likelihood Estimation” (MLE). There are other methods as well.

Page 5: Bayes and Naïve Bayes Classifiers

Continuous Random Variables

• A continuous random variable 𝑥𝑥 can take any value in an interval on the real line– 𝑥𝑥 usually corresponds to some real-valued

measurements, e.g., 𝑥𝑥 = today’s lowest temperature – The probability of a continuous random variable

taking an exact value is typically zero: P(𝑥𝑥=56.2)=0– It is more meaningful to measure the probability of a

random variable taking a value within an interval 𝑃𝑃 𝑥𝑥 ∈ 50, 60

– This is captured in the Probability density function

Page 6: Bayes and Naïve Bayes Classifiers

PDF: probability density function

• We often use 𝑓𝑓(𝑥𝑥) to denote the PDF of 𝑥𝑥• 𝑓𝑓(𝑥𝑥) ≥ 0• 𝑓𝑓(𝑥𝑥) can be larger than 1• ∫−∞

∞ 𝑓𝑓 𝑥𝑥 𝑑𝑑𝑥𝑥 = 1

• ∫𝑥𝑥1𝑥𝑥2 𝑓𝑓 𝑥𝑥 𝑑𝑑𝑥𝑥 = 𝑃𝑃(𝑥𝑥1 < 𝑥𝑥 < 𝑥𝑥2)

• If 𝑓𝑓(𝑥𝑥1) = 𝛼𝛼𝑓𝑓 (𝑥𝑥2):When x is sampled from 𝑓𝑓(𝑥𝑥), you are 𝛼𝛼

times as likely to see a 𝑥𝑥 value “near” 𝑥𝑥1than that a 𝑥𝑥 value “near” 𝑥𝑥2

𝑃𝑃(15 < 𝑥𝑥 < 20)

Page 7: Bayes and Naïve Bayes Classifiers

Commonly Used Continuous Distributions

f

f

f

E.g., Suppose we know that in the last 10 minutes, one customer arrived at OSU federal counter #1. The actual time of arrival X can be modeled by a uniform distribution over the interval of (0, 10)

E.g., the body temp. of a person, the average IQ of a class of 1st graders

E.g., the time between two successive forest fires

Page 8: Bayes and Naïve Bayes Classifiers

Copyright © Andrew W. Moore

Conditional Probability• P(A|B) = probability of A being true given

that B is true

F

H

H = “Have a headache”F = “Coming down with Flu”

P(H) = 1/10P(F) = 1/40P(H|F) = 1/2

“Headaches are rare and flu is rarer, but if you’re coming down with a flu there’s a 50-50 chance you’ll have a headache.”

Page 9: Bayes and Naïve Bayes Classifiers

Copyright © Andrew W. Moore

Conditional ProbabilityF

H

H = “Have a headache”F = “Coming down with Flu”

P(H) = 1/10P(F) = 1/40P(H|F) = 1/2

P(H|F)

= Area of “H and F” region------------------------------

Area of “F” region

= P(H ^ F)-----------

P(F)

Page 10: Bayes and Naïve Bayes Classifiers

Copyright © Andrew W. Moore

Definition of Conditional Probability

𝑃𝑃(𝐴𝐴|𝐵𝐵) =𝑃𝑃(𝐴𝐴 ∧ 𝐵𝐵)𝑃𝑃(𝐵𝐵)

Corollary: The Chain Rule𝑃𝑃(𝐴𝐴 ∧ 𝐵𝐵) = 𝑃𝑃(𝐴𝐴|𝐵𝐵) 𝑃𝑃(𝐵𝐵)

Page 11: Bayes and Naïve Bayes Classifiers

Copyright © Andrew W. Moore

Probabilistic Inference

F

H

H = “Have a headache”F = “Coming down with Flu”

P(H) = 1/10P(F) = 1/40P(H|F) = 1/2

One day you wake up with a headache. You think: “Drat! 50% of flus are associated with headaches so I must have a 50-50 chance of coming down with flu”

Is this reasoning good?

Page 12: Bayes and Naïve Bayes Classifiers

Copyright © Andrew W. Moore

Probabilistic Inference

F

H

H = “Have a headache”F = “Coming down with Flu”

P(H) = 1/10P(F) = 1/40P(H|F) = 1/2

P(F ∧ H) = …

P(F|H) = …

Page 13: Bayes and Naïve Bayes Classifiers

Copyright © Andrew W. Moore

What we just did…P(A ∧ B) P(A|B) P(B)

P(B|A) = ----------- = ---------------P(A) P(A)

This is Bayes Rule

Bayes, Thomas (1763) An essay towards solving a problem in the doctrine of chances. Philosophical Transactions of the Royal Society of London, 53:370-418

Page 14: Bayes and Naïve Bayes Classifiers

Copyright © Andrew W. Moore

Using Bayes Rule to Gamble

The “Win” envelope has a dollar and four beads in it

The “Lose” envelope has three beads and no money

Trivial question: someone draws an envelope at random and offers to sell it to you. How much should you pay in order to not lose money on average?

$1.00

Page 15: Bayes and Naïve Bayes Classifiers

Copyright © Andrew W. Moore

Using Bayes Rule to Gamble

The “Win” envelope has a dollar and four beads in it

$1.00

The “Lose” envelope has three beads and no money

Interesting question: before deciding, you are allowed to see one bead drawn from the envelope.

Suppose it’s black: How much should you pay? Suppose it’s red: How much should you pay?

Page 16: Bayes and Naïve Bayes Classifiers

Where are we?

• We have recalled the fundamentals of probability

• We have discussed conditional probability and Bayes rule

• Now we will move on to talk about the Bayes classifier

Page 17: Bayes and Naïve Bayes Classifiers

Basic Idea• Each example is described by 𝑚𝑚 input features, i.e., 𝒙𝒙 =

𝑥𝑥1, 𝑥𝑥2, … , 𝑥𝑥𝑚𝑚 𝑇𝑇, for now we assume they are discrete variables• Each example belongs to one of 𝑐𝑐 possible classes, denoted by 𝑦𝑦 ∈ {1, … , 𝑐𝑐}

• If we don’t know anything about the features, a randomly drawn example has a fixed probability 𝑃𝑃(𝑦𝑦 = 𝑗𝑗) to belong to class 𝑗𝑗

• If an example belongs to class 𝑗𝑗, its features 𝒙𝒙 = 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 𝑇𝑇

will follow some particular distribution 𝑝𝑝 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 𝑦𝑦 = 𝑗𝑗• Given an example 𝒙𝒙 = 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 𝑇𝑇, a bayes classifier reasons

about the value of 𝑦𝑦 using Bayes rule:

𝑃𝑃 𝑦𝑦 = 𝑗𝑗 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 =𝑃𝑃 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 𝑦𝑦 = 𝑗𝑗 𝑃𝑃(𝑦𝑦 = 𝑗𝑗)

𝑃𝑃(𝐵𝐵1, … ,𝐵𝐵𝑚𝑚)

Page 18: Bayes and Naïve Bayes Classifiers

Learning a Bayes Classifier

Given a set of training data 𝑆𝑆 = {(𝒙𝒙𝒊𝒊,𝑦𝑦𝑖𝑖): 𝐵𝐵 = 1, … ,𝐵𝐵}, we learn:

• Class Priors: 𝑃𝑃 𝑦𝑦 = 𝑗𝑗 for 𝑗𝑗 = 1, … 𝑐𝑐

• Class Conditional Distribution: 𝑃𝑃 𝑥𝑥 = 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 𝑇𝑇 𝑦𝑦 = 𝑗𝑗 for = 1, … , 𝑐𝑐

• Marginal Distribution of 𝒙𝒙: 𝑃𝑃(𝐵𝐵1, … ,𝐵𝐵𝑚𝑚)

𝑃𝑃(𝑦𝑦 = 𝑗𝑗) =1𝐵𝐵�𝑖𝑖=1

𝑛𝑛

𝐼𝐼(𝑦𝑦𝑖𝑖 = 𝑗𝑗)

examples class totalof# ),...,,( valueshave that examples class of#

)|,...,(

21

1

juuuj

jyuuP

m

m

=

=

No need to learn, can be computed using

𝑃𝑃(𝒙𝒙) = �𝑗𝑗=1

𝑐𝑐

𝑃𝑃(𝒙𝒙│𝑦𝑦 = 𝑗𝑗)𝑃𝑃(𝑦𝑦 = 𝑗𝑗)

𝑃𝑃 𝑦𝑦 = 𝑗𝑗 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 =𝑃𝑃 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 𝑦𝑦 = 𝑗𝑗 𝑃𝑃(𝑦𝑦 = 𝑗𝑗)

𝑷𝑷(𝒖𝒖𝟏𝟏, … ,𝒖𝒖𝒎𝒎)

Page 19: Bayes and Naïve Bayes Classifiers

Learning a joint distributionBuild a JD table for your attributes in which the probabilities are unspecified

The fill in each row with

records ofnumber totalrow matching records)row(ˆ =P

A B C Prob0 0 0 ?

0 0 1 ?

0 1 0 ?

0 1 1 ?

1 0 0 ?

1 0 1 ?

1 1 0 ?

1 1 1 ?

A B C Prob0 0 0 0.30

0 0 1 0.05

0 1 0 0.10

0 1 1 0.05

1 0 0 0.05

1 0 1 0.10

1 1 0 0.25

1 1 1 0.10Fraction of all records in whichA and B are True but C is False

Page 20: Bayes and Naïve Bayes Classifiers

Example of Learning a Joint• This Joint was

obtained by learning from three attributes in the UCI “Adult” Census Database [Kohavi 1995]

UCI machine learning repository:http://archive.ics.uci.edu/ml/datasets/Adult

48842 examples in total12363 examples with this value combination

Page 21: Bayes and Naïve Bayes Classifiers

Bayes Classifiers in a nutshell

)()|,,(argmax),,(

)()|,,(argmax

),,|(argmax

1

1

1

1predict

jyPjyuuPuuP

jyPjyuuP

uujyPy

mj

m

m

j

mj

===

===

==

1. Estimate 𝑃𝑃(𝑦𝑦 = 𝑗𝑗 ) as fraction of class j examples for j=1,…,c.2. Learn conditional joint distribution 𝑝𝑝(𝑥𝑥1, 𝑥𝑥2, … 𝑥𝑥𝑚𝑚 | 𝑦𝑦 = 𝑗𝑗 ) for 𝑗𝑗 =

1, … , 𝑐𝑐

3. To make a prediction for a new example 𝒙𝒙 = 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 𝑇𝑇

learning

If you wish to have the probability value 𝑃𝑃(𝑦𝑦 = 1| 𝐵𝐵1, … , 𝐵𝐵𝑚𝑚 𝑇𝑇), you will need to compute the normalizing factor 𝑃𝑃 𝐵𝐵1, … , 𝐵𝐵𝑚𝑚 𝑇𝑇 .

Page 22: Bayes and Naïve Bayes Classifiers

Example: Spam Filtering• Use the Bag-of-words representation• Assume a dictionary of 𝑚𝑚 words and tokens• Create one binary attribute for each dictionary entry

– i.e., 𝑥𝑥𝐵𝐵 = 1 means the 𝐵𝐵-th word is used in the email• Consider a reasonable dictionary size: 𝑚𝑚 = 5000 --- we

have 5000 binary attributes• How many parameters that we need to learn?

– We need to learn the class prior 𝑃𝑃(𝑦𝑦=1),𝑃𝑃(𝑦𝑦=0): 1 – For each of the two classes, we need to learn a joint distribution table of

5000 binary variables: 25000 − 1– Total: 2 ∗ 25000 − 1 + 1

• Clearly we don’t have nearly enough data to estimate that many parameters

Page 23: Bayes and Naïve Bayes Classifiers

Joint Distribution Overfits

• It is common to encounter the following situation:– No training examples have the exact value

combinations 𝒙𝒙 = (𝐵𝐵1,𝐵𝐵2, … .𝐵𝐵𝑚𝑚), – 𝑃𝑃(𝒙𝒙|𝑦𝑦 = 𝑗𝑗 ) = 0 for all values of 𝑦𝑦– How do we make predictions for such examples?

• To avoid overfitting– Make some bold assumptions to simplify the joint

distribution

Page 24: Bayes and Naïve Bayes Classifiers

The Naïve Bayes Assumption

• Assume that each feature is independent of any other features given the class label

)|()|()|,,(

11

11

juxPjuxPjyuxuxP

mm

mm

======

Page 25: Bayes and Naïve Bayes Classifiers

A note about independence• Assume A and B are two random events.

Then“A and B are independent”

if and only ifP(A|B) = P(A)

Page 26: Bayes and Naïve Bayes Classifiers

Independence Theorems• Assume P(A|B) = P(A)• Then

P(A^B) = P(A) P(B)

• Assume P(A|B) = P(A)• Then

P(B|A) = P(B)

Page 27: Bayes and Naïve Bayes Classifiers

Examples of independent events

• Two separate coin tosses• Consider the following four events:

– T: Toothache ( I have a toothache)– C: Catch (dentist’s steel probe catches in my

tooth)– A: Cavity (I have a cavity)– W: Weather (weather is good)– P(T, C, A, W) =P(T,C,A)P(W)

Page 28: Bayes and Naïve Bayes Classifiers

Conditional Independence

• P(A|B,C) = P(A|C)– A and B are conditionally independent given

C– P(B|A,C)=P(B|C)

• If A and B are conditionally independent given C, then we have– P(A,B|C) = P(A|C) P(B|C)

Page 29: Bayes and Naïve Bayes Classifiers

Example of conditional independence

– T: Toothache ( I have a toothache)– C: Catch (dentist’s steel probe catches in my tooth)– A: Cavity

T and C are conditionally independent given A: P(T, C|A) =P(T|A)*P(C|A)

So , events that are not independent from each other might be conditionally independent given some fact

It can also happen the other way around. Events that are independent might become conditionally dependent given some fact.

B=Burglar in your house; A = Alarm (Burglar) rang in your houseE = Earthquake happenedB is independent of E (ignoring some minor possible connections between them)However, if we know A is true, then B and E are no longer independent. Why?P(B|A) >> P(B|A, E) Knowing E is true makes it much less likely for B to be true

Page 30: Bayes and Naïve Bayes Classifiers

Naïve Bayes Classifier• By assuming that each attribute is independent

of any other attributes given the class label, we now have a Naïve Bayes Classifier

• Instead of learning a joint distribution of all features, we learn 𝑝𝑝(𝑥𝑥𝑖𝑖|𝑦𝑦 = 𝑗𝑗) separately for each feature 𝑥𝑥𝐵𝐵

• And we compute the joint by taking the product:

𝑃𝑃(𝒙𝒙 = 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 𝑇𝑇|𝑦𝑦 = 𝑗𝑗) = �𝑖𝑖=1

𝑚𝑚

𝑃𝑃 𝑥𝑥𝑖𝑖 = 𝐵𝐵𝑖𝑖 𝑦𝑦 = 𝑗𝑗

Page 31: Bayes and Naïve Bayes Classifiers

Naïve Bayes Classifiers in a nutshell

∏=

====

=====

====

m

iii

j

mmj

mmj

jyuxPjyP

jyPjyuxuxP

uxuxjyPy

1

11

11predict

)|()(argmax

)()|,,(argmax

),,|(argmax

1. Learn the 𝑝𝑝(𝑥𝑥𝑖𝑖| 𝑦𝑦 = 𝑗𝑗 ) for each feature 𝑥𝑥𝑖𝑖, and y value 𝑗𝑗2. Estimate 𝑃𝑃(𝑦𝑦 = 𝑗𝑗 ) as fraction of records with 𝑦𝑦 = 𝑗𝑗 .3. For a new example 𝒙𝒙 = 𝐵𝐵1, … ,𝐵𝐵𝑚𝑚 𝑇𝑇:

learning

Page 32: Bayes and Naïve Bayes Classifiers

ExampleX1 X2 X3 Y1 1 1 01 1 0 00 0 0 00 1 0 10 0 1 10 1 1 1

Apply Naïve Bayes, and make prediction for (1,0,1)?

Page 33: Bayes and Naïve Bayes Classifiers

When Bag-of-words uses counts (Multinomial Naïve Bayes)

• Often text is represented by the number of times each word appeared in it– E.g., “There are some shinning apples on the apple tree” – Word “apple” will be counted as appearing twice (ignoring the difference between

plural and singular form)– How do we use naïve bayes in this case?

• We still learn 𝑝𝑝(𝑤𝑤𝑖𝑖|𝑦𝑦 = 1) for each word 𝐵𝐵• It will be estimated as

# of times word 𝐵𝐵 appeared in spam emailstotal # words in spam emails

• For the sentence S above, we will have:𝑃𝑃 𝑆𝑆 𝑦𝑦 = 1

= 𝑃𝑃 "there" 𝑦𝑦 = 1 𝑃𝑃 "are" 𝑦𝑦 = 1 …𝑃𝑃 "𝑎𝑎𝑝𝑝𝑝𝑝𝐵𝐵𝐵𝐵" 𝑦𝑦 = 1 2 …𝑃𝑃("𝑡𝑡𝐵𝐵𝐵𝐵𝐵𝐵"|𝑦𝑦 = 1)

Similarly for 𝑃𝑃(𝑆𝑆|𝑦𝑦 = 0)

• Bayes rule can then be used to compute 𝑃𝑃 𝑦𝑦 = 1 𝑆𝑆

Page 34: Bayes and Naïve Bayes Classifiers

Laplace Smoothing• With Naïve Bayes Assumption, we still have zero probabilities• E.g., if we receive an email that contains a word 𝑤𝑤 that has

never appeared in the training emails– 𝑃𝑃 𝑤𝑤 𝑠𝑠𝑝𝑝𝑎𝑎𝑚𝑚 = 0 and 𝑃𝑃 𝑤𝑤 𝐵𝐵𝐵𝐵𝐵𝐵𝑠𝑠𝑝𝑝𝑎𝑎𝑚𝑚 = 0

• As such we ignore all the other words in the email because of this single rare word

• Laplace smoothing can help

Binary:

𝑃𝑃 𝑤𝑤 𝑠𝑠𝑝𝑝𝑎𝑎𝑚𝑚 =# of spam emails with word 𝑤𝑤 + 1

# of spam emails + 2Multinomial:

𝑃𝑃 𝑤𝑤 𝑠𝑠𝑝𝑝𝑎𝑎𝑚𝑚 =# of times word 𝑤𝑤 appeared in spam emails + 1

total # words in spam emails +𝑚𝑚

Page 35: Bayes and Naïve Bayes Classifiers

Final Notes about (Naïve) Bayes Classifier

• Any density estimator can be plugged in to estimate 𝑝𝑝(𝑥𝑥1, 𝑥𝑥2, … , 𝑥𝑥𝑚𝑚 |𝑦𝑦) for Bayes, or 𝑝𝑝(𝑥𝑥𝐵𝐵|𝑦𝑦) for Naïve Bayes

• Real valued attributes can be discretized or directly modeled using simple continuous distributions such as Gaussian (Normal) distribution

• Naïve Bayes is wonderfully cheap and survives tens of thousands of attributes easily

• Laplace smoothing is important to avoid extreme probabilities