Docsity

Naive Bayes Classifier: Understanding the Basics, Study notes of Computer Science

Partial preview of the text

Download Naive Bayes Classifier: Understanding the Basics and more Study notes Computer Science in PDF only on Docsity!

Lecture 8Lecture

Oct^15

th^2008

Bayes

Classifiers

in^ a

nutshell

1. Learn the P(

X,X, … X^12

|Y=vm

) for each value vi^

i

3 E ti^

t^ P(Y

)^

f^ ti^

f^

d^ ith

Y

3. Estimate

P(Y=v

) as fraction of records withi^

Y=v^ .i^

4. For a new prediction:

) () |

( argmax

)

| ( argmax

(^11)

(^11)

vY Pv Yu Xu XP

u Xu Xv YP

Y^

mm

v

=

=

=

=

=

L

L

predict

) () |

( argmax

(^11)

vY Pv Yu Xu XP

mm

v

=

=

=

Estimating

the^ joint distribution of

X1^ X^

X m

distribution

of^ X1 ,^ X

,^ …^ X m given^ y^ can

be^ problematic!

Naïve Bayes AssumptionNaïve

Bayes

Assumption

•^ Assume that each attribute is independent ofAssume

that

each

attribute

is^ independent

of

any^ other

attributes

given

the^

class

label^ )|

() | (

) |

(

(^111) 1

i mm i

i mm

vY u XP vY uX P

vY u Xu XP

=

=

=

=

=

L L

A note about independenceA^ note

about

independence

• Assume

A^ and

B^ are

Boolean

Random

Variables.

Then

“A and B are independent”A^ and

B^ are

independent

if^ and

only

if

P(A|B)

P(A)

P(A|B)

=^ P(A)

• “A^

and^ B

are^

independent”

is^ often

notated

as

BA ⊥ BA ⊥

Independence TheoremsIndependence

Theorems

•^ Assume

P(A|B)

=^ P(A)

•^ Assume

P(A|B)

=^ P(A)

•^ Then

P(~A|B)

•^ Then

P(A|~B)

=^ P(~A)

=^ P(A)

Examples of independent eventsExamples

of^

independent

events

•^ Two separate coin tossesTwo

separate

coin

tosses

•^ Consider

the^

following

four

variables:

T T^

th^ h

( I h

t^ th

h )

–^ T:^ T

oothache

(^ I^ have

a^ toothache)

–^ C:^ Catch

(dentist’s

steel

probe

catches

in^ my

tooth)tooth) – A:^ Cavity W W

th

T,^ C,^ A,^ W

–^ W:^

Weather

–^ p(T,

C,^ A,

W)^ =p(T,

C,^ A)

p(W)

T,^ C,^ A^
W

Example of conditional independenceExample

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)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 p^

g 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^ house

E^ =^ Earthquake

happened B^ is^ independent

of^ E^ (ignoring

some^ 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

Naïve

Bayes

Classifier

-^ Assume

you^ want

to^ predict

output

Y^ which

has^ arity

n and Y^

values^ v

,^ v ,^ …^ 12

v. ny

A^

th^

i^ t^

tt ib t^

ll d X (

X^ X^

X )

-^ Assume

there^ are

m^ input

attributes

called^

X=( X ,^ X^1

,^ …^ X) 2 m

-^ Learn

a^ conditional

distribution

of^ p(X|y)

for^ each

possible

y

value,^ y

=^ v ,^ v^12

,^ …^ v,ny,

we^ do^

this^ by:

-^ Break

training

set^ into

n subsets Y^

called^ DS

,^ DS^ ,^ … 12

DS^ based ny^

on

the^ y^ values,

i.e.,^ DS^ i

=^ Records

in^ which

Y=v^ i

-^ For^ each

DS^ ,^ learn i^

a^ joint^ distribution

of^ input

distribution^ )|

