Python ML Gradient Boosting with scikit-learn
Gradient boosting is a method that builds many small trees in a row. Each new tree fixes the mistakes of the trees before it.
Fit a boosting model
GradientBoostingClassifier builds 100 small trees by default. The score shows how many test rows it gets right.
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
model = GradientBoostingClassifier(random_state=42)
model.fit(X_train, y_train)
print("Test accuracy:", round(model.score(X_test, y_test), 4))A fast learner learns more from each tree
learning_rate sets how much each tree counts. A rate of 0.5 beats a rate of 0.05 on this data.
for rate in [0.05, 0.5]:
m = GradientBoostingClassifier(learning_rate=rate, random_state=42)
m.fit(X_train, y_train)
print("Rate", rate, ":", round(m.score(X_test, y_test), 4))The most useful column
feature_importances_ gives each column a share of the splits. The top column holds the biggest share.
pairs = sorted(zip(data.feature_names, model.feature_importances_), key=lambda p: p[1], reverse=True)
print("Top feature:", pairs[0][0], round(float(pairs[0][1]), 4))Example
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.2, random_state=42)
model = GradientBoostingClassifier(random_state=42)
model.fit(X_train, y_train)
print("Test accuracy:", round(model.score(X_test, y_test), 4))
pairs = sorted(zip(data.feature_names, model.feature_importances_),
key=lambda pair: pair[1], reverse=True)
for name, value in pairs[:3]:
print(name, round(float(value), 4))Key points
- Boosting: a method that builds models in a row, each one fixing the last one's mistakes
- Weak learner: a small model that is only slightly better than guessing
- Use GradientBoostingClassifier to build a boosting model for yes-or-no answers
- Use n_estimators to choose the count of trees the model builds
- Use learning_rate to set how much each new tree counts
- Use feature_importances_ to see which columns the trees used most
In this example, the data comes from load_breast_cancer, which holds 569 rows of tumour measurements. train_test_split keeps a fifth of the rows for testing, with random_state=42 so the split repeats. GradientBoostingClassifier builds 100 small trees, and fit trains them in a row. score reports the test accuracy of 0.9561. The last lines sort the columns by feature_importances_ and print the top three. Mean concave points leads with a share of 0.4505. In short, boosting turns a row of weak trees into one strong model by aiming each tree at the errors that remain.
Note
More trees can memorise the training data, so check the test score as the trees grow.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What does each new tree in gradient boosting try to fix?