XGBoost (CPU Version)

3.2.0 · active · verified Thu Apr 16

XGBoost is an optimized distributed gradient boosting library designed for speed and performance. The `xgboost-cpu` package serves as a convenience installer, providing the core XGBoost library (version 2.0.3 as of `xgboost-cpu==3.2.0`) compiled with CPU-only optimizations. This meta-package, currently at version 3.2.0, facilitates specific build installations and is updated alongside major XGBoost releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to train a basic XGBoost classifier for a binary classification task using synthetic data. It showcases the common `XGBClassifier` API, similar to scikit-learn models.

import xgboost as xgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.datasets import make_classification
import os

# 1. Generate synthetic data for a classification task
X, y = make_classification(n_samples=1000, n_features=20, n_informative=10, n_redundant=5, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 2. Initialize the XGBoost classifier
# 'objective' specifies the learning task.
# 'eval_metric' defines the metric for evaluation during training.
# `use_label_encoder=False` is recommended for XGBoost 1.x and 2.x to avoid a deprecation warning.
# `n_jobs` can be set to -1 to use all available CPU cores, or a specific number.
model = xgb.XGBClassifier(
    objective='binary:logistic',
    eval_metric='logloss',
    n_estimators=100,
    learning_rate=0.1,
    max_depth=5,
    use_label_encoder=False, # Required for older versions to silence warning
    n_jobs=int(os.environ.get('XGB_N_JOBS', '-1')), # Example of using env var for N_JOBS
    random_state=42
)

# 3. Train the model
model.fit(X_train, y_train)

# 4. Make predictions on the test set
y_pred = model.predict(X_test)

# 5. Evaluate the model's performance
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy:.4f}")

view raw JSON →