XGBoosting Home | About | Contact | Examples

Configure XGBoost Histogram Tree Method (tree_method=hist)

The histogram-based tree method is an efficient approximation technique in XGBoost that bins continuous features into discrete buckets, making split finding faster.

This method is particularly useful for large datasets where exact splits are computationally expensive.

Here’s an example demonstrating how to configure an XGBoost model with the histogram tree method for a binary classification task using a synthetic dataset:

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score

# Generate a synthetic binary classification dataset
X, y = make_classification(n_samples=10000, n_features=20, n_informative=10, n_redundant=5, random_state=42)

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize an XGBClassifier with histogram tree method
model = XGBClassifier(tree_method='hist', max_depth=5, learning_rate=0.1, n_estimators=100)

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

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

# Evaluate the model
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy: {accuracy:.4f}")

In this example, we first generate a synthetic binary classification dataset using make_classification() from scikit-learn with 10,000 samples, 20 features, 10 informative features, and 5 redundant features. We then split the data into training and testing sets.

Next, we initialize an XGBClassifier with tree_method='hist' and set several key hyperparameters:

We then train the model using the fit() method, make predictions on the test set using predict(), and evaluate the model’s performance using accuracy_score() from sklearn.metrics.

The histogram tree method can significantly speed up the training process, especially for large datasets, while still maintaining good performance. However, the binning process may result in a slight loss of precision compared to the exact method.

As with any model, it’s essential to tune the hyperparameters to find the optimal balance between model complexity and generalization. Experiment with different values for max_depth, learning_rate, and n_estimators to find the best combination for your specific problem.

See Also