-
-
Save sazio/d2b0189a2182c5ef45afe08467eceb23 to your computer and use it in GitHub Desktop.
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
# 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