Cheat sheets › Chapter 6
Trees and ensembles cheat sheet
Split data with questions, then combine many models into one. Every lesson in one page: the key idea, the pseudocode and the terms.
Decision trees
A decision tree makes a prediction by asking a series of yes-or-no questions, like the game twenty questions. It starts with one question at the top.
ask: is income > 40k?
yes → ask: any missed payments?
yes → decline
no → approve
no → ask: has a guarantor?
yes → approve
no → decline
# learning = choosing the best questions, in the best orderTerms: leaf
Entropy and Gini
A tree wants groups that are as “pure” as possible: all one answer. impurity measures how mixed a group is. All teal: impurity 0. Half and half: as mixed as it gets.
impurity of a group = how mixed it is
pure group (all one class) → impurity 0
50 / 50 mix → impurity highest
for each possible question:
split the group, measure the impurity of each side
pick the question that leaves the purest groups
# entropy and Gini are two ways to measure impurityBagging
Draw random samples from the data, putting each row back after picking it. Some rows show up twice and others not at all. Each sample is a bootstrap bag.
models = []
repeat 3 times:
bag = random sample of the data, with replacement
models.add(train_tree(bag))
new example → each model votes
answer = majority vote (or the average for numbers)Random forests
A random forest is bagging with decision trees, plus one twist: at each question, a tree may only consider a few randomly chosen features. That stops every tree from leaning on the same strong clue.
forest = []
repeat 100 times:
bag = random sample of the rows
tree = train_tree(bag, each split looks at only a few random features)
forest.add(tree)
answer = majority vote of all the treesTerms: random forest
Boosting
Boosting starts with a very simple model, a weak learner: here, a single cut. It gets most points right, but a couple are on the wrong side.
weights = equal for every example
repeat for several rounds:
model = train_small_model(data, weights)
mistakes = examples this model got wrong
increase the weight of the mistakes
final answer = weighted vote of all the small modelsTerms: weak learner, boosting
Gradient boosting
Gradient boosting begins with the simplest possible guess for every house: the average price. The dashed lines show how far off that is.
prediction = the average price # start simple
repeat 100 times:
errors = true − prediction # what we still get wrong
tree = small tree that predicts the errors
prediction = prediction + 0.1 × tree # a small correction
# XGBoost, LightGBM and CatBoost are fast versions of thisTerms:
Revise with flashcards and quizzes. Free practice for every lesson in this chapter.
Open in the app