Skip to content

Instantly share code, notes, and snippets.

@sazio
Created August 17, 2020 22:25
Show Gist options
  • Save sazio/d2b0189a2182c5ef45afe08467eceb23 to your computer and use it in GitHub Desktop.
Save sazio/d2b0189a2182c5ef45afe08467eceb23 to your computer and use it in GitHub Desktop.
# permutation feature importance with knn for classification
from sklearn.neighbors import KNeighborsClassifier
from sklearn.inspection import permutation_importance
from matplotlib import pyplot as plt
# define the model
model = KNeighborsClassifier()
# fit the model
model.fit(X_clf, y_clf)
# perform permutation importance
results = permutation_importance(model, X_clf, y_clf, scoring='accuracy')
# get importance
importance = results.importances_mean
# 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