You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中绘制XGBoost模型的Top K重要特征?

Plotting Top K Feature Importance with XGBoost

Great question—dealing with thousands of features makes the default plot unreadable, so focusing on the top N is totally the way to go. Here are two solid approaches to achieve this:

1. Use XGBoost's Built-in Parameter (Easiest Way)

You might not have noticed, but xgb.plot_importance() actually has a max_num_features parameter that lets you specify exactly how many top features to display. This is the quickest solution:

import xgboost as xgb
import matplotlib.pyplot as plt

# Assuming your trained model is stored in xgb_model
xgb.plot_importance(xgb_model, max_num_features=100)  # Show top 100 features
plt.title("Top 100 Feature Importance (Weight)")
plt.show()

By default, this uses the weight importance type (number of times a feature is used in splits). If you want to use a different metric like gain (average gain across splits) or cover (average coverage of splits), just add the importance_type argument:

xgb.plot_importance(xgb_model, max_num_features=100, importance_type='gain')

2. Manual Approach Using get_score() (For Full Control)

If you want more flexibility over the plot (like customizing colors, labels, or layout), you can use get_score() to extract the importance scores, filter to the top K, then plot with Matplotlib:

Step 1: Extract and Sort Importance Scores

First, get the importance dictionary and sort it in descending order:

# Extract importance scores (choose your preferred importance type)
importance_dict = xgb_model.get_score(importance_type='gain')

# Sort features by importance (highest first)
sorted_features = sorted(importance_dict.items(), key=lambda x: x[1], reverse=True)

# Take the top 100 features
top_100_features = sorted_features[:100]

Step 2: Prepare Data for Plotting

Separate the feature names and their scores into two lists:

feature_names = [item[0] for item in top_100_features]
importance_scores = [item[1] for item in top_100_features]

Step 3: Create the Plot

Use Matplotlib to create a horizontal bar plot (easier to read long feature names):

plt.figure(figsize=(10, 25))  # Adjust figure size to fit 100 features
plt.barh(feature_names, importance_scores, color='skyblue')

# Customize labels and title
plt.xlabel('Importance Score (Gain)')
plt.ylabel('Feature')
plt.title('Top 100 Feature Importance')

# Invert y-axis so highest importance is at the top
plt.gca().invert_yaxis()

plt.tight_layout()  # Ensure labels don't get cut off
plt.show()

This approach lets you tweak every aspect of the plot—change colors, add annotations, or even save it directly to a file with plt.savefig('top_features.png').

内容的提问来源于stack exchange,提问作者skydome20

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:00:22