Does xgboost have feature_importances_?

Question:

I’m calling xgboost via its scikit-learn-style Python interface:

model = xgboost.XGBRegressor() 
%time model.fit(trainX, trainY)
testY = model.predict(testX)

Some sklearn models tell you which importance they assign to features via the attribute feature_importances. This doesn’t seem to exist for the XGBRegressor:

model.feature_importances_
AttributeError   Traceback (most recent call last)
<ipython-input-36-fbaa36f9f167> in <module>()
----> 1 model.feature_importances_

AttributeError: 'XGBRegressor' object has no attribute 'feature_importances_'

The weird thing is: For a collaborator of mine the attribute feature_importances_ is there! What could be the issue?

These are the versions I have:

In [2]: xgboost.__version__
Out[2]: '0.6'

In [4]: sklearn.__version__
Out[4]: '0.18.1'

… and the xgboost C++ library from github, commit ef8d92fc52c674c44b824949388e72175f72e4d1.

Asked By: clstaudt

||

Answers:

How did you install xgboost? Did you build the package after cloning it from github, as described in the doc?

http://xgboost.readthedocs.io/en/latest/build.html

As in this answer:

Feature Importance with XGBClassifier

There always seems to be a problem with the pip-installation and xgboost. Building and installing it from your build seems to help.

Answered By: Jens Beyer

This is useful for you,maybe.

xgb.plot_importance(bst)

And this is the link:plot

Answered By: huangqinjian

This worked for me:

model.get_booster().get_score(importance_type='weight')

hope it helps

Answered By: ChrisDanger

Yes, XGBoost model has feature importance details

Try the following : (It also gives you the names of the features and its weightage)

from matplotlib import pyplot

bars = xgb_model.get_booster().feature_names
y_pos = np.arange(len(bars))
pyplot.bar(range(len(xgb_model.feature_importances_)), xgb_model.feature_importances_)
pyplot.xticks(y_pos, xgb_model.get_booster().feature_names, color='black', rotation=45, fontsize='25', horizontalalignment='right')
pyplot.show()

Feature importance plot

Answered By: Apoorva ventra