Last active
May 14, 2022 12:58
-
-
Save mertbozkir/c31d73756d1f2176f6000c3273452929 to your computer and use it in GitHub Desktop.
Plotting feature importances for machine learning model.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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