(^11

i mm

vY

uX

Pv

Yu

XP

vY

uX

uX

P^

=^ L

(^11

i mm i^

vY

uX

Pv

Yu

XP

=^

L

) () | () | ( argmax

(^11)

vY Pv Yu XP vY uX P Y^

mm

=

=

= =^

L

predict

v

Final Notes about

(Naïve)

Bayes

Classifier

Final

Notes

about

(Naïve)

Bayes

Classifier

-^ Any

density

estimator

can^ be

plugged

in^ to^ estimate

p(X^ ,X^1

, 2

X^ |y) or p(

X^ |y) for Naïve

bayes

…,^ Xm^

|y),^ or

p(X|y)i

for^ Naïve

bayes

-^ Real

valued

attributes

can^ be

modeled

using^

simple

distributions

such^ as

Gaussian

(Normal)

distribution

-^ Zero

probabilities

are^ painful

for^ both

joint^ and

naïve.

A^ hack

called Laplace smoothing can help!called

Laplace

smoothing

can^ help!

-^ Original

estimation: P(X^ =1|y=0)^1

-^ Smoothed

estimation

(never^ estimate

zero^ probability):

(#^ of^ examples

with^ y=0,

X^ =1)/(#^1

of^ examples

with^ y=0)

(^

p^

y)

P(X^ =1|y=0)^1

= ( 1+ # of^ examples

with^ y=0,

X^ =1^ )^ /(^1

k+^ #^ of^ examples

with^ y=0)

-^ Naïve Bayes is wonderfully cheap and survives tens of•^ Naïve

Bayes

is^ wonderfully

cheap

and^ survives

tens^ of

thousands

of^ attributes

easily

Bayes Classifier

is^ a

Generative h

Approach

-^ Generative approach:Generative

approach:

-^ Learn

p(y),^ p(X|y),

and^ then

apply

bayes rule

to^ compute

p(y|X)

for^ making

predictions

-^ This

is^ in^ essence

assuming

that^ each

data^ point

is

independently,

identically

distributed

(i.i.d),

and^ generated

following a

generative process

governed by p(y) and

following

a^ generative

process

governed

by^ p(y)

and

p(X|y)

p(y)y (^) y Bayesclassifier

y^ p(y) Naïve^ Bayesclassifier p(X|y)X

X^1

classifier

X^ m

p(X^ |y)^1

p(X^ |y)m^

Logistic RegressionLogistic

Regression

•^ First let

’s look at the term regression

First^

let s^

look^

at^ the

term

regression

•^ Regression

is^ similar

to^ classification,

except

that the y value we are trying to predict is athat^

the^ y

value

we^ are

trying

to^ predict

is^ a

continuous

value

(as^ opposed

to^ a

categorical

value)value) Classification:

Given^ income,

savings,

predict^

loan^ applicant

as^ “high

risk”^ vs “low

risk”

Regression:

Given^ income,

savings,

predict^

credit^ score

Linear regressionLinear

regression • Essentially

try^ to

fit^ a^ straight

line

through

a^ clouds

of^ points L^ k f

[

]

-^ Look

for^ w=[w

,w,…,w^2

]m

ŷ^ =^ w

+w1x+…+w^1

xandmm^

ŷ^ is^ as

close^ to

y^ as^ possible

y

-^ Logistic

regression

can^ be

think^

of^ as

extension

of^ linear

regression

to^ the

case where the target value

y is

case^ where

the^ target

value

y^ is

binary x

Learning

w^ for

logistic

regression

-^ Given

a^ set^

of^ training

data^ points,

we^ would

like^ to

find^ a

i h^

h h^

weight

vector

w^ such

that

is^ large

(e.g.^ 1)

for^ positive

training

examples,

and^ small

)... (e.g.

(^

xwmm xww

e

xy

P^

+++

0)^ otherwise • This^ can

be^ captured

in^ the

following

objective

function:

p^

g^ j

=^ ∑ i

ii y P L^

)| (log )(

w,x

w

Note^ that

the^ superscript

i^ is^ an^ index

to

the^ examples

in^ the^ training

set

∑^

= − −+ = =^ i

ii i ii i^

yP y yP y^

))]| (^1) ( (^1) log() (^1) () | (^1) ( log[

w,x

w,x

This is call the likelihood function of

w^ and by maximizing this objective function we

This^ is^ call

the^ likelihood

function

of^ w,^ and

by^ maximizing

this^ objective

function

,^ we

perform

what^ we

call^ “maximum

likelihood

estimation”

of^ the^ parameter

w.

Optimizing L(w)Optimizing

L(w)

•^ Unfortunately this does not have a close formUnfortunately

this^

does

not^

have

a^ close

form

solution • Instead we iteratively search for the optimal• Instead,

we^ iteratively

search

for^ the

optimal

w S^

i h^

d^

i^

i^ l^

i

•^ Start

with

a^ random

w,^ iteratively

improve

w

(similar

to^ Perceptron)