-
-
Save sazio/6ac9243bd0e0a1b9e2ca1fad73eca32a 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 regression | |
from sklearn.neighbors import KNeighborsRegressor | |
from sklearn.inspection import permutation_importance | |
from matplotlib import pyplot as plt | |
# define the model | |
model = KNeighborsRegressor() | |
# fit the model | |
model.fit(X_reg, y_reg) | |
# perform permutation importance | |
results = permutation_importance(model, X_reg, y_reg, scoring='neg_mean_squared_error') | |
# 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