XGBoost › Implementation using XGBoost by DMLC
Machine Learning from ScratchAll chapters XGBoost › Implementation using XGBoost by DMLCOne section · Sections in this chapter7 / 10 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
XGBoost can be implemented using the scikit-learn compatible XGBoost library, which provides a familiar interface while using XGBoost's advanced optimization features. Let's demonstrate this with both classification and regression examples.
Classification Example
import numpy as np
np.random.seed(42)from sklearn.model_selection import cross_val_score
X_class, y_class = make_classification(
n_samples=1000,
n_features=20,
n_informative=15,
n_redundant=5,
n_classes=3,
random_state=42,
)
X_train_class, X_test_class, y_train_class, y_test_class = train_test_split(
X_class, y_class, test_size=0.3, random_state=42
)
xgb_classifier = xgb.XGBClassifier(
n_estimators=100,
max_depth=6,
learning_rate=0.1,
subsample=0.8,
colsample_bytree=0.8,
random_state=42,
verbosity=0, # Suppress output
)
xgb_classifier.fit(X_train_class, y_train_class)
y_pred_class = xgb_classifier.predict(X_test_class)
y_pred_proba = xgb_classifier.predict_proba(X_test_class)
accuracy = accuracy_score(y_test_class, y_pred_class)
# Cross-validation score
cv_scores = cross_val_score(xgb_classifier, X_class, y_class, cv=5)Classification Report:
Accuracy: 0.8267
Cross-validation accuracy: 0.8100 (+/- 0.0610)
precision recall f1-score support
0 0.80 0.82 0.81 99
1 0.86 0.83 0.85 114
2 0.81 0.83 0.82 87
accuracy 0.83 300
macro avg 0.82 0.83 0.83 300
weighted avg 0.83 0.83 0.83 300

Regression Example
from sklearn.datasets import make_regression
from sklearn.model_selection import cross_val_score
X_reg, y_reg = make_regression(
n_samples=1000, n_features=20, n_informative=15, noise=0.1, random_state=42
)
X_train_reg, X_test_reg, y_train_reg, y_test_reg = train_test_split(
X_reg, y_reg, test_size=0.3, random_state=42
)
xgb_regressor = xgb.XGBRegressor(
n_estimators=100,
max_depth=6,
learning_rate=0.1,
subsample=0.8,
colsample_bytree=0.8,
random_state=42,
verbosity=0,
)
xgb_regressor.fit(X_train_reg, y_train_reg)
y_pred_reg = xgb_regressor.predict(X_test_reg)
from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y_test_reg, y_pred_reg)
r2 = r2_score(y_test_reg, y_pred_reg)
cv_scores_reg = cross_val_score(xgb_regressor, X_reg, y_reg, cv=5, scoring="r2")Regression Report: Mean Squared Error: 8607.3039 R² Score: 0.8293 Cross-validation R²: 0.8127 (+/- 0.0374)


Hyperparameter Tuning Example
from sklearn.model_selection import GridSearchCV
param_grid = { # Define parameter grid to test
"n_estimators": [50, 100, 200],
"max_depth": [3, 6, 9],
"learning_rate": [0.01, 0.1, 0.2],
"subsample": [0.8, 0.9, 1.0],
}
grid_search = GridSearchCV(
xgb.XGBClassifier(random_state=42, verbosity=0),
param_grid,
cv=3,
scoring="accuracy",
n_jobs=-1,
)
grid_search.fit(X_train_class, y_train_class)
best_model = grid_search.best_estimator_ # Evaluate on test set
test_accuracy = best_model.score(X_test_class, y_test_class)Best parameters:
{'learning_rate': 0.2, 'max_depth': 6, 'n_estimators': 200, 'subsample': 0.8}
Best cross-validation score: 0.7843
Test set accuracy with best parameters: 0.8233Top 10 Hyperparameter Combinations: ================================================================================ Rank n_est max_d lr sub Mean CV Std CV -------------------------------------------------------------------------------- 1 200.0 6.0 0.2 0.8 0.7843 0.0169 2 100.0 6.0 0.2 0.8 0.7814 0.0164 3 200.0 3.0 0.2 0.9 0.7800 0.0126 4 100.0 9.0 0.2 0.8 0.7800 0.0196 5 50.0 6.0 0.2 0.8 0.7771 0.0129 6 200.0 9.0 0.2 0.8 0.7771 0.0164 7 200.0 3.0 0.1 0.8 0.7743 0.0184 8 200.0 6.0 0.1 0.9 0.7743 0.0184 9 200.0 3.0 0.2 1.0 0.7728 0.0124 10 200.0 3.0 0.2 0.8 0.7714 0.0105 Best Parameters Found: ======================================== learning_rate: 0.2 max_depth: 6 n_estimators: 200 subsample: 0.8 Best CV Score: 0.7843


