26
Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach, Kumar Introduction to Data Mining 4/18/2004 1

Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Embed Size (px)

Citation preview

Page 1: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Data Mining Classification: Naïve Bayes Classifier

Lecture Notes for Chapter 4 &5

Introduction to Data Miningby

Tan, Steinbach, Kumar

© Tan,Steinbach, Kumar Introduction to Data Mining 4/18/2004 1

Page 2: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Classification: Definition

• Given a collection of records (training set )– Each record contains a set of attributes, one of the

attributes is the class.

• Find a model for class attribute as a function of the values of other attributes.

• Goal: previously unseen records should be assigned a class as accurately as possible.– A test set is used to determine the accuracy of the model. Usually, the

given data set is divided into training and test sets, with training set used to build the model and test set used to validate it.

Page 3: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Illustrating Classification Task

Apply

Model

Induction

Deduction

Learn

Model

Model

Tid Attrib1 Attrib2 Attrib3 Class

1 Yes Large 125K No

2 No Medium 100K No

3 No Small 70K No

4 Yes Medium 120K No

5 No Large 95K Yes

6 No Medium 60K No

7 Yes Large 220K No

8 No Small 85K Yes

9 No Medium 75K No

10 No Small 90K Yes 10

Tid Attrib1 Attrib2 Attrib3 Class

11 No Small 55K ?

12 Yes Medium 80K ?

13 Yes Large 110K ?

14 No Small 95K ?

15 No Large 67K ? 10

Test Set

Learningalgorithm

Training Set

Page 4: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Examples of Classification Task

• Predicting tumor cells as benign or malignant

• Classifying credit card transactions as legitimate or fraudulent

• Classifying secondary structures of protein as alpha-helix, beta-sheet, or random coil

• Categorizing news stories as finance, weather, entertainment, sports, etc

Page 5: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Classification Techniques

• Decision Tree based Methods

• Rule-based Methods

• Memory based reasoning

• Neural Networks

• Naïve Bayes and Bayesian Belief Networks

• Support Vector Machines

Page 6: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Example of a Decision Tree

Tid Refund MaritalStatus

TaxableIncome Cheat

1 Yes Single 125K No

2 No Married 100K No

3 No Single 70K No

4 Yes Married 120K No

5 No Divorced 95K Yes

6 No Married 60K No

7 Yes Divorced 220K No

8 No Single 85K Yes

9 No Married 75K No

10 No Single 90K Yes10

categoric

al

categoric

al

continuous

class

Refund

MarSt

TaxInc

YESNO

NO

NO

Yes No

Married Single, Divorced

< 80K > 80K

Splitting Attributes

Training Data Model: Decision Tree

Page 7: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Another Example of Decision Tree

Tid Refund MaritalStatus

TaxableIncome Cheat

1 Yes Single 125K No

2 No Married 100K No

3 No Single 70K No

4 Yes Married 120K No

5 No Divorced 95K Yes

6 No Married 60K No

7 Yes Divorced 220K No

8 No Single 85K Yes

9 No Married 75K No

10 No Single 90K Yes10

categoric

al

categoric

al

continuous

classMarSt

Refund

TaxInc

YESNO

NO

NO

Yes No

Married Single,

Divorced

< 80K > 80K

There could be more than one tree that fits the same data!

Page 8: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Decision Tree Classification Task

Apply

Model

Induction

Deduction

Learn

Model

Model

Tid Attrib1 Attrib2 Attrib3 Class

1 Yes Large 125K No

2 No Medium 100K No

3 No Small 70K No

4 Yes Medium 120K No

5 No Large 95K Yes

6 No Medium 60K No

7 Yes Large 220K No

8 No Small 85K Yes

9 No Medium 75K No

10 No Small 90K Yes 10

Tid Attrib1 Attrib2 Attrib3 Class

11 No Small 55K ?

12 Yes Medium 80K ?

13 Yes Large 110K ?

14 No Small 95K ?

15 No Large 67K ? 10

Test Set

TreeInductionalgorithm

Training SetDecision Tree

Page 9: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Apply Model to Test Data

Refund

MarSt

TaxInc

YESNO

NO

NO

Yes No

Married Single, Divorced

< 80K > 80K

Refund Marital Status

Taxable Income Cheat

No Married 80K ? 10

Test DataStart from the root of tree.

Page 10: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Apply Model to Test Data

Refund

MarSt

TaxInc

YESNO

NO

NO

Yes No

Married Single, Divorced

< 80K > 80K

Refund Marital Status

Taxable Income Cheat

No Married 80K ? 10

Test Data

Page 11: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Apply Model to Test Data

Refund

MarSt

TaxInc

YESNO

NO

NO

Yes No

Married Single, Divorced

< 80K > 80K

Refund Marital Status

Taxable Income Cheat

No Married 80K ? 10

Test Data

Page 12: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Apply Model to Test Data

Refund

MarSt

TaxInc

YESNO

NO

NO

Yes No

Married Single, Divorced

< 80K > 80K

Refund Marital Status

Taxable Income Cheat

No Married 80K ? 10

Test Data

Page 13: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Apply Model to Test Data

Refund

MarSt

TaxInc

YESNO

NO

NO

Yes No

Married Single, Divorced

< 80K > 80K

Refund Marital Status

Taxable Income Cheat

No Married 80K ? 10

Test Data

Page 14: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Apply Model to Test Data

Refund

MarSt

TaxInc

YESNO

NO

NO

Yes No

Married Single, Divorced

< 80K > 80K

Refund Marital Status

Taxable Income Cheat

No Married 80K ? 10

Test Data

Assign Cheat to “No”

Page 15: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Bayes Classifier

• A probabilistic framework for solving classification problems

• Conditional Probability:

• Bayes theorem:)(

)()|()|(

APCPCAP

ACP

)(),(

)|(

)(),(

)|(

CPCAP

CAP

APCAP

ACP

Page 16: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Example of Bayes Theorem• Given:

– A doctor knows that meningitis causes stiff neck 50% of the time

– Prior probability of any patient having meningitis is 1/50,000

– Prior probability of any patient having stiff neck is 1/20

• If a patient has stiff neck, what’s the probability he/she has meningitis?

0002.020/150000/15.0

)()()|(

)|( SP

MPMSPSMP

Page 17: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Bayesian Classifiers• Consider each attribute and class label as

random variables

• Given a record with attributes (A1, A2,…,An)

– Goal is to predict class C– Specifically, we want to find the value of C that

maximizes P(C| A1, A2,…,An )

• Can we estimate P(C| A1, A2,…,An ) directly from data?

Page 18: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Bayesian Classifiers• Approach:

– compute the posterior probability P(C | A1, A2, …, An) for all values of C using the Bayes theorem

– Choose value of C that maximizes P(C | A1, A2, …, An)

– Equivalent to choosing value of C that maximizes P(A1, A2, …, An|C) P(C)

• How to estimate P(A1, A2, …, An | C )?

)()()|(

)|(21

21

21

n

n

n AAAPCPCAAAP

AAACP

Page 19: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Naïve Bayes Classifier

• Assume independence among attributes Ai

when class is given: – P(A1, A2, …, An |C) = P(A1| Cj) P(A2| Cj)… P(An| Cj)

– Can estimate P(Ai| Cj) for all Ai and Cj.

– New point is classified to Cj if P(Cj) P(Ai| Cj) is maximal.

Page 20: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

How to Estimate Probabilities from Data?

• Class: P(C) = Nc/N– e.g., P(No) = 7/10,

P(Yes) = 3/10

• For discrete attributes:

P(Ai | Ck) = |Aik|/ Nc

– where |Aik| is number of instances having attribute Ai and belongs to class Ck

– Examples:

P(Status=Married|No) = 4/7P(Refund=Yes|Yes)=0

k

Page 21: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

How to Estimate Probabilities from Data?

• For continuous attributes: – Discretize the range into bins

• one ordinal attribute per bin• violates independence assumption

– Two-way split: (A < v) or (A > v)• choose only one of the two splits as new attribute

– Probability density estimation:• Assume attribute follows a normal distribution• Use data to estimate parameters of distribution

(e.g., mean and standard deviation)• Once probability distribution is known, can use it to

estimate the conditional probability P(Ai|c)

k

Page 22: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

How to Estimate Probabilities from Data?

• Normal distribution:

– One for each (Ai,ci) pair

• For (Income, Class=No):– If Class=No

• sample mean = 110• sample variance = 2975

2

2

2

)(

221

)|( ij

ijiA

ij

jiecAP

0072.0)54.54(2

1)|120( )2975(2

)110120( 2

eNoIncomeP

Page 23: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Example of Naïve Bayes Classifier

120K)IncomeMarried,No,Refund( X

P(X|Class=No) = P(Refund=No|Class=No) P(Married| Class=No) P(Income=120K|

Class=No) = 4/7 4/7 0.0072 = 0.0024

P(X|Class=Yes) = P(Refund=No| Class=Yes) P(Married| Class=Yes) P(Income=120K| Class=Yes)

= 1 0 1.2 10-9 = 0

Since P(X|No)P(No) > P(X|Yes)P(Yes)

Therefore P(No|X) > P(Yes|X) => Class = No

Given a Test Record:

Page 24: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Naïve Bayes Classifier

• If one of the conditional probability is zero, then the entire expression becomes zero

• Probability estimation:

mN

mpNCAP

cN

NCAP

N

NCAP

c

ici

c

ici

c

ici

)|(:estimate-m

1)|(:Laplace

)|( :Originalc: number of classes

p: prior probability

m: parameter

Page 25: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Example of Naïve Bayes ClassifierName Give Birth Can Fly Live in Water Have Legs Class

human yes no no yes mammalspython no no no no non-mammalssalmon no no yes no non-mammalswhale yes no yes no mammalsfrog no no sometimes yes non-mammalskomodo no no no yes non-mammalsbat yes yes no yes mammalspigeon no yes no yes non-mammalscat yes no no yes mammalsleopard shark yes no yes no non-mammalsturtle no no sometimes yes non-mammalspenguin no no sometimes yes non-mammalsporcupine yes no no yes mammalseel no no yes no non-mammalssalamander no no sometimes yes non-mammalsgila monster no no no yes non-mammalsplatypus no no no yes mammalsowl no yes no yes non-mammalsdolphin yes no yes no mammalseagle no yes no yes non-mammals

Give Birth Can Fly Live in Water Have Legs Class

yes no yes no ?

0027.02013

004.0)()|(

021.0207

06.0)()|(

0042.0134

133

1310

131

)|(

06.072

72

76

76

)|(

NPNAP

MPMAP

NAP

MAP

A: attributes

M: mammals

N: non-mammals

P(A|M)P(M) > P(A|N)P(N)

=> Mammals

Page 26: Data Mining Classification: Naïve Bayes Classifier Lecture Notes for Chapter 4 &5 Introduction to Data Mining by Tan, Steinbach, Kumar © Tan,Steinbach,

Naïve Bayes (Summary)

• Robust to isolated noise points

• Handle missing values by ignoring the instance during probability estimate calculations

• Robust to irrelevant attributes

• Independence assumption may not hold for some attributes– Use other techniques such as Bayesian Belief

Networks (BBN)