Data Mining 2019 Classification Trees (1) Ad Feelders Universiteit - - PowerPoint PPT Presentation

data mining 2019 classification trees 1
SMART_READER_LITE
LIVE PREVIEW

Data Mining 2019 Classification Trees (1) Ad Feelders Universiteit - - PowerPoint PPT Presentation

Data Mining 2019 Classification Trees (1) Ad Feelders Universiteit Utrecht Ad Feelders ( Universiteit Utrecht ) Data Mining 1 / 45 Modeling: Data Mining Tasks Classification / Regression Dependency Modeling (Graphical Models; Bayesian


slide-1
SLIDE 1

Data Mining 2019 Classification Trees (1)

Ad Feelders

Universiteit Utrecht

Ad Feelders ( Universiteit Utrecht ) Data Mining 1 / 45

slide-2
SLIDE 2

Modeling: Data Mining Tasks

Classification / Regression Dependency Modeling (Graphical Models; Bayesian Networks) Frequent Pattern Mining (Association Rules) Subgroup Discovery (Rule Induction; Bump Hunting) Clustering Ranking

Ad Feelders ( Universiteit Utrecht ) Data Mining 2 / 45

slide-3
SLIDE 3

Classification

Predict the class of an object on the basis of some of its attributes. For example, predict: Good/bad credit for loan applicants, using

income age ...

Spam/no spam for e-mail messages, using

% of words matching a given word (e.g. “free”) use of CAPITAL LETTERS ...

Music Genre (Rock, Techno, Death Metal, ...) based on audio features and lyrics.

Ad Feelders ( Universiteit Utrecht ) Data Mining 3 / 45

slide-4
SLIDE 4

Building a classification model

The basic idea is to build a classification model using a set of training

  • examples. Each training example contains attribute values and the

corresponding class label. There are many techniques to do that: Statistical Techniques

Discriminant Analysis Logistic Regression

Data Mining/Machine Learning

Classification Trees Bayesian Network Classifiers Neural Networks Support Vector Machines ...

Ad Feelders ( Universiteit Utrecht ) Data Mining 4 / 45

slide-5
SLIDE 5

Strong and Weak Points of Classification Trees

Strong points: Are easy to interpret (if not too large). Select relevant attributes automatically. Can handle both numeric and categorical attributes. Weak point: Single trees are usually not among the top performers. However: Averaging multiple trees (bagging, random forests) can bring them back to the top! But ease of interpretation suffers as a consequence.

Ad Feelders ( Universiteit Utrecht ) Data Mining 5 / 45

slide-6
SLIDE 6

Example: Loan Data

Record age married?

  • wn house

income gender class 1 22 no no 28,000 male bad 2 46 no yes 32,000 female bad 3 24 yes yes 24,000 male bad 4 25 no no 27,000 male bad 5 29 yes yes 32,000 female bad 6 45 yes yes 30,000 female good 7 63 yes yes 58,000 male good 8 36 yes no 52,000 male good 9 23 no yes 40,000 female good 10 50 yes yes 28,000 female good

Ad Feelders ( Universiteit Utrecht ) Data Mining 6 / 45

slide-7
SLIDE 7

Credit Scoring Tree

5 5

bad good rec#

1…10

3

7,8,9

5 2

1…6,10

1 2

2,6,10

4

1,3,4,5

2

6,10

1

2 income > 36,000 income 36,000 age > 37 age 37 married not married

Ad Feelders ( Universiteit Utrecht ) Data Mining 7 / 45

slide-8
SLIDE 8

Cases with income > 36, 000

Record age married?

  • wn house

income gender class 1 22 no no 28,000 male bad 2 46 no yes 32,000 female bad 3 24 yes yes 24,000 male bad 4 25 no no 27,000 male bad 5 29 yes yes 32,000 female bad 6 45 yes yes 30,000 female good 7 63 yes yes 58,000 male good 8 36 yes no 52,000 male good 9 23 no yes 40,000 female good 10 50 yes yes 28,000 female good

Ad Feelders ( Universiteit Utrecht ) Data Mining 8 / 45

slide-9
SLIDE 9

Partitioning the attribute space

30 40 50 60 30 40 50 bad bad bad bad bad good good good good good

age income 36 37

Good Bad

Ad Feelders ( Universiteit Utrecht ) Data Mining 9 / 45

slide-10
SLIDE 10

Why not split on gender in top node?

5 5

bad good rec#

1…10

2 3

2,5,6,9,10 gender = male gender = female

3 2

1,3,4,7,8

Ad Feelders ( Universiteit Utrecht ) Data Mining 10 / 45

slide-11
SLIDE 11

Why not split on gender in top node?

5 5

bad good rec#

1…10

2 3

2,5,6,9,10 gender = male gender = female

3 2

1,3,4,7,8

Intuitively: learning the value of gender doesn’t provide much information about the class label.

Ad Feelders ( Universiteit Utrecht ) Data Mining 10 / 45

slide-12
SLIDE 12

Impurity of a node

We strive towards nodes that are pure in the sense that they only contain observations of a single class. We need a measure that indicates “how far” a node is removed from this ideal. We call such a measure an impurity measure.

Ad Feelders ( Universiteit Utrecht ) Data Mining 11 / 45

slide-13
SLIDE 13

Impurity function

The impurity i(t) of a node t is a function of the relative frequencies of the classes in that node: i(t) = φ(p1, p2, . . . , pJ) where the pj(j = 1, . . . , J) are the relative frequencies of the J different classes in node t. Sensible requirements of any quantification of impurity:

1 Should be at a maximum when the observations are distributed evenly

  • ver all classes.

2 Should be at a minimum when all observations belong to a single

class.

3 Should be a symmetric function of p1, . . . , pJ. Ad Feelders ( Universiteit Utrecht ) Data Mining 12 / 45

slide-14
SLIDE 14

Quality of a split (test)

We define the quality of binary split s in node t as the reduction of impurity that it achieves ∆i(s, t) = i(t) − {π(ℓ)i(ℓ) + π(r)i(r)} where ℓ is the left child of t, r is the right child of t, π(ℓ) is the proportion

  • f cases sent to the left, and π(r) the proportion of cases sent to the right.

t ℓ r π(ℓ) π(r) i(t) i(ℓ) i(r)

Ad Feelders ( Universiteit Utrecht ) Data Mining 13 / 45

slide-15
SLIDE 15

Well known impurity functions

Impurity functions we consider: Resubstitution error Gini-index (CART, Rpart) Entropy (C4.5, Rpart)

Ad Feelders ( Universiteit Utrecht ) Data Mining 14 / 45

slide-16
SLIDE 16

Resubstitution error

Measures the fraction of cases that is classified incorrectly if we assign every case in node t to the majority class in that node. That is i(t) = 1 − max

j

p(j|t) where p(j|t) is the relative frequency of class j in node t.

Ad Feelders ( Universiteit Utrecht ) Data Mining 15 / 45

slide-17
SLIDE 17

Resubstitution error: credit scoring tree

5 5 3

i = 0

5 2 1 2

i = 1/3

4

i = 0

2

i = 0

1

i = 0

i = 1/2 i = 2/7

Ad Feelders ( Universiteit Utrecht ) Data Mining 16 / 45

slide-18
SLIDE 18

Graph of resubstitution error for two-class case

p(0) 1-max(p(0),1-p(0)) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 0.0 0.1 0.2 0.3 0.4 0.5 Ad Feelders ( Universiteit Utrecht ) Data Mining 17 / 45

slide-19
SLIDE 19

Resubstitution error

Questions: Does resubstitution error meet the sensible requirements?

Ad Feelders ( Universiteit Utrecht ) Data Mining 18 / 45

slide-20
SLIDE 20

Resubstitution error

Questions: Does resubstitution error meet the sensible requirements? What is the impurity reduction of the second split in the credit scoring tree if we use resubstitution error as impurity measure?

Ad Feelders ( Universiteit Utrecht ) Data Mining 18 / 45

slide-21
SLIDE 21

Impurity Reduction

Impurity reduction of second split (using resubstitution error): ∆i(s, t) = i(t) − {π(ℓ)i(ℓ) + π(r)i(r)} = 2 7 − 3 7 × 1 3 + 4 7 × 0

  • = 2

7 − 1 7 = 1 7

Ad Feelders ( Universiteit Utrecht ) Data Mining 19 / 45

slide-22
SLIDE 22

Which split is better?

400400 300 100 100300

s1

400 400 200400 2000

s2

Ad Feelders ( Universiteit Utrecht ) Data Mining 20 / 45

slide-23
SLIDE 23

Which split is better?

400400 300 100 100300

s1

400 400 200400 2000

s2 These splits have the same resubstitution error, but s2 is commonly preferred because it creates a leaf node.

Ad Feelders ( Universiteit Utrecht ) Data Mining 20 / 45

slide-24
SLIDE 24

Class of suitable impurity functions

Problem: resubstitution error only decreases at a constant rate as the node becomes purer. We need an impurity measure which gives greater rewards to purer

  • nodes. Impurity should decrease at an increasing rate as the node

becomes purer. Hence, impurity should be a strictly concave function of p(0). We define the class F of impurity functions (for two-class problems) that has this property:

1 φ(0) = φ(1) = 0 (minimum at p(0) = 0 and p(0) = 1) 2 φ(p(0)) = φ(1 − p(0)) (symmetric) 3 φ′′(p(0)) < 0, 0 < p(0) < 1 (strictly concave) Ad Feelders ( Universiteit Utrecht ) Data Mining 21 / 45

slide-25
SLIDE 25

Impurity function: Gini index

For the two-class case the Gini index is i(t) = p(0|t)p(1|t) = p(0|t)(1 − p(0|t)) Question 1: Check that the Gini index belongs to F. Question 2: Check that if we use the Gini index, split s2 is indeed preferred. Note: The variance of a Bernoulli random variable with probability of success p is p(1 − p). Hence we are attempting to minimize the variance

  • f the class distribution.

Ad Feelders ( Universiteit Utrecht ) Data Mining 22 / 45

slide-26
SLIDE 26

Gini index: credit scoring tree

5 5 3

i = 0

5 2 1 2

i = 2/9

4

i = 0

2

i = 0

1

i = 0

i = 1/4 i = 10/49

Ad Feelders ( Universiteit Utrecht ) Data Mining 23 / 45

slide-27
SLIDE 27

Can impurity increase?

Is it possible that a split makes things worse, i.e. ∆i(s, t) < 0? Not if φ ∈ F. Because φ is a concave function, we have φ(p(0|ℓ)π(ℓ) + p(0|r)π(r)) ≥ π(ℓ)φ(p(0|ℓ)) + π(r)φ(p(0|r)) Since p(0|t) = p(0|ℓ)π(ℓ) + p(0|r)π(r) it follows that φ(p(0|t)) ≥ π(ℓ)φ(p(0|ℓ)) + π(r)φ(p(0|r))

Ad Feelders ( Universiteit Utrecht ) Data Mining 24 / 45

slide-28
SLIDE 28

Can impurity increase? Not if φ is concave.

p(0|ℓ) p(0|r) p(0|t) = π(ℓ)p(0|ℓ) + π(r)p(0|r) φ(p(0|ℓ)) φ(p(0|r)) φ(p(0|t)) π(ℓ)φ(p(0|ℓ)) + π(r)φ(p(0|r))

Ad Feelders ( Universiteit Utrecht ) Data Mining 25 / 45

slide-29
SLIDE 29

Split s1 and s2 with resubstitution error

Ad Feelders ( Universiteit Utrecht ) Data Mining 26 / 45

slide-30
SLIDE 30

Split s1 and s2 with Gini

Ad Feelders ( Universiteit Utrecht ) Data Mining 27 / 45

slide-31
SLIDE 31

Impurity function: Entropy

For the two-class case the entropy is i(t) = −p(0|t) log p(0|t) − p(1|t) log p(1|t) Question: Check that entropy impurity belongs to F. Remark: this is the average amount of information generated by drawing (with replacement) an example at random from this node, and observing its class.

Ad Feelders ( Universiteit Utrecht ) Data Mining 28 / 45

slide-32
SLIDE 32

Three impurity measures

p(0) 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 0.0 0.2 0.4 0.6 0.8 1.0

Entropy (solid), Gini (dot-dash) and resubstitution (dash) impurity.

Ad Feelders ( Universiteit Utrecht ) Data Mining 29 / 45

slide-33
SLIDE 33

The set of splits considered

1 Each split depends on the value of only a single attribute. 2 If attribute x is numeric, we consider all splits of type x ≤ c where c

is (halfway) between two consecutive values of x in their sorted order.

3 If attribute x is categorical, taking values in {b1, b2, . . . , bL}, we

consider all splits of type x ∈ S, where S is any non-empty proper subset of {b1, b2, . . . , bL}.

Ad Feelders ( Universiteit Utrecht ) Data Mining 30 / 45

slide-34
SLIDE 34

Splits on numeric attributes

There is only a finite number of distinct splits, because there are at most n distinct values of a numeric attribute in the training sample (where n is the number of examples in the training sample). Example: possible splits on income in the root for the loan data Income Class Quality (split after) 0.25− 24 B 0.1(1)(0)+0.9(4/9)(5/9) = 0.03 27 B 0.2(1)(0) + 0.8 (3/8)(5/8) = 0.06 28 B,G 0.4(3/4)(1/4) + 0.6(2/6)(4/6) = 0.04 30 G 0.5(3/5)(2/5) + 0.5(2/5)(3/5) = 0.01 32 B,B 0.7(5/7)(2/7) + 0.3(0)(1) = 0.11 40 G 0.8(5/8)(3/8) + 0.2(0)(1) = 0.06 52 G 0.9(5/9)(4/9) + 0.1(0)(1) = 0.03 58 G

Ad Feelders ( Universiteit Utrecht ) Data Mining 31 / 45

slide-35
SLIDE 35

Splits on a categorical attribute

For a categorical attribute with L distinct values there are 2L−1 − 1 distinct splits to consider. Why?

Ad Feelders ( Universiteit Utrecht ) Data Mining 32 / 45

slide-36
SLIDE 36

Splits on a categorical attribute

For a categorical attribute with L distinct values there are 2L−1 − 1 distinct splits to consider. Why? There are 2L − 2 non-empty proper subsets of {b1, b2, . . . , bL}. But a subset and the complement of that subset result in the same split, so we should divide this number by 2.

Ad Feelders ( Universiteit Utrecht ) Data Mining 32 / 45

slide-37
SLIDE 37

Splitting on categorical attributes: shortcut

For two-class problems, and φ ∈ F, we don’t have to check all 2L−1 − 1 possible splits. Sort the p(0|x = bℓ), that is, p(0|x = bℓ1) ≤ p(0|x = bℓ2) ≤ . . . ≤ p(0|x = bℓL) Then one of the L − 1 subsets {bℓ1, . . . , bℓh}, h = 1, . . . , L − 1, is the optimal split. Thus the search is reduced from computing 2L−1 − 1 splits to computing only L − 1 splits.

Ad Feelders ( Universiteit Utrecht ) Data Mining 33 / 45

slide-38
SLIDE 38

Splitting on categorical attributes: example

Let x be a categorical attribute with possible values a, b, c, d. Suppose p(0|x = a) = 0.6, p(0|x = b) = 0.4, p(0|x = c) = 0.2, p(0|x = d) = 0.8 Sort the values of x according to probability of class 0 c b a d We only have to consider the splits: {c}, {c, b}, and {c, b, a}. Intuition: put values with low probability of class 0 in one group, and values with high probability of class 0 in the other.

Ad Feelders ( Universiteit Utrecht ) Data Mining 34 / 45

slide-39
SLIDE 39

Splitting on numerical attributes: shortcut

Income Class Quality (split after) 0.25− 24 B 0.1(1)(0)+0.9(4/9)(5/9) = 0.03 27 B 0.2(1)(0) + 0.8 (3/8)(5/8) = 0.06 28 B,G 0.4(3/4)(1/4) + 0.6(2/6)(4/6) = 0.04 30 G 0.5(3/5)(2/5) + 0.5(2/5)(3/5) = 0.01 32 B,B 0.7(5/7)(2/7) + 0.3(0)(1) = 0.11 40 G 0.8(5/8)(3/8) + 0.2(0)(1) = 0.06 52 G 0.9(5/9)(4/9) + 0.1(0)(1) = 0.03 58 G Optimal split can only occur between consecutive values with different class distributions.

Ad Feelders ( Universiteit Utrecht ) Data Mining 35 / 45

slide-40
SLIDE 40

Splitting on numerical attributes

Income Class Quality (split after) 0.25− 24 B 27 B 0.2(1)(0) + 0.8 (3/8)(5/8) = 0.06 28 B,G 0.4(3/4)(1/4) + 0.6(2/6)(4/6) = 0.04 30 G 0.5(3/5)(2/5) + 0.5(2/5)(3/5) = 0.01 32 B,B 0.7(5/7)(2/7) + 0.3(0)(1) = 0.11 40 G 52 G 58 G Optimal split can only occur between consecutive values with different class distributions.

Ad Feelders ( Universiteit Utrecht ) Data Mining 36 / 45

slide-41
SLIDE 41

Segment borders: numeric example

A segment is a block of consecutive values of the split attribute for which the class distribution is identical. Optimal splits can only occur at segment borders. Consider the following data on numeric attribute x and class label y. The class label can take on two different values, coded as A and B. x 8 8 12 12 14 16 16 18 20 20 y A B A B A A A A A B The class probabilities (relative frequencies) are: x 8 12 14 16 18 20 P(A) 0.5 0.5 1 1 1 0.5 P(B) 0.5 0.5 0.5 So we obtain the segments: (8, 12), (14, 16, 18) and (20). Only consider the splits: x ≤ 13 and x ≤ 19 Ignore: x ≤ 10, x ≤ 15 and x ≤ 17

Ad Feelders ( Universiteit Utrecht ) Data Mining 37 / 45

slide-42
SLIDE 42

Optimal splits of gini index

Theorem The gini index optimal splits can only occur on segment borders. Consider the two-class case and binary splits. Let B be a segment, and let A be everything to the left of B, and C everything to the right of B. We show that the optimal split cannot occur inside B. Define: a: the number of cases in part A. a1: the number of cases in part A belonging to class 1. b: the number of cases in segment B. p1: the relative frequency of class 1 in segment B. ℓ: the number of cases from segment B sent to the left by the split. ℓ ∈ [0, b].

Ad Feelders ( Universiteit Utrecht ) Data Mining 38 / 45

slide-43
SLIDE 43

Optimal splits of gini index

A B C

L R

We perform a binary split into a left part L and a right part R. ℓ denotes the number of cases of segment B that goes to the left. Wherever we split inside B, the class distribution of the part of B that goes to the left (right) is the same, and has probability of class 1 equal to p1.

Ad Feelders ( Universiteit Utrecht ) Data Mining 39 / 45

slide-44
SLIDE 44

Optimal splits of gini index

Note that the probability of class 1 in the left part is given by pL = a1 + ℓp1 a + ℓ So the impurity of the left group as a function of ℓ is given by i(L) = pL(1 − pL) = pL − p2

L = a1 + ℓp1

a + ℓ − a1 + ℓp1 a + ℓ 2 The weighted average of the gini index of the child nodes is given by: NL N i(L) + NR N i(R), where NL is the number of cases sent to the left, etc. Note that we want to minimize this weighted average.

Ad Feelders ( Universiteit Utrecht ) Data Mining 40 / 45

slide-45
SLIDE 45

Optimal splits of gini index

The contribution of the left part is (ignore constant 1

N ):

f (ℓ) = NL × i(L) = (a + ℓ) a1 + ℓp1 a + ℓ − (a1 + ℓp1)2 (a + ℓ)2

  • = (a1 + ℓp1) − (a1 + ℓp1)2

a + ℓ We show that this is a concave function of ℓ, which implies that the minimum is attained either for ℓ = 0, or ℓ = b. The second derivative with respect to ℓ is given by f ′′(ℓ) = −2 (ap1 − a1)2 (a + ℓ)3 ≤ 0 The second derivative is negative everywhere, so the function is indeed concave.

Ad Feelders ( Universiteit Utrecht ) Data Mining 41 / 45

slide-46
SLIDE 46

Optimal splits of gini index

1 By symmetry, the contribution of the right child to the weighted

average is also a concave function of ℓ, and therefore the average gini index as a whole is a concave function of ℓ.

2 Hence, it attains its minimum for ℓ = 0, or ℓ = b (i.e. at the segment

borders), so the optimal split can never occur inside segment B.

3 This result is true for arbitrary concave impurity measures (e.g.

entropy) and generalizes to arbitrary number of classes.

Ad Feelders ( Universiteit Utrecht ) Data Mining 42 / 45

slide-47
SLIDE 47

Weighted average of gini index

Numeric example with a = 50, a1 = 10, b = 60, p1 = 0.8,c = 30,c1 = 10.

10 20 30 40 50 60 0.21 0.22 0.23 0.24 0.25 gini−index

Ad Feelders ( Universiteit Utrecht ) Data Mining 43 / 45

slide-48
SLIDE 48

Caveat

1 In the first practical assignment we use the parameters

nmin and minleaf to stop tree growing early.

2 A split is not allowed to produce a child node with

less than minleaf observations.

3 The segment borders algorithm doesn’t combine very well

with the minleaf constraint.

4 Better use the “brute force” approach in the assignment. Ad Feelders ( Universiteit Utrecht ) Data Mining 44 / 45

slide-49
SLIDE 49

Basic Tree Construction Algorithm (control flow)

Construct tree nodelist ← {{training data}} Repeat current node ← select node from nodelist nodelist ← nodelist − current node if impurity(current node) > 0 then S ← set of candidate splits in current node s* ← arg maxs∈S impurity reduction(s,current node) child nodes ← apply(s*,current node) nodelist ← nodelist ∪ child nodes fi Until nodelist = ∅

Ad Feelders ( Universiteit Utrecht ) Data Mining 45 / 45