Processing math: 100%

17: Decision Trees


Motivation for Decision Trees

Often you don't care about the exact nearest neighbor, you just want to make a prediction. Nearest neighbor search is slow and requires a lot of storage O(nd).
New idea:
  1. Build a KD-type tree with only pure leaves
  2. Descent test point and make decision based on leaf label. Exact nearest neighbor is not really needed.

    Binary decision tree. Only labels are stored.

New goal: Build a tree that is:
  1. Maximally compact
  2. Only has pure leaves
Quiz: Is it always possible to find a consistent tree?
Yes, if and only if no two input vectors have identical features but different labels
Bad News! Finding a minimum size tree is NP-Hard!!
Good News: We can approximate it very effectively with a greedy strategy. We keep splitting the data to minimize an impurity function that measures label purity amongst the children.

Impurity Functions

Data: S={(x1,y1),,(xn,yn)},yi{1,,c}, where c is the number of classes

Gini impurity

Let SkS where Sk={(x,y)S:y=k} (all inputs with labels k)
S=S1Sc
Define: pk=|Sk||S|fraction of inputs in S with label k
Note: This is different from Gini coefficient. See Gini Coefficient of a leaf: G(S)=ck=1pk(1pk)

Fig: Gini Impurity Function

Gini impurity of a tree: GT(S)=|SL||S|GT(SL)+|SR||S|GT(SR) where:

Entropy

Let p1,,pk be defined as before. We know what we don't want (Uniform Distribution): p1=p2==pc=1c This is the worst case since each leaf is equally likely. Prediction is random guessing. Define the impurity as how close we are to uniform. Use KL-Divergence to compute "closeness"
Note: KL-Divergence is not a metric because it is not symmetric, i.e., KL(p||q)KL(q||p).
Let q1,,qc be the uniform label/distribution. i.e. qk=1ck KL(p||q)=ck=1pklogpkqk0KL-Divergence =kpklog(pk)pklog(qk) where qk=1c =kpklog(pk)+pklog(c) =kpklog(pk)+log(c)kpk where log(c)constant,kpk=1
maxpKL(p||q)=maxpkpklog(pk) =minpkpklog(pk) =minpH(s)Entropy
Entropy over tree: H(S)=pLH(SL)+pRH(SR) pL=|SL||S|,pR=|SR||S|

ID3-Algorithm

Base Cases: ID3(S):{if ˉy s.t. (x,y)S,y=ˉyreturn leaf  with label ˉyif ˉx s.t. (x,y)S,x=ˉxreturn leaf  with mode(y:(x,y)S) or mean (regression) The Equation above indicates the ID3 algorithm stop under two cases. The first case is that all the data points in a subset of have the same label. If this happens, we should stop splitting the subset and create a leaf with label y. The other case is there are no more attributes could be used to split the subset. Then we create a leaf and label it with the most common y.

Try all features and all possible splits. Pick the split that minimizes impurity (e.g. s>t) where ffeature and tthreshold
Recursion: Define: [SL={(x,y)S:xft}SR={(x,y)S:xft}]
Quiz: Why don't we stop if no split can improve impurity?
Example: XOR

Fig 4: Example XOR

Regression Trees

CART: Classification and Regression Trees

Assume labels are continuous: yiR
Impurity: Squared Loss L(S)=1|S|(x,y)S(yˉyS)2Average squared difference from average label where ˉyS=1|S|(x,y)SyAverage label At leaves, predict ˉyS. Finding best split only costs O(nlogn)

Fig: CART


CART summary: