Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive...

56
Chapter 9 – Classification and Regression Trees Roger Bohn April 2017 Notes based on: Data Mining for Business Intelligence Shmueli, Patel & Bruce 1

Transcript of Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive...

Page 1: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Chapter 9 – Classification and Regression TreesRoger Bohn April 2017

Notes based on: Data Mining for Business Intelligence Shmueli, Patel & Bruce

�1

Page 2: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

�2

Page 3: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

�3

Page 4: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

II. Results and Interpretation ● There are 1183 auction results that are a part of the

observation used in the model. The four most important rules are as follows: ● Rule 1: IF(OpenPrice < 1.23), THEN COMPETITIVE with a

91% accuracyRule 2: IF(3.718 > OpenPrice >= 1.23) AND (2572 > sellerRating >= 570.5), THEN COMPETITIVE with

● a 82% accuracyRule 3: IF(OpenPrice >= 1.23) AND (sellerRating < 570.5) THEN COMPETITIVE with a 65% accuracy

● Rule 4: IF(3.718 > OpenPrice >= 1.23) AND (sellerRating >= 2572), THEN NOT COMPETITIVE with a 33% accuracy

�4

Page 5: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Trees and RulesGoal: Classify or predict an outcome based on a set of

predictorsThe output is a set of rules Example: ● Goal: classify a record as “will accept credit card

offer” or “will not accept”● Rule might be “IF (Income > 92.5) AND (Education <

1.5) AND (Family <= 2.5) THEN Class = 0 (nonacceptor)

● Also called CART, Decision Trees, or just Trees● Rules are represented by tree diagrams

�5

Page 6: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Trees and RulesGoal: Classify or predict an outcome based on a set of

predictorsThe output is a set of rules, shown as a tree Example: ● Goal: classify a record as “will accept credit card

offer” or “will not accept”● Rule might be “IF (Income > 92.5) AND (Education <

1.5) AND (Family <= 2.5) THEN Class = 0 (nonacceptor)

● Also called CART, Decision Trees, or just Trees● Rules are represented by tree diagrams

�6

Page 7: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

�7

Page 8: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Key Ideas Recursive partitioning: Repeatedly split the records into two parts so as to achieve maximum homogeneity within the new parts

Pruning the tree: Simplify the tree by pruning peripheral branches to avoid overfitting

�8

Page 9: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Recursive Partitioning

�9

Page 10: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Recursive Partitioning Steps● Pick one of the predictor variables, xi

● Pick a value of xi, say si, that divides the training data into two (not necessarily equal) portions

● Measure how “pure” or homogeneous each of the resulting portions are “Pure” = containing records of mostly one class

● Algorithm tries different values of xi, and si to maximize purity in initial split

● After you get a “maximum purity” split, repeat the process for a second split, and so on

�10

Page 11: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Example: Riding Mowers

● Goal: Classify 24 households as owning or not owning riding mowers

● Predictors = Income, Lot Size

�11

Page 12: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Income Lot_Size Ownership60.0 18.4 owner85.5 16.8 owner64.8 21.6 owner61.5 20.8 owner87.0 23.6 owner110.1 19.2 owner108.0 17.6 owner82.8 22.4 owner69.0 20.0 owner93.0 20.8 owner51.0 22.0 owner81.0 20.0 owner75.0 19.6 non-owner52.8 20.8 non-owner64.8 17.2 non-owner43.2 20.4 non-owner84.0 17.6 non-owner49.2 17.6 non-owner59.4 16.0 non-owner66.0 18.4 non-owner47.4 16.4 non-owner33.0 18.8 non-owner51.0 14.0 non-owner63.0 14.8 non-owner�12

Page 13: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

How to split● Order records according to one variable, say lot

size

● Find midpoints between successive valuesE.g. first midpoint is 14.4 (halfway between 14.0 and 14.8)

● Divide records into those with lotsize > 14.4 and those < 14.4

● After evaluating that split, try the next one, which is 15.4 (halfway between 14.8 and 16.0)

�13

Page 14: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Note: Categorical Variables● Examine all possible ways in which the categories

can be split.

● E.g., categories A, B, C can be split 3 ways{A} and {B, C}{B} and {A, C}{C} and {A, B}

● With many categories, # of splits becomes huge● XLMiner supports only binary categorical variables

�14

Page 15: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

The first split: Lot Size = 19,000

�15

