Snorrlaxxx
Snorrlaxxx

Reputation: 168

XGBoost get feature importance as a list of columns instead of plot

I am wondering if you we can get the feature importance as a list of columns instead of a plot. This is what I have

xg_reg = xgb.train(params=params, dtrain=data_dmatrix, num_boost_round=10)
import matplotlib.pyplot as plt

xgb.plot_importance(xg_reg)
plt.rcParams['figure.figsize'] = [5,5]
plt.show()

Which gives me this plot

enter image description here

I would like to instead just get a list of the top features since I have over 800 different features.

Upvotes: 1

Views: 3246

Answers (2)

Sicko-Code
Sicko-Code

Reputation: 45

To get the importance of each feature as a dict:

feature_importance = bst.get_score()
sorted([(v,k) for k,v in feature_importance.items()], reverse=True)

Upvotes: 1

jared_mamrot
jared_mamrot

Reputation: 26215

You can use xgb.get_score(). Here are a number of examples: How to get feature importance in xgboost?

Upvotes: 2

Related Questions