48
1 Decision Tree Learning

1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Embed Size (px)

Citation preview

Page 1: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

1

Decision Tree Learning

Page 2: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Decision Trees

• Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch for each value of the feature, and leaves specify the category.

• Can represent arbitrary conjunction and disjunction. Can represent any classification function over discrete feature vectors.

• Can be rewritten as a set of rules, i.e. disjunctive normal form (DNF).– red circle → pos– red circle → A blue → B; red square → B green → C; red triangle → C

color

red blue green

shape

circle square triangleneg pos

pos neg neg

color

red blue green

shape

circle square triangle B C

A B C

Page 3: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Buy a present: another toy example

3

Page 4: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

A more realistic example: buy or build or stay?

4

Page 5: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Project investment (The project portfolio is

optimized against a constraint (budget available).

5

Page 6: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

6

Properties of Decision Tree Learning

• Continuous (real-valued) features can be handled by allowing nodes to split a real valued feature into two ranges based on a threshold (e.g. length < 3 and length 3)

• Classification trees have discrete class labels at the leaves, regression trees allow real-valued outputs at the leaves.

• Algorithms for finding consistent trees are efficient for processing large amounts of training data for data mining tasks.

• Methods developed for handling noisy training data (both class and feature noise).

• Methods developed for handling missing feature values.

Page 7: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

7

Top-Down Decision Tree Induction

• Recursively build a tree top-down by divide and conquer.

<big, red, circle>: + <small, red, circle>: +<small, red, square>: <big, blue, circle>:

color

red blue green

<big, red, circle>: + <small, red, circle>: +<small, red, square>:

Page 8: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

8

shape

circle square triangle

Top-Down Decision Tree Induction

• Recursively build a tree top-down by divide and conquer.

<big, red, circle>: + <small, red, circle>: +<small, red, square>: <big, blue, circle>:

<big, red, circle>: + <small, red, circle>: +<small, red, square>:

color

red blue green

<big, red, circle>: + <small, red, circle>: +

pos<small, red, square>:

neg pos

<big, blue, circle>: neg neg

How do we decide the order in which we test attributes?How do we decide the class of nodes for which we have no examples?How do we decide the order in which we test attributes?How do we decide the class of nodes for which we have no examples?

Let’s ignore for now these 2 issues and describe the algorithm first

Page 9: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

9

Decision Tree Induction Pseudocode

DTree(examples, features) returns a tree a)If all examples are in one category, return a leaf node with that category label. b)Else if the set of features is empty, return a leaf node with the category label that is the most common in examples. Else pick a feature F and create a node R for it For each possible value vi of F: Let examplesi be the subset of examples that have value vi for F

Add an out-going edge E to node R labeled with the value vi.

If examplesi is empty then attach a leaf node to edge E labeled with the category that is the most common in examples. else call DTree(examplesi , features – {F}) and attach the resulting tree as the subtree under edge E. Return the subtree rooted at R.

a) and b) are the termination conditions a) and b) are the termination conditions e.g.”color”e.g.”color” e.g. color=rede.g. color=red

color

red

Page 10: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

10

Example (1)

Examples: <big, red, circle>: + <small, red, circle>: +<small, red, square>: <big, blue, circle>:

color

red<big, red, circle>: + <small, red, circle>: +<small, red, square>:

pick a feature F and create a node R for it, eg. color

For each possible value vi of F:Let examplesi be the subset of examples that have value vi for FAdd an out-going edge E to node R labeled with the value vi.

color

if (…) else call DTree(examplesi , features – {F}) and attach the resulting tree as the subtree under edge E.

Dtree( , <dimension,shape>)<big, red, circle>: + <small, red, circle>: +<small, red, square>:

Page 11: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Example (2)

11

DTree( , <dimension,shape>)<big, red, circle>: + <small, red, circle>: +<small, red, square>:

If all examples are in one category, return a leaf node with that category label.

color

redshape

circle<big, red, circle>: + <small, red, circle>: +

pick a feature F and create a node R for it, eg. shape

positive<small, red, square>: negative

squaretriangle

If examplesi is empty. then attach a leaf node to edge E labeled with the category thatis the most common in examples.

positive

Page 12: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

12

shape

circle square triangle

Example (3)

<big, blue, circle>:

<big, red, circle>: + <small, red, circle>: +<small, red, square>:

color

red blue green

<big, red, circle>: + <small, red, circle>: +

pos<small, red, square>:

neg pos

<big, blue, circle>: neg neg

Now we know how to decide the class when we have no examples, but how do we decide the order in which we create nodes? Now we know how to decide the class when we have no examples, but how do we decide the order in which we create nodes?

Page 13: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

13

Picking a Good Split Feature