Page 16: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Second Split: Income = $84,000

�16

Page 17: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

After All Splits

�17

Page 18: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Measuring Impurity

�18

Page 19: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Gini IndexGini Index for rectangle A containing m records

p = proportion of cases in rectangle A that belong to class k

● I(A) = 0 when all cases belong to same class● Max value when all classes are equally represented (=

0.50 in binary case)

Note: XLMiner uses a variant called “delta splitting rule”

I(A) = 1 -

�19

Page 20: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Entropy

p = proportion of cases (out of m) in rectangle A that belong to class k

● Entropy ranges between 0 (most pure) and log2(m) (equal representation of classes)

�20

Page 21: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Impurity and Recursive Partitioning

● Obtain overall impurity measure (weighted avg. of individual rectangles)

● At each successive stage, compare this measure across all possible splits in all variables

● Choose the split that reduces impurity the most

● Chosen split points become nodes on the tree

�21

Page 22: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

First Split – The Tree

�22

Page 23: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Tree after three splits

�23

Page 24: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Tree Structure

● Split points become nodes on tree (circles with split value in center)

● Rectangles represent “leaves” (terminal points, no further splits, classification value noted)

● Numbers on lines between nodes indicate # cases

● Read down tree to derive ruleE.g., If lot size < 19, and if income > 84.75, then class = “owner”

�24

Page 25: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Determining Leaf Node Label

● Each leaf node label is determined by “voting” of the records within it, and by the cutoff value

● Records within each leaf node are from the training data

● Default cutoff=0.5 means that the leaf node’s label is the majority class.

● Cutoff = 0.75: requires majority of 75% or more “1” records in the leaf to label it a “1” node

�25

Page 26: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Tree after all splits

�26

Page 27: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

The Overfitting Problem

�27

Page 28: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Stopping Tree Growth● Natural end of process is 100% purity in each leaf

● This overfits the data, which end up fitting noise in the data

● Overfitting leads to low predictive accuracy of new data

● Past a certain point, the error rate for the validation data starts to increase

�28

Page 29: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Full Tree Error Rate

�29

Page 30: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

CHAID

CHAID, older than CART, uses chi-square statistical test to limit tree growth

Splitting stops when purity improvement is not statistically significant

�30

Page 31: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Pruning● CART lets tree grow to full extent, then prunes it back

● Idea is to find that point at which the validation error begins to rise

● Generate successively smaller trees by pruning leaves

● At each pruning stage, multiple trees are possible

● Use cost complexity to choose the best tree at that stage

�31

Page 32: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Cost Complexity

CC(T) = cost complexity of a treeErr(T) = proportion of misclassified recordsα = penalty factor attached to tree size (set by

user)

● Among trees of given size, choose the one with lowest CC

● Do this for each size of tree

CC(T) = Err(T) + α L(T)

�32

Page 33: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Using Validation Error to PrunePruning process yields a set of trees of different sizes and associated error rates

Two trees of interest:●Minimum error tree

Has lowest error rate on validation data●Best pruned tree

Smallest tree within one std. error of min. errorThis adds a bonus for simplicity/parsimony

�33

Page 34: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Error rates on pruned trees

�34

Page 35: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Regression Trees

�35

Page 36: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Regression Trees for Prediction

● Used with continuous outcome variable● Procedure similar to classification tree● Many splits attempted, choose the one that

minimizes impurity

�36

Page 37: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Differences from CT● Prediction is computed as the average of

numerical target variable in the rectangle (in CT it is majority vote)

● Impurity measured by sum of squared deviations from leaf mean

● Performance measured by RMSE (root mean squared error)

�37

Page 38: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Advantages of trees● Easy to use, understand● Produce rules that are easy to interpret &

implement● Variable selection & reduction is automatic● Do not require the assumptions of statistical

models● Work fine with missing data

�38

Page 39: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Disadvantages

● May not perform well where there is structure in the data that is not well captured by horizontal or vertical splits

● Very simple, don’t always give best fits

Page 40: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Summary● Classification and Regression Trees are an easily

understandable and transparent method for predicting or classifying new records

● A tree is a graphical representation of a set of rules

● Trees must be pruned to avoid over-fitting of the training data

● As trees do not make any assumptions about the data structure, they usually require large samples

�40

Page 41: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Toyota Corolla Prices● Case analysis

● Higher or lower than median price?● 1436 records, 38 attributes● Textbook page 237● Clean up and

�41

Page 42: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Lots of variables. Which to use?

● Look for unimportant variables● Little variation in the data● Zero correlation (not always safe)

● Look for groups of variables (high correlation)● Logically irrelevant to price (judgment)

�42

Page 43: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

�43

Page 44: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

�44

5/28/2014 ggpairs3.png (800×800)

http://solomonmessing.files.wordpress.com/2014/01/ggpairs3.png 1/1

Page 45: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

ggpairs1#,Uncomment,these,lines,and,install,if,necessary: 2 #install.packages('GGally')3 #install.packages('ggplot2')4 #install.packages('scales')5 #install.packages('memisc') 6 7 library(ggplot2) 8 library(GGally) 9 library(scales) 10 data(diamonds)11 12 diasamp = diamonds[sample(1:length(diamonds$price),10000),]13 ggpairs(diasamp,params = c(shape = I("."),outlier.shape=I(".")))

�45

Page 46: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

another ggpairs example

�46

https://tgmstat.wordpress.com/2013/11/13/plot-matrix-with-the-r-package-ggally/

Page 47: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

One variable regression lines

�47 https://www.r-bloggers.com/multiple-regression-lines-in-ggpairs/

Notice 3 regions;each can have different information.

Page 48: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

�48

272 CHAPTER 11 Intermediate graphs

disp -0.85 0.90 1.00 0.79 -0.710 0.89 -0.434 -0.71 -0.591 -0.56 0.395hp -0.78 0.83 0.79 1.00 -0.449 0.66 -0.708 -0.72 -0.243 -0.13 0.750drat 0.68 -0.70 -0.71 -0.45 1.000 -0.71 0.091 0.44 0.713 0.70 -0.091wt -0.87 0.78 0.89 0.66 -0.712 1.00 -0.175 -0.55 -0.692 -0.58 0.428qsec 0.42 -0.59 -0.43 -0.71 0.091 -0.17 1.000 0.74 -0.230 -0.21 -0.656vs 0.66 -0.81 -0.71 -0.72 0.440 -0.55 0.745 1.00 0.168 0.21 -0.570am 0.60 -0.52 -0.59 -0.24 0.713 -0.69 -0.230 0.17 1.000 0.79 0.058gear 0.48 -0.49 -0.56 -0.13 0.700 -0.58 -0.213 0.21 0.794 1.00 0.274carb -0.55 0.53 0.39 0.75 -0.091 0.43 -0.656 -0.57 0.058 0.27 1.000

Which variables are most related? Which variables are relatively independent? Arethere any patterns? It isn’t that easy to tell from the correlation matrix without signifi-cant time and effort (and probably a set of colored pens to make notations).

You can display that same correlation matrix using the corrgram() function in thecorrgram package (see figure 11.17). The code is

library(corrgram)corrgram(mtcars, order=TRUE, lower.panel=panel.shade, upper.panel=panel.pie, text.panel=panel.txt, main="Corrgram of mtcars intercorrelations")

gear

am

drat

mpg

vs

qsec

wt

disp

cyl

hp

carb

Corrgram of mtcars intercorrelations

Figure 11.17 Corrgram of the correlations among the variables in the mtcars data frame. Rows and columns have been reordered using principal components analysis.

Licensed to Alice Shih <[email protected]>

Page 49: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

�49

Page 50: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Corolla tree: age, km,

�50

Page 51: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

�51

Page 52: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

How to build in practice● Start with every plausible variable● Let the algorithm decide what belongs in final

model● Do NOT pre-screen based on theory

● Do Throw out obvious junk● Consider pruning highly correlated variables (at least

at first)

�52

Page 53: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Evaluating result: Confusion matrix● Calculate model using only training data● Evaluate model using only testing (or validation)

data.

�53

Page 54: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Report what you DID

● Don’t omit so many variables unless sure they don’t matter.

�54

Page 55: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Typical results ● Age (in months)● Km traveled● Air conditioning● Weight

�55

Page 56: Notes based on: Data Mining for Business Intelligence€¦ · Find midpoints between successive values E.g. first midpoint is 14.4 (halfway between 14.0 and 14.8) Divide records into

Air conditioning● AC Yes/No● Automatic AC Yes/no● Model thinks this is 2 independent variables● Use outside knowledge:● Convert this to 1 variable with 3 le● vels

�56