The implementation demonstrates several key aspects of using XGBoost in practice. The scikit-learn compatible interface makes it easy to integrate XGBoost into existing machine learning workflows, while the extensive hyperparameter options allow for fine-tuning performance. The feature importance analysis shows how XGBoost can help identify the most relevant features in the dataset, which is useful for both model interpretation and feature selection.
Key takeaways from this implementation:
- XGBoost provides excellent out-of-the-box performance with minimal tuning
- The algorithm handles both classification and regression tasks effectively
- Hyperparameter tuning can significantly improve performance
- Feature importance analysis shows which features influence the model's decisions
Ranking Implementation Example
XGBoost also supports learning-to-rank tasks used in applications like search engines, recommendation systems, and information retrieval. Let's demonstrate how to implement ranking with XGBoost.
import numpy as np
np.random.seed(42)def generate_ranking_data(n_queries=100, n_docs_per_query=10, n_features=5):
"""Generate synthetic ranking data with queries, documents, and relevance scores."""
documents = []
relevance_scores = []
query_groups = []
for query_id in range(n_queries):
# Generate documents for this query
n_docs = np.random.randint(5, n_docs_per_query + 1)
# Generate features for each document
doc_features = np.random.randn(n_docs, n_features)
# Generate relevance scores (0-4 scale)
relevance = np.random.choice(
[0, 1, 2, 3, 4], size=n_docs, p=[0.1, 0.2, 0.3, 0.3, 0.1]
)
# Add query-specific bias to make some queries easier to rank
query_bias = np.random.randn(n_features) * 0.5
doc_features += query_bias
documents.extend(doc_features)
relevance_scores.extend(relevance)
query_groups.extend([n_docs]) # Group size for each query
return (
np.array(documents),
np.array(relevance_scores),
np.array(query_groups),
)
# Generate training and test data
train_docs, train_relevance, train_groups = generate_ranking_data(80, 8, 5)
test_docs, test_relevance, test_groups = generate_ranking_data(20, 8, 5)print(
f"Training data: {len(train_docs)} documents across {len(train_groups)} queries"
)
print(
f"Test data: {len(test_docs)} documents across {len(test_groups)} queries"
)
print(f"Training group sizes: {train_groups}")
print(f"Test group sizes: {test_groups}")xgb_ranker = xgb.XGBRanker(
n_estimators=100,
max_depth=6,
learning_rate=0.1,
subsample=0.8,
colsample_bytree=0.8,
random_state=42,
verbosity=0,
objective="rank:pairwise", # Use pairwise ranking objective
)
xgb_ranker.fit(
train_docs,
train_relevance,
group=train_groups, # Group by query
eval_set=[(test_docs, test_relevance)],
eval_group=[test_groups],
verbose=False,
)
train_predictions = xgb_ranker.predict(train_docs)
test_predictions = xgb_ranker.predict(test_docs)



Ranking Comparison for First Query Group (size: 8): Actual ranking (by relevance): ['Doc 6', 'Doc 4', 'Doc 7', 'Doc 5', 'Doc 3', 'Doc 1', 'Doc 8', 'Doc 2'] Predicted ranking (by XGBoost): ['Doc 4', 'Doc 2', 'Doc 3', 'Doc 6', 'Doc 7', 'Doc 1', 'Doc 8', 'Doc 5']
The ranking implementation demonstrates XGBoost's capability to handle learning-to-rank problems effectively. Key aspects of this implementation include:
- Group-based training: XGBoost uses query groups to understand which documents belong to the same query
- Pairwise ranking objective: The model optimizes for correct pairwise document ordering
- NDCG evaluation: Standard ranking metrics are used to assess performance
- Feature importance: Understanding which features contribute most to ranking decisions
This makes XGBoost particularly valuable for applications like search engines, recommendation systems, and any scenario where relative ordering of items is more important than absolute scores.