Top Banner
Decision Trees
38

Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Dec 18, 2015

Download

Documents

Jordan Peters
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Decision Trees

Page 2: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

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 3: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

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 4: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

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 5: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

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 6: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

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 7: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

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 8: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

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 9: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

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 10: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Digression: Entropy

Page 11: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Bits• We are watching a set of independent random samples of

X• We see that X has four possible values

• So we might see: BAACBADCDADDDA…• We transmit data over a binary serial link. We can encode

each reading with two bits (e.g. A=00, B=01, C=10, D = 11)

0100001001001110110011111100…

Page 12: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Fewer Bits• Someone tells us that the probabilities are not

equal

• It’s possible……to invent a coding for your transmission that only

uses1.75 bits on average per symbol. Here is one.

Page 13: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

General Case• Suppose X can have one of m values…

• What’s the smallest possible number of bits, on average, per symbol, needed to transmit a stream of symbols drawn from X’s distribution? It’s

• Well, Shannon got to this formula by setting down several desirable properties for uncertainty, and then finding it.

mmm ppppppentropy 21211 log...log),...,(

Page 14: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Back to Decision Trees

Page 15: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Constructing decision trees (ID3)• Normal procedure: top down in a recursive divide-and-

conquer fashion

– First: an attribute is selected for root node and a branch is

created for each possible attribute value

– Then: the instances are split into subsets (one for each

branch extending from the node)

– Finally: the same procedure is repeated recursively for each

branch, using only instances that reach the branch

• Process stops if all instances have the same class

Page 16: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Weather dataOutlook Temp Humidity Windy Play

Sunny Hot High False No

Sunny Hot High True No

Overcast Hot High False Yes

Rainy Mild High False Yes

Rainy Cool Normal False Yes

Rainy Cool Normal True No

Overcast Cool Normal True Yes

Sunny Mild High False No

Sunny Cool Normal False Yes

Rainy Mild Normal False Yes

Sunny Mild Normal True Yes

Overcast Mild High True Yes

Overcast Hot Normal False Yes

Rainy Mild High True No

Page 17: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Which attribute to select?

(a)(b)

(c) (d)

Page 18: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

A criterion for attribute selection• Which is the best attribute?

• The one which will result in the smallest tree– Heuristic: choose the attribute that produces the “purest”

nodes

• Popular impurity criterion: entropy of nodes– Lower the entropy purer the node.

• Strategy: choose attribute that results in lowest entropy of the children nodes.

Page 19: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Attribute “Outlook”outlook=sunny

info([2,3]) = entropy(2/5,3/5) = -2/5*log(2/5) -3/5*log(3/5) = .971

outlook=overcastinfo([4,0]) = entropy(4/4,0/4) = -1*log(1) -0*log(0) = 0

outlook=rainyinfo([3,2]) = entropy(3/5,2/5) = -3/5*log(3/5)-2/5*log(2/5) = .971

Expected info:.971*(5/14) + 0*(4/14) + .971*(5/14) = .693

0*log(0) is normally

not defined.

Page 20: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Attribute “Temperature”temperature=hot

info([2,2]) = entropy(2/4,2/4) = -2/4*log(2/4) -2/4*log(2/4) = 1

temperature=mildinfo([4,2]) = entropy(4/6,2/6) = -4/6*log(1) -2/6*log(2/6) = .528

temperature=coolinfo([3,1]) = entropy(3/4,1/4) = -3/4*log(3/4)-1/4*log(1/4) = .811

Expected info:1*(4/14) + .528*(6/14) + .811*(4/14) = .744

Page 21: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Attribute “Humidity”humidity=high

info([3,4]) = entropy(3/7,4/7) = -3/7*log(3/7) -4/7*log(4/7) = .985

humidity=normalinfo([6,1]) = entropy(6/7,1/7) = -6/7*log(6/7) -1/7*log(1/7) = .592

Expected info:.985*(7/14) + .592*(7/14) = .788

Page 22: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Attribute “Windy”windy=false

info([6,2]) = entropy(6/8,2/8) = -6/8*log(6/8) -2/8*log(2/8) = .811

humidity=trueinfo([3,3]) = entropy(3/6,3/6) = -3/6*log(3/6) -3/6*log(3/6) = 1

Expected info:.811*(8/14) + 1*(6/14) = .892

Page 23: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

And the winner is..."Outlook"

...So, the root will be "Outlook"

Outlook

Page 24: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Continuing to split (for Outlook="Sunny")

Outlook Temp Humidity Windy Play

Sunny Hot High False No

Sunny Hot High True No

Sunny Mild High False No

Sunny Cool Normal False Yes

Sunny Mild Normal True Yes

Which one to choose?

Page 25: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Continuing to split (for Outlook="Sunny")

temperature=hot: info([2,0]) = entropy(2/2,0/2) = 0

temperature=mild: info([1,1]) = entropy(1/2,1/2) = 1

temperature=cool: info([1,0]) = entropy(1/1,0/1) = 0

Expected info: 0*(2/5) + 1*(2/5) + 0*(1/5) = .4

humidity=high: info([3,0]) = 0

humidity=normal: info([2,0]) = 0

Expected info: 0

windy=false: info([1,2]) = entropy(1/3,2/3) =

-1/3*log(1/3) -2/3*log(2/3) = .918

humidity=true: info([1,1]) = entropy(1/2,1/2) = 1

Expected info: .918*(3/5) + 1*(2/5) = .951

Winner is "humidity"

Page 26: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Tree so far

Page 27: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Continuing to split (for Outlook="Overcast")• Nothing to split here,

"play" is always "yes".

Outlook Temp Humidity Windy Play

Overcast Hot High False Yes

Overcast Cool Normal True Yes

Overcast Mild High True Yes

Overcast Hot Normal False Yes

Tree so far

Page 28: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Continuing to split (for Outlook="Rainy")Outlook Temp Humidity Windy Play

Rainy Mild High False Yes

Rainy Cool Normal False Yes

Rainy Cool Normal True No

Rainy Mild Normal False Yes

Rainy Mild High True No

• We can easily see that "Windy" is the one to choose. (Why?)

Page 29: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

The final decision tree

• Note: not all leaves need to be pure; sometimes identical instances have different classes

Splitting stops when data can’t be split any further

Page 30: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Information gain Sometimes people don’t use directly the entropy of a

node. Rather the information gain is being used.

Clearly, greater the information gain better the purity of a node. So, we choose “Outlook” for the root.

Page 31: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Highly-branching attributes• The weather data with ID code

Page 32: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Tree stump for ID code attribute

Page 33: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Highly-branching attributesSo, • Subsets are more likely to be pure if there is a large

number of values– Information gain is biased towards choosing attributes with a

large number of values

– This may result in overfitting (selection of an attribute that is non-optimal for prediction)

Page 34: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

The gain ratio• Gain ratio: a modification of the information gain that

reduces its bias

• Gain ratio takes number and size of branches into account

when choosing an attribute

– It corrects the information gain by taking the intrinsic

information of a split into account

• Intrinsic information: entropy (with respect to the attribute

on focus) of node to be split.

Page 35: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Computing the gain ratio

Page 36: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Gain ratios for weather data

Page 37: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

More on the gain ratio• “Outlook” still comes out top but “Humidity” is now a much

closer contender because it splits the data into two subsets instead of three.

• However: “ID code” has still greater gain ratio. But its advantage is greatly reduced.

• Problem with gain ratio: it may overcompensate– May choose an attribute just because its intrinsic information

is very low

– Standard fix: choose an attribute that maximizes the gain ratio, provided the information gain for that attribute is at least as great as the average information gain for all the attributes examined.

Page 38: Decision Trees. Example of a Decision Tree categorical continuous class Refund MarSt TaxInc YES NO YesNo Married Single, Divorced < 80K> 80K Splitting.

Discussion• Algorithm for top-down induction of decision trees (“ID3”)

was developed by Ross Quinlan (University of Sydney Australia)

• Gain ratio is just one modification of this basic algorithm– Led to development of C4.5, which can deal with numeric

attributes, missing values, and noisy data

• There are many other attribute selection criteria! (But almost no difference in accuracy of result.)