Module 13 · Decision trees and random forests · Lesson 51 of 66
Trees and forests
Split data with yes-or-no questions, draw the tree, then let a whole forest vote.
In this lesson · about 25 minutes
- Explain how a decision tree splits data
- Train and draw a DecisionTreeClassifier
- Explain why a random forest usually beats a single tree
A decision tree classifies by asking a series of yes-or-no questions about the features: Is the patient older than 55 months? Did the surgery start below vertebra 14? Each question splits the data in two, and the tree keeps splitting until the groups are mostly one class. Training picks, at every step, the question that best separates the classes (measured by Gini impurity or entropy).
Trees are easy to read, but a single tree tends to memorize its training data. A random forest trains many trees, each on a random sample of the rows and with a random subset of features to choose from at each split, and lets them vote. The trees make different mistakes, so the vote is more reliable than any one of them.
The data
81 children who had spinal surgery. Kyphosis says whether a spinal curvature was present after the operation; Age is in months, Number is how many vertebrae were involved, and Start is the first vertebra operated on.
Exercise
Train a tree
The data is split for you. Create a DecisionTreeClassifier(random_state=101) called dtree, fit it, and save its test predictions as predictions.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('kyphosis.csv')
from sklearn.model_selection import train_test_split
X = df.drop('Kyphosis', axis=1)
y = df['Kyphosis']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=101)Draw the tree
Exercise
Grow a forest
Now train a RandomForestClassifier(n_estimators=100, random_state=101) called rfc and save its predictions as rfc_pred. Print the confusion matrix.
Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_csv('kyphosis.csv')
from sklearn.model_selection import train_test_split
X = df.drop('Kyphosis', axis=1)
y = df['Kyphosis']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=101)Which features matter?
Exercises solved: 0 of 2
Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.