XGBoosting Home | About | Contact | Examples

XGBoost Configure "error@t" Eval Metric

When performing binary classification tasks with XGBoost, the “error@t” evaluation metric provides flexibility in setting a specific classification threshold.

By default, XGBoost uses a threshold of 0.5 to map predicted probabilities to class labels, but “error@t” allows you to specify a custom threshold value.

The “error@t” metric measures the binary classification error at the specified threshold t. This is particularly useful when you need to optimize for a specific decision threshold or control the trade-off between precision and recall.

For example, if you’re building a spam email classifier and want to minimize false positives, you might set a higher threshold to ensure that only emails with high predicted probabilities are classified as spam.

Here’s an example of how to use “error@t” as the evaluation metric with XGBoost and scikit-learn:

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier
import matplotlib.pyplot as plt

# Generate a synthetic binary classification dataset
X, y = make_classification(n_samples=1000, n_classes=2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create an XGBClassifier with "error@0.7" as the evaluation metric
model = XGBClassifier(n_estimators=100, eval_metric='error@0.7', early_stopping_rounds=10, random_state=42)

# Train the model with early stopping
model.fit(X_train, y_train, eval_set=[(X_test, y_test)])

# Retrieve the "error@0.7" values from the training process
results = model.evals_result()
epochs = len(results['validation_0']['error@0.7'])
x_axis = range(0, epochs)

# Plot the "error@0.7" values
plt.figure()
plt.plot(x_axis, results['validation_0']['error@0.7'], label='Test')
plt.legend()
plt.xlabel('Number of Boosting Rounds')
plt.ylabel('Binary Classification Error')
plt.title('XGBoost "error@0.7" Performance')
plt.show()

In this example, we generate a synthetic binary classification dataset using scikit-learn’s make_classification function and split the data into training and testing sets.

We create an instance of XGBClassifier and set eval_metric='error@0.7' to specify the “error@t” metric with a threshold of 0.7. We also set early_stopping_rounds=10 to enable early stopping if the metric doesn’t improve for 10 consecutive rounds.

During training, we pass the testing set as the eval_set to monitor the model’s performance on unseen data. After training, we retrieve the “error@0.7” values using the evals_result() method.

Finally, we plot the “error@0.7” values against the number of boosting rounds to visualize the model’s performance during training. This plot helps us assess the model’s classification error at the specified threshold and determine the optimal number of boosting rounds.

By using “error@t” as the evaluation metric, you can fine-tune your XGBoost model’s performance based on a specific classification threshold, allowing you to optimize for precision, recall, or any other desired metric.



See Also