Python ML XGBoost
XGBoost is a fast boosting library that builds small trees in a row. It is the tool behind many competition-winning models.
XGBoost in four lines
XGBClassifier works like a scikit-learn model. Fit it, then score it.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
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 = XGBClassifier(n_estimators=50, random_state=42)
model.fit(X_train, y_train)
print("Test accuracy:", round(model.score(X_test, y_test), 4))Stop early when the score stalls
early_stopping_rounds=10 stops training after ten rounds with no gain. The best round is the round the model kept.
X_train, X_temp, y_train, y_temp = train_test_split(data.data, data.target, test_size=0.3, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(X_temp, y_temp, test_size=0.5, random_state=42)
early = XGBClassifier(n_estimators=200, early_stopping_rounds=10, random_state=42)
early.fit(X_train, y_train, eval_set=[(X_val, y_val)], verbose=False)
print("Best round:", early.best_iteration)
print("Test accuracy:", round(early.score(X_test, y_test), 4))The most useful column
XGBoost ranks columns by how much they split the data. One column leads by a wide margin.
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.model_selection import train_test_split
from xgboost import XGBClassifier
data = load_breast_cancer()
X_train, X_temp, y_train, y_temp = train_test_split(
data.data, data.target, test_size=0.3, random_state=42)
X_val, X_test, y_val, y_test = train_test_split(
X_temp, y_temp, test_size=0.5, random_state=42)
model = XGBClassifier(n_estimators=200, early_stopping_rounds=10,
random_state=42)
model.fit(X_train, y_train, eval_set=[(X_val, y_val)], verbose=False)
print("Best round:", model.best_iteration)
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
- XGBoost: a fast library for gradient boosting, short for Extreme Gradient Boosting
- Use XGBClassifier for yes-or-no answers and XGBRegressor for numbers
- Use n_estimators to set the most trees the model may build
- Early stopping: a rule that stops training when the score stops improving
- Validation data: rows the model never trains on, used only to check progress
- Use early_stopping_rounds to set how many flat rounds to wait before stopping
- Use eval_set to give the model validation data for checking progress while it trains
- Use feature_importances_ to rank the columns by how much the trees used them
In this example, the data is split twice. The first split holds back 30 percent of the rows. The second split cuts that piece in half, into validation rows and test rows. XGBClassifier may build up to 200 trees, but early_stopping_rounds=10 stops it after ten rounds with no gain on the validation rows. It stops at round 23, which is the best round it kept. The test accuracy is 0.9535. The last lines rank the columns by feature_importances_, and mean concave points leads with 0.4833. In short, XGBoost is gradient boosting with speed tricks and a built-in rule for stopping early.
Note
Install XGBoost first with pip install xgboost. The lesson code needs that install.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
What does early stopping do?