How to beginner · 3 min read

How to use XGBoost feature importance

Quick answer
Use the XGBClassifier or XGBRegressor from the xgboost library to train your model, then call the get_booster().get_score() method to retrieve feature importance scores. Visualize these scores with xgboost.plot_importance() for clear interpretation.

PREREQUISITES

  • Python 3.8+
  • pip install xgboost>=1.7.0
  • pip install matplotlib>=3.0

Setup

Install the xgboost and matplotlib libraries to train models and visualize feature importance.

bash
pip install xgboost matplotlib

Step by step

This example trains an XGBClassifier on the Iris dataset, extracts feature importance scores, and plots them.

python
import xgboost as xgb
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

# Load data
iris = load_iris()
X, y = iris.data, iris.target

# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train XGBoost classifier
model = xgb.XGBClassifier(use_label_encoder=False, eval_metric='mlogloss')
model.fit(X_train, y_train)

# Predict and evaluate
y_pred = model.predict(X_test)
print(f"Accuracy: {accuracy_score(y_test, y_pred):.3f}")

# Get feature importance scores
importance = model.get_booster().get_score(importance_type='weight')
print("Feature importance (weight):", importance)

# Plot feature importance
xgb.plot_importance(model, importance_type='weight')
plt.show()
output
Accuracy: 1.000
Feature importance (weight): {'f2': 14, 'f3': 13, 'f0': 11, 'f1': 9}

Common variations

You can change importance_type to 'gain', 'cover', or 'total_gain' for different importance metrics. For regression, use XGBRegressor. You can also extract importance as a dictionary and customize plots with matplotlib.

python
import xgboost as xgb

# Different importance types
importance_gain = model.get_booster().get_score(importance_type='gain')
importance_cover = model.get_booster().get_score(importance_type='cover')

print("Gain importance:", importance_gain)
print("Cover importance:", importance_cover)

# Plot gain importance
xgb.plot_importance(model, importance_type='gain')
plt.show()
output
Gain importance: {'f2': 0.45, 'f3': 0.40, 'f0': 0.35, 'f1': 0.30}
Cover importance: {'f2': 0.50, 'f3': 0.42, 'f0': 0.38, 'f1': 0.33}

Troubleshooting

  • If get_score() returns an empty dictionary, ensure the model is trained before calling it.
  • If plots do not show, verify matplotlib.pyplot.show() is called in scripts.
  • For multi-class problems, feature importance is aggregated across all classes.

Key Takeaways

  • Use get_booster().get_score() with different importance_type values to extract feature importance.
  • Visualize feature importance easily with xgboost.plot_importance() and customize plots with matplotlib.
  • Train your model fully before extracting importance to avoid empty results.
  • For classification, XGBClassifier is preferred; for regression, use XGBRegressor.
  • Feature importance helps interpret model decisions and improve feature engineering.
Verified 2026-04
Verify ↗