XGBoost Cheat Sheet
XGBoost cheat sheet covering the scikit-learn and native training APIs, key hyperparameters, early stopping, and feature importance.
2 PagesIntermediateMar 22, 2026
Scikit-learn API
Familiar fit/predict interface.
python
from xgboost import XGBClassifiermodel = XGBClassifier( n_estimators=300, max_depth=6, learning_rate=0.05, subsample=0.8, colsample_bytree=0.8, eval_metric="logloss", random_state=42,)model.fit(X_train, y_train, eval_set=[(X_val, y_val)], verbose=False)preds = model.predict(X_test)proba = model.predict_proba(X_test)
Native DMatrix API
Lower-level API with more control.
python
import xgboost as xgbdtrain = xgb.DMatrix(X_train, label=y_train)dval = xgb.DMatrix(X_val, label=y_val)params = {"objective": "binary:logistic", "max_depth": 6, "eta": 0.05}bst = xgb.train( params, dtrain, num_boost_round=500, evals=[(dval, "validation")], early_stopping_rounds=20,)bst.save_model("model.json")
Key Hyperparameters
Parameters that matter most for tuning.
- n_estimators- number of boosting rounds (trees)
- max_depth- max tree depth, controls overfitting
- learning_rate (eta)- shrinks each tree's contribution
- subsample- fraction of rows sampled per tree
- colsample_bytree- fraction of columns sampled per tree
- reg_alpha / reg_lambda- L1/L2 regularization on leaf weights
- early_stopping_rounds- stop when the validation metric stops improving
Feature Importance
Inspect and plot which features matter.
python
import matplotlib.pyplot as pltfrom xgboost import plot_importanceplot_importance(model, max_num_features=15, importance_type="gain")plt.show()importances = model.feature_importances_
Pro Tip
Set early_stopping_rounds together with an eval_set so training halts automatically once the validation metric plateaus — this both prevents overfitting and saves training time.
Was this cheat sheet helpful?
Explore Topics
#XGBoost#XGBoostCheatSheet#DataScience#Intermediate#ScikitLearnAPI#NativeDMatrixAPI#KeyHyperparameters#FeatureImportance#MachineLearning#APIs#CheatSheet#SkillVeris