• Goal is to have the resulting tree be as small as possible, per Occam’s razor.

• Finding a minimal decision tree (nodes, leaves, or depth) is an NP-hard optimization problem.

• Top-down divide-and-conquer method does a greedy search for a simple tree but does not guarantee to find the smallest.– General lesson in ML: “Greed is good.”

• Want to pick a feature that creates subsets of examples that are relatively “pure” in a single class so they are “closer” to being leaf nodes.

• There are a variety of heuristics for picking a good test, a popular one is based on information gain that originated with the ID3 system of Quinlan (1979).

Page 14: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

14

Entropy

• Entropy (disorder, impurity) of a set of examples, S, relative to a binary classification is:

where p1 is the fraction of positive examples in S and p0 is the fraction of negatives. (Notice that Entropy(S) is an estimate of the population entropy)

• If all examples are in one category, entropy is zero (we define 0log(0)=0)

• If examples are equally mixed (p1=p0=0.5), entropy is a maximum of 1.

• Entropy can be viewed as the number of bits required on average to encode the class of an example in S. It is also an estimate of the initial “disorder” or “uncertainty” about a classification, given the set S.

• For multi-class problems with c categories, entropy generalizes to:

)(log)(log)( 020121 ppppSEntropy

c

iii ppSEntropy

12 )(log)(

Page 15: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

15

Entropy Plot for Binary Classification

Page 16: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

16

Information Gain

• The information gain of a feature F (as we have seen) is the expected reduction in entropy resulting from splitting on this feature.

where Sv is the subset of S having value v for feature F.

• Entropy of each resulting subset weighted by its relative size.

• Example:– <big, red, circle>: + <small, red, circle>: +

– <small, red, square>: <big, blue, circle>:

)()(),()(

vFValuesv

v SEntropyS

SSEntropyFSGain

2+, 2 : E=1 size

big small1+,1 1+,1E=1 E=1

Gain=1(0.51 + 0.51) = 0

2+, 2 : E=1 color

red blue2+,1 0+,1E=0.918 E=0

Gain=1(0.750.918 + 0.250) = 0.311

2+, 2 : E=1 shape

circle square2+,1 0+,1E=0.918 E=0

Gain=1(0.750.918 + 0.250) = 0.311

Page 17: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

New pseudo-code

17

DTree(examples, features) returns a tree a)If all examples are in one category, return a leaf node with that category label. b)Else if the set of features is empty, return a leaf node with the category label that is the most common in examples. Else pick the best feature F according to IG and create a node R for it For each possible value vi of F: Let examplesi be the subset of examples that have value vi for F

Add an out-going edge E to node R labeled with the value vi.

If examplesi is empty then attach a leaf node to edge E labeled with the category that is the most common in examples. else call DTree(examplesi , features – {F}) and attach the resulting tree as the subtree under edge E. Return the subtree rooted at R.

Page 18: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

A complete example

Page 19: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

A Decision Tree for weather dataThe weather data example.

ID code Outlook Temperature Humidity Windy Play

a

b

c

d

e

f

g

h

i

j

k

l

m

n

Sunny

Sunny

Overcast

Rainy

Rainy

Rainy

Overcast

Sunny

Sunny

Rainy

Sunny

Overcast

Overcast

Rainy

Hot

Hot

Hot

Mild

Cool

Cool

Cool

Mild

Cool

Mild

Mild

Mild

Hot

Mild

High

High

High

High

Normal

Normal

Normal

High

Normal

Normal

Normal

High

Normal

High

False

True

False

False

False

True

True

False

False

False

True

True

False

True

No

No

Yes

Yes

Yes

No

Yes

No

Yes

Yes

Yes

Yes

Yes

No

Page 20: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

The Process of Constructing a Decision Tree

• Select an attribute to place at the root of the decision tree and make one branch for every possible value.

• Repeat the process recursively for each branch.

Page 21: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Information Gained by Knowing the Result of a Decision

• In the weather data example, there are 9 instances of which the decision to play is “yes” and there are 5 instances of which the decision to play is “no’. Then, the information gained by knowing the result of the decision is

Page 22: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

The General Form for Calculating the Information Gain

• Entropy of a decision =

P1, P2, …, Pn are the probabilities of the n possible outcomes.

nn PPPPPP logloglog 2211

Page 23: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Information Further Required If “Outlook” Is Placed at the Root

Outlook

yesyesnonono

yesyesyesyes

yesyesyesnono

sunny overcast rainy

.693.0971.014

50

14

4971.0

14

5

requiredfurther nInformatio

bits

Page 24: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Information Gained by Placing Each of the 4 Attributes

• Gain(outlook) = 0.940 bits – 0.693 bits

= 0.247 bits.

• Gain(temperature) = 0.029 bits.

• Gain(humidity) = 0.152 bits.

• Gain(windy) = 0.048 bits.

Page 25: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

The Strategy for Selecting an Attribute to Place at a Node

• Select the attribute that gives us the largest information gain.

• In this example, it is the attribute “Outlook”.

Outlook

2 “yes” 3 “no”

4 “yes” 3 “yes”2 “no”

sunny overcast rainy

Page 26: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

The Recursive Procedure for Constructing a Decision Tree

• Apply to each branch recursively to construct the decision tree.

• For example, for the branch “Outlook = Sunny”, we evaluate the information gained by applying each of the remaining 3 attributes.– Gain(Outlook=sunny;Temperature) = 0.971 – 0.4 =

0.571

– Gain(Outlook=sunny;Humidity) = 0.971 – 0 = 0.971

– Gain(Outlook=sunny;Windy) = 0.971 – 0.951 = 0.02

Page 27: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Recursive selection

• Similarly, we also evaluate the information gained by applying each of the remaining 3 attributes for the branch “Outlook = rainy”.– Gain(Outlook=rainy;Temperature) = 0.971 –

0.951 = 0.02– Gain(Outlook=rainy;Humidity) = 0.971 – 0.951

= 0.02– Gain(Outlook=rainy;Windy) =0.971 – 0 =

0.971

Page 28: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

The resulting tree

28

Outlook

humidity windyyes

no yesyes no

sunny overcast rainy

high normal false true

Page 29: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

DT can be represented as a set of rules

29

Outlook

humidity windyyes

no yesyes no

sunny overcast rainy

high normal false true

IF Outlook=sunny AND humidity=high noIF Outlook=sunny AND humidity=normal yesIF Outlook=overcast yesIF Outlook=rainy AND windy=false yesIF Outlook=rainy AND windy=true no

Page 30: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Support and Confidence

30

Each rule as a support |Sv|/|S| represented by the set of examples that fit the ruleEach rule has also a confidence which might or might not be equal to support.

Remember one of the 2 “exit” conditions in the algorithm:Else if the set of features is empty, return a leaf node with the category label that is the most common in examples.Hence the set of examples |Sv| does NOT has a uniform classification, but, say,|Sv+| positive and |Sv-| negative, if |Sv+| >|Sv-| class is pos, support is |Sv+|/|S| andconfidence is |Sv+| /|Sv|

Page 31: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Example

31

Outlook

2 “yes” 3 “no”

4 “yes” 3 “yes”2 “no”

sunny overcast rainy

IF Outlook=sunny THEN no Support=3/14Confidence=3/5

Page 32: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

32

Hypothesis Space Search in DL

• Performs batch learning that processes all training instances at once rather than incremental learning (e.g. like VS) that updates a hypothesis after each example.

• Performs hill-climbing (greedy search) that may only find a locally-optimal solution. Guaranteed to find a tree consistent with any conflict-free training set (i.e. identical feature vectors always assigned the same class), but not necessarily the simplest tree.

• Finds a single discrete hypothesis, so there is no way to provide confidences or create useful queries.

Page 33: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

33

Bias in Decision-Tree Induction

• Information-gain gives a bias for trees with minimal depth.

• Implements a search (preference) bias instead of a language (restriction) bias (e.g. conjunctive hypotheses only as for VS).

Page 34: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

34

History of Decision-Tree Research

• Hunt and colleagues use exhaustive search decision-tree methods (CLS) to model human concept learning in the 1960’s.

• In the late 70’s, Quinlan developed ID3 with the information gain heuristic to learn expert systems from examples.

• Simulataneously, Breiman and Friedman and colleagues develop CART (Classification and Regression Trees), similar to ID3.

• In the 1980’s a variety of improvements are introduced to handle noise, continuous features, missing features, and improved splitting criteria. Various expert-system development tools results.

• Quinlan’s updated decision-tree package (C4.5) released in 1993.

• Weka includes Java version of C4.5 called J48.

Page 35: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

35

Computational Complexity

• Worst case builds a complete tree where every path test every feature. Assume n examples and m features.

• At each level, i, in the tree, must examine the remaining m i features for each instance at that level to calculate info gains.

• However, learned tree is rarely complete (number of leaves is n). In practice, complexity is linear in both number of features (m) and number of training examples (n).

F1

Fm

Maximum of n examples spread acrossall nodes at each of the m levels

)(1

2

m

i

nmOni

Page 36: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

36

Overfitting

• Learning a tree that classifies the training data perfectly may not lead to the tree with the best generalization to unseen data.– There may be noise in the training data that the tree is erroneously

fitting.– The algorithm may be making poor decisions towards the leaves of the

tree that are based on very little data and may not reflect reliable trends.

• A hypothesis, h, is said to overfit the training data is there exists another hypothesis, h´, such that h has less error than h´ on the training data but greater error on independent test data.

hypothesis complexity

accu

racy

on training data

on test data

Page 37: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Overfitting example (1)

37

Page 38: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

38

Overfitting Example (2)

voltage (V)

curr

ent (

I)

Testing Ohms Law: V = IR (I = (1/R)V)

Ohm was wrong, we have found a more accurate function!

Perfect fit to training data with an 9th degree polynomial(can fit n points exactly with an n-1 degree polynomial)

Experimentallymeasure 10 points

Fit a curve to theResulting data.

Page 39: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

39

Overfitting Example (3)

voltage (V)

curr

ent (

I)

Testing Ohms Law: V = IR (I = (1/R)V)

Better generalization with a linear functionthat fits training data less accurately.

Page 40: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

40

Overfitting Noise in Decision Trees

• Category or feature noise can easily cause overfitting.– Add noisy instance <medium, blue, circle>: pos (but really neg)

shape

circle square triangle

color

red bluegreen

pos neg pos

neg neg

Page 41: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

41

Overfitting Noise in Decision Trees

• Category or feature noise can easily cause overfitting.– Add noisy instance <medium, blue, circle>: pos (but really neg)

shape

circle square triangle

color

red bluegreen

pos neg pos

neg

<big, blue, circle>: <medium, blue, circle>: +

small med big

posneg neg

• Conflicting examples can also arise if the features are incomplete and inadequate to determine the class or if the target concept is non-deterministic.

Page 42: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Example (inadequate features)

42

X(has-4-legs, has-tail, has-long-nose, has-big-ears,can-be-ridden) Classes: horse, camel, elephant

The missing feature has-hump does not allow to correctly classify a camel

Page 43: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

43

Overfitting Prevention (Pruning) Methods

• Two basic approaches for decision trees– Prepruning: Stop growing tree as some point during top-down

construction when there is no longer sufficient data to make reliable decisions (e.g. |Svi|<k).

– Postpruning: Grow the full tree, then remove subtrees that do not have sufficient evidence.

• Label leaf resulting from pruning with the majority class of the remaining data, or a class probability distribution.

• Method for determining which subtrees to prune:– Cross-validation: Reserve some training data as a hold-out set

(validation set, tuning set) to evaluate utility of subtrees.– Statistical test: Use a statistical test on the training data to determine

if any observed regularity can be dismisses, as likely due to random chance.

– Minimum description length (MDL): Determine if the additional complexity of the hypothesis is less complex than just explicitly remembering any exceptions resulting from pruning (e.g. for mammals: wings=false, except for bats).

Page 44: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

44

Reduced Error Pruning

• A post-pruning, cross-validation approach.

Partition training data D in “learning” L and “test” T sets.Build a complete tree from the L data.Until accuracy on validation set decreases do: For each non-leaf node, n, in the tree do: Temporarily prune the subtree below n and replace it with a leaf labeled with the current majority class at that node. Measure and record the accuracy of the pruned tree on the validation set. Permanently prune the node that results in the greatest increase in accuracy on the validation set.

Page 45: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Example

45

triangle

blueshape

circle square

red green

pos neg pos

neg

<big, blue, circle>: <medium, blue, circle>: +<small, blue, circle>:

<big, blue, circle>: <medium, blue, circle>: +<small, blue, circle>:

small med big

posneg neg

neg

Majority of “removed” examples under the pruned sub-tree is neg

Page 46: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

46

Issues with Reduced Error Pruning

• The problem with this approach is that it potentially “wastes” training data on the validation set.

• Severity of this problem depends where we are on the learning curve:

test

acc

urac

y

number of training examples

A learning curve shows the accuracy, e.g. the learning rate, as the number of training examples grows. A learning curve shows the accuracy, e.g. the learning rate, as the number of training examples grows.

Page 47: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

47

Cross-Validating without Loosing Training Data

• If the algorithm is modified to grow trees breadth-first rather than depth-first (e.g. test all values of a feature first and create all nodes at level i), we can stop growing after reaching any specified tree complexity.

• First, run several trials of reduced error-pruning using different random splits of learning and test sets.

• Record the complexity of the pruned tree learned in each trial. Let C be the average pruned-tree complexity (nodes+ edges).

• Grow a final tree breadth-first from all the training data but stop when the complexity reaches C.

• Similar cross-validation approach can be used to set arbitrary algorithm parameters in general.

Page 48: 1 Decision Tree Learning. Decision Trees Tree-based classifiers for instances represented as feature-vectors. Nodes test features, there is one branch

Summary

• Decision tree algorithm

• Ordering nodes (Information Gain)

• Testing and pruning

48