Skip to content

Instantly share code, notes, and snippets.

@mertbozkir
Last active May 14, 2022 12:58
Show Gist options
  • Save mertbozkir/c31d73756d1f2176f6000c3273452929 to your computer and use it in GitHub Desktop.
Save mertbozkir/c31d73756d1f2176f6000c3273452929 to your computer and use it in GitHub Desktop.
Plotting feature importances for machine learning model.
def plot_importance(model, features, num = len(X), save = False):
feature_imp = pd.DataFrame({'Value': model.feature_importances_, 'Feature': features.columns})
plt.figure(figsize = (10, 10))
sns.set(font_scale = 1)
sns.barplot(x = 'Value', y = 'Feature',
data = feature_imp.sort_values(by = 'Value', ascending = False)[0:num])
plt.title('Features')
plt.tight_layout()
plt.show()
if save:
plt.savefig('importances.png')
# Example: plot_importance(random_forest, X, num = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment