27
Data Mining Classification and Prediction 1

Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

Embed Size (px)

Citation preview

Page 1: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

1

Data Mining

Classification and Prediction

Page 2: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

2

Classification predicts categorical class labels (discrete or

nominal) classifies data (constructs a model) based on

the training set and the values (class labels) in a classifying attribute and uses it in classifying new data

New data values are classified according to the generated model

Prediction models continuous-valued functions, i.e.,

predicts unknown or missing values

Classification vs. Prediction

Page 3: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

3

Classification – Aim/Method

Classification: Predicts discrete labels using values of other attributes of that data

Input: Training set Each data has attribute values where one of

them is the class information Method:

A model is generated using training set Accuracy of the model is tested with test sample

Output: New data is assigned to the best fitting class

using the model

Page 4: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

4

Typical applications

Credit approval Target marketing Medical diagnosis Fraud detection Detect user behaviors

Page 5: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

5

Preprocessing

Data transformation Discretisation

Integer age {child, young, mid-aged, old} Normalization

[-1,1], [0,1]… Data Cleaning

Noise reduction Remove unnecessary attributes

Page 6: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

6

Classification—A Two-Step Process

Model construction: describing a set of predetermined classes

Each tuple/sample is assumed to belong to a predefined class, as determined by the class label attribute

The set of tuples used for model construction is training set

The model is represented as classification rules, decision trees, or mathematical formulae

Model usage: for classifying future or unknown objects Estimate accuracy of the model

The known label of test sample is compared with the classified result from the model

Accuracy rate is the percentage of test set samples that are correctly classified by the model

Test set is independent of training set, otherwise over-fitting will occur

If the accuracy is acceptable, use the model to classify data tuples whose class labels are not known

Page 7: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

7

Process (1): Model Construction

TrainingData

NAME RANK YEARS TENUREDMike Assistant Prof 3 noMary Assistant Prof 7 yesBill Professor 2 yesJim Associate Prof 7 yesDave Assistant Prof 6 noAnne Associate Prof 3 no

ClassificationAlgorithms

IF rank = ‘professor’OR years > 6THEN tenured = ‘yes’

Classifier(Model)

Page 8: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

8

Process (2): Check Accuracy of the Model

Classifier

TestingData

NAME RANK YEARS TENUREDTom Assistant Prof 2 noMerlisa Associate Prof 7 noGeorge Professor 5 yesJoseph Assistant Prof 7 yes

IF rank = ‘professor’OR years > 6THEN tenured = ‘yes’

75% accuracy

Page 9: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

9

Process (3): Using the Model in Prediction

Classifier

Unseen Data

(Jeff, Professor, 4)

Tenured?

Page 10: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

10

Supervised vs. Unsupervised Learning

Supervised learning (classification) Supervision: The training data (observations,

measurements, etc.) are accompanied by labels indicating the class of the observations

New data is classified based on the training set

Unsupervised learning (clustering) The class labels of training data is unknown Given a set of measurements, observations,

etc. with the aim of establishing the existence of classes or clusters in the data

Page 11: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

11

Issues: Data Preparation

Data cleaning Preprocess data in order to reduce noise and

handle missing values Relevance analysis (feature selection)

Remove the irrelevant or redundant attributes Data transformation

Generalize and/or normalize data

Page 12: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

12

Issues: Evaluating Classification Methods

Accuracy classifier accuracy: predicting class label predictor accuracy: guessing value of predicted

attributes Speed

time to construct the model (training time) time to use the model (classification/prediction

time) Robustness: handling noise and missing values Scalability: efficiency in disk-resident databases Interpretability

understanding and insight provided by the model

Other measures, e.g., goodness of rules, such as decision tree size or compactness of classification rules

Page 13: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

13

Decision Trees

A tree in the form of a flow diagram Each mid-node -> attribute check Branches -> check result Leaves -> classes

Attribute

CL

Page 14: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

14

Decision Tree Induction: Training Dataset

age income student credit_rating buys_computer<=30 high no fair no<=30 high no excellent no31…40 high no fair yes>40 medium no fair yes>40 low yes fair yes>40 low yes excellent no31…40 low yes excellent yes<=30 medium no fair no<=30 low yes fair yes>40 medium yes fair yes<=30 medium yes excellent yes31…40 medium no excellent yes31…40 high yes fair yes>40 medium no excellent no

Page 15: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

15

Output: A Decision Tree for “buys_computer”

age?

overcast

student? credit rating?

<=30 >4031..40

fairexcellentyesno

NO YES NO YES

YES

Page 16: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

16

Example 2

id Refund

Marital St. Income Cheat

1 Y Single 125K Y

2 N Married 100K N

3 N Single 70K N

4 Y Married 120K Y

5 N Divorced 95K Y

6 N Married 60K N

7 Y Divorced 220K Y

8 N Single 85K Y

9 N Married 75K N

10

N Single 90K Y

Refund?

Mar. St.?

no

Mar.Sin,Div

YES

NO

YES

yes

Income

>80K

NO

<80KThere can be more than 1 tree that fits the data

Predict: Y,Div,75KN,Sin,55K

Page 17: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

17

Algorithm for Decision Tree Induction

Basic algorithm (a greedy algorithm) Tree is constructed in a top-down recursive divide-and-

conquer manner At start, all the training examples are at the root Attributes are categorical (if continuous-valued, they are

discretized in advance) Examples are partitioned recursively based on selected

attributes Test attributes are selected on the basis of a heuristic or

statistical measure (e.g., information gain) Conditions for stopping partitioning

All samples for a given node belong to the same class There are no remaining attributes for further partitioning

– majority voting is employed for classifying the leaf There are no samples left

Page 18: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

18

Best Attribute Selection

Original Data 10 in C0 (males) 10 in C1 (females)

