Skip to content

Instantly share code, notes, and snippets.

@sazio
Created August 17, 2020 22:25
Show Gist options
  • Save sazio/012124d504f6dab7b05b879d41a1da14 to your computer and use it in GitHub Desktop.
Save sazio/012124d504f6dab7b05b879d41a1da14 to your computer and use it in GitHub Desktop.
# decision tree for feature importance on a regression problem
from sklearn.tree import DecisionTreeRegressor
from matplotlib import pyplot as plt
# define the model
model = DecisionTreeRegressor()
# fit the model
model.fit(X_reg, y_reg)
# 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
plt.bar([x for x in range(len(importance))], importance)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment