Decision Trees¶
Exercise: Place the concepts¶
Key Concepts¶
concept |
description |
|---|---|
decision tree |
a classification model constructing questions |
root node |
the first question a tree asks |
leaf node |
the result of the last question |
depth |
number of consecutive questions |
CART |
name of the tree construction algorithm |
GINI |
a metric for the purity of nodes |
The GINI index¶
The GINI index is used to evaluate which partition results in the best separation of the classes K:
Where p indicates the proportion of points belonging to class K.
Which is the best possible GINI score, which the worst?
The CART Algorithm¶
finds out which if statements give the best separation
it is a divide-and-conquer algorithm
it is a greedy algorithm
as a metric, the GINI purity score
The Algorithm
For each feature, determine the best threshold for splitting
Determine the best feature-threshold pair
Create two child nodes
Split the data points across child nodes
For each node, start again from 1.
If subset is pure or max_depth is reached STOP
Decision Trees in Scikit-learn¶
from sklearn.tree import DecisionTreeClassifier
m = DecisionTreeClassifier(max_depth=4)
m.fit(Xtrain, ytrain)
print(m.score(Xtrain, ytrain))
print(m.score(Xval, yval))
Hint
Try different values for the max_depth hyperparameter. What effect does it have on the score?
Plotting Decision Trees¶
One advantage of Decision Trees is that scikit can plot them easily:
from sklearn.tree import plot_tree
plot_tree(model)
Recap Questions¶
what is a binary tree?
what do the internal nodes of a decision tree contain?
what do the leaves (external nodes) of a tree contain?
what is node impurity? How can it be measured?
What is the minimum and maximum depth of a Decision Tree trained on one million data points?