car?

C0: 2C1: 6

yes no

C0: 8C1: 4

Mar. St.?

C0: 5C1: 3

Mar.

C0: 3C1: 4

Sin.

C0: 2C1: 3

Div.

Greedy way:Homogeneous distributionperforms better• Most (all) in the same class

soldier?

C0: 8C1: 0

yes no

C0: 2C1:

Page 19: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

19

Attribute Selection – Information Gain

Based on entropy – uncertainty

If all samples are in the same class then entropy is 0

If uniformly distributed then it is 1 Otherwise 0< entropy < 1

Aim: Pick the attribute that decrease the entropy most

)(log21

i

m

ii ppEntropy

Page 20: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

20

Attribute Selection Measure: Information Gain (ID3/C4.5)

Select the attribute with the highest information gain

Let pi be the probability that an arbitrary tuple in D belongs to class Ci, estimated by |Ci, D|/|D|

Expected information (entropy) needed to classify a tuple in D:

Information needed (after using A to split D into v partitions) to classify D:

Information gained by branching on attribute A

)(log)( 21

i

m

ii ppDInfo

)(||

||)(

1j

v

j

jA DI

D

DDInfo

(D)InfoInfo(D)Gain(A) A

Page 21: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

21

Attribute Selection: Information Gain

g Class P: buys_computer = “yes”

g Class N: buys_computer = “no”

means “age <=30”

has 5 out of 14 samples,

with 2 yes’es and 3 no’s.

Hence

Similarly,

age pi ni I(pi, ni)<=30 2 3 0.97131…40 4 0 0>40 3 2 0.971

694.0)2,3(14

5

)0,4(14

4)3,2(

14

5)(

I

IIDInfoage

048.0)_(

151.0)(

029.0)(

ratingcreditGain

studentGain

incomeGain

246.0)()()( DInfoDInfoageGain ageage income student credit_rating buys_computer

<=30 high no fair no<=30 high no excellent no31…40 high no fair yes>40 medium no fair yes>40 low yes fair yes>40 low yes excellent no31…40 low yes excellent yes<=30 medium no fair no<=30 low yes fair yes>40 medium yes fair yes<=30 medium yes excellent yes31…40 medium no excellent yes31…40 high yes fair yes>40 medium no excellent no

)3,2(14

5I

940.0)14

5(log

14

5)

14

9(log

14

9)5,9()( 22 IDInfo

Page 22: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

22

Computing Information-Gain for Continuous-Value Attributes

Let attribute A be a continuous-valued attribute Must determine the best split point for A

Sort the value A in increasing order Typically, the midpoint between each pair of

adjacent values is considered as a possible split point

(ai+ai+1)/2 is the midpoint between the values of ai and

ai+1

The point with the minimum expected information requirement for A is selected as the split-point for A

Split: D1 is the set of tuples in D satisfying A ≤ split-

point, and D2 is the set of tuples in D satisfying A > split-point

Page 23: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

23

Gain Ratio for Attribute Selection (C4.5)

Information gain measure is biased towards attributes with a large number of values

C4.5 (a successor of ID3) uses gain ratio to overcome the problem (normalization to information gain)

GainRatio(A) = Gain(A)/SplitInfo(A) Ex.

gain_ratio(income) = 0.029/0.926 = 0.031 The attribute with the maximum gain ratio is

selected as the splitting attribute

||

||log

||

||)( 2

1 D

D

D

DDSplitInfo j

v

j

jA

926.0)14

4(log

14

4)

14

6(log

14

6)

14

4(log

14

4)( 222 DSplitInfoA

Page 24: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

24

Gini index (CART, IBM IntelligentMiner)

If a data set D contains examples from n classes, gini index, gini(D) is defined as

where pj is the relative frequency of class j in D If a data set D is split on A into two subsets D1 and D2, the

gini index gini(D) is defined as

Reduction in Impurity:

The attribute provides the smallest ginisplit(D) (or the largest

reduction in impurity) is chosen to split the node (need to enumerate all the possible splitting points for each attribute)

n

jp jDgini

1

21)(

)(||||)(

||||)( 2

21

1 DginiDD

DginiDDDginiA

)()()( DginiDginiAginiA

Page 25: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

25

Gini index (CART, IBM IntelligentMiner)

Ex. D has 9 tuples in buys_computer = “yes” and 5 in “no”

Suppose the attribute income partitions D into 10 in D1:

{medium, high} and 4 in D2 {low}

but gini{low,medium} is 0.442 {low,medium} / {high} is a better split than {low} /

{medium,high} as it has lower gini split and higher Δgini value

459.114

5

14

91)(

22

Dgini

)(14

4)(

14

10)( 21}{ DGiniDGiniDgini lowincome

45.016

9

16

11

14

4

100

16

100

361

14

10

Page 26: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

26

Comparing Attribute Selection Measures

The three measures, in general, return good results but Information gain:

biased towards multivalued attributes Gain ratio:

tends to prefer unbalanced splits in which one partition is much smaller than the others

Gini index: biased to multivalued attributes has difficulty when # of classes is large tends to favor tests that result in equal-sized

partitions and purity in both partitions

Page 27: Data Mining Classification and Prediction 1. 2 Classification predicts categorical class labels (discrete or nominal) classifies data (constructs a model)

27

Overfitting and Tree Pruning

Overfitting: An induced tree may overfit the training data

Too many branches, some may reflect anomalies due to noise or outliers

Poor accuracy for unseen samples

Two approaches to avoid overfitting Prepruning: Halt tree construction early—do not split a

node if this would result in the goodness measure falling below a threshold

Difficult to choose an appropriate threshold Postpruning: Remove branches from a “fully grown” tree—

get a sequence of progressively pruned trees Use a set of data different from the training data to

decide which is the “best pruned tree”