XGBoosting Home | About | Contact | Examples

Evaluate XGBoost Performance with the Mean Absolute Error Metric

When evaluating the performance of regression models, it’s essential to use appropriate metrics that quantify how well the model’s predictions align with the actual values. One widely used metric for this purpose is Mean Absolute Error (MAE).

MAE measures the average absolute difference between the predicted and actual values. It provides an intuitive understanding of how much the model’s predictions deviate from the true values on average. A lower MAE indicates better model performance, as it suggests that the predictions are closer to the actual values.

Here’s an example of how to calculate the MAE for an XGBoost regressor using the scikit-learn library in Python:

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error

# Generate a synthetic dataset for regression
X, y = make_regression(n_samples=1000, n_features=10, noise=0.1, 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 and train the XGBoost regressor
model = XGBRegressor(n_estimators=100, learning_rate=0.1, random_state=42)
model.fit(X_train, y_train)

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

# Calculate the mean absolute error
mae = mean_absolute_error(y_test, y_pred)

print(f"Mean Absolute Error: {mae:.2f}")

In this example:

  1. We generate a synthetic dataset for a regression problem using make_regression from scikit-learn.
  2. We split the data into training and testing sets using train_test_split.
  3. We initialize an XGBoost regressor with specified hyperparameters and train it on the training data using fit().
  4. We make predictions on the test set using the trained model’s predict() method.
  5. We calculate the MAE using scikit-learn’s mean_absolute_error function, which takes the true values (y_test) and predicted values (y_pred) as arguments.
  6. Finally, we print the MAE to evaluate the model’s performance.

By calculating the MAE, we can assess how well the XGBoost regressor is performing in terms of predicting values close to the actual values. This metric provides a clear and interpretable measure of the model’s average prediction error, helping us understand its effectiveness and guiding further improvements.



See Also