XGBoosting Home | About | Contact | Examples

XGBoost Linear Booster "coef_" Property

When using XGBoost’s linear booster, it’s often valuable to inspect the learned feature coefficients to gain insights into the model’s decision-making process.

The coefficients represent the weights assigned to each feature, indicating their importance and direction of influence on the target variable. Accessing these coefficients is straightforward using the coef_ property of the trained model.

Here’s an example that demonstrates how to retrieve and interpret the feature coefficients of an XGBoost linear model using a synthetic dataset:

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

# Generate a synthetic regression dataset
X, y = make_regression(n_samples=1000, n_features=5, 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 an XGBRegressor with linear booster
model = XGBRegressor(booster='gblinear')

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

# Access the learned feature coefficients
coefficients = model.coef_

# Print the coefficients along with their corresponding feature names
for feature, coef in zip(range(len(coefficients)), coefficients):
    print(f"Feature {feature}: {coef:.4f}")

In this example, we generate a synthetic regression dataset using make_regression() from scikit-learn. We then split the data into training and testing sets.

Next, we initialize an XGBRegressor with booster='gblinear' to specify a linear model and train it using the fit() method.

After training, we access the learned feature coefficients using the coef_ property of the trained model. The coefficients are stored as a numpy array in the same order as the input features.

Finally, we print the coefficients along with their corresponding feature indices using a for loop and zip().

Interpreting the coefficients is straightforward: a positive coefficient indicates a positive correlation between the feature and the target variable, while a negative coefficient indicates a negative correlation. The magnitude of the coefficient represents the strength of the relationship.

By inspecting the learned coefficients, you can gain valuable insights into which features are most important for the model’s predictions and how they influence the target variable. This information can be used for feature selection, model simplification, or to guide further data analysis and domain expertise.

Keep in mind that the interpretation of coefficients assumes that the features are on a similar scale. If the features have different scales, it’s recommended to normalize or standardize them before training the linear model to ensure fair comparison of the coefficients.

See Also