Skip to content

Instantly share code, notes, and snippets.

@sazio
Created August 17, 2020 22:25
Show Gist options
  • Save sazio/f94bc851d9e8b78c2975215f94955505 to your computer and use it in GitHub Desktop.
Save sazio/f94bc851d9e8b78c2975215f94955505 to your computer and use it in GitHub Desktop.
# xgboost for feature importance on a classification problem
from xgboost import XGBClassifier
from matplotlib import pyplot
# define the model
model = XGBClassifier()
# fit the model
model.fit(X_clf, y_clf)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment