Bitpath

Lesson 2 · Unit 02

Decision trees

Pick the question that leaves the purest groups

12 exercisesabout 3 min

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 not

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.

Python
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.5

10 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.

One email when Bitpath is ready to try. Nothing else. Privacy