Lesson 2 · Unit 02
Decision trees
Pick the question that leaves the purest groups
What you practise
- A split is a yes/no question, Gini impurity and information gain computed for candidate splits, depth and leaf size as the overfitting knobs, reading a printed tree
Try a question from this lesson
Pick one
Eight emails: 4 spam, 4 not. Two yes/no questions could split them. Which question is more useful to ask first?
Split A: contains "free"?
yes: 4 spam, 0 not
no: 0 spam, 4 not
Split B: sent in the morning?
yes: 2 spam, 2 not
no: 2 spam, 2 notCorrect answer
A: each group is all one kind
Why
After split A you know the answer for every email. After split B each group is still a coin flip, so nothing was learned. A tree looks for the question that leaves its groups as pure as possible.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Gini impurity
A decision tree scores a group of rows by its impurity. The usual score is Gini:
gini = 1 - (sum of each class's share, squared)
A group that is all one class scores 0. An even mix of two classes scores 0.5, the worst a two-class group can do.
def gini(counts):
n = sum(counts)
ps = [c / n for c in counts]
return 1 - sum(p * p for p in ps)
print(gini([4, 0])) # 0.0
print(gini([2, 2])) # 0.510 more exercises in the app
- 4× type the answer
- 3× multiple choice
- 1× fill the blanks
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.