Created
March 27, 2026 15:03
-
-
Save BlazerYoo/bc8741a25d790d416dd069d5ea2d06d7 to your computer and use it in GitHub Desktop.
2a
This file contains hidden or 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
| # CITATION: https://scikit-learn.org/stable/modules/permutation_importance.html#permutation-importance | |
| # CITATION: https://scikit-learn.org/stable/modules/generated/sklearn.inspection.permutation_importance.html | |
| from sklearn.inspection import permutation_importance | |
| models = { | |
| 'Nearest Neighbors': knn, | |
| 'Naive Bayes': nb, | |
| 'Logistic Regression': log_reg, | |
| 'Support Vector Machines': svm, | |
| 'Decision Tree': dt, | |
| 'Random Forest': rf | |
| } | |
| # Set input data for permutation importance | |
| X_test_dict = { | |
| 'Nearest Neighbors': X_test_scaled, | |
| 'Naive Bayes': X_test, | |
| 'Logistic Regression': X_test_scaled, | |
| 'Support Vector Machines': X_test_scaled, | |
| 'Decision Tree': X_test, | |
| 'Random Forest': X_test | |
| } | |
| for name, model in models.items(): | |
| # CITATION: https://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter | |
| result = permutation_importance( | |
| model, X_test_dict[name], y_test, | |
| scoring='accuracy', n_repeats=30, random_state=1234 | |
| ) | |
| sorted_idx = result.importances_mean.argsort()[::-1][:5] | |
| print(f'\n{name} - Top 5 Features (in terms of impact on Accuracy):') | |
| for i in sorted_idx: | |
| print(f'{feature_names[i]:<25}: {result.importances_mean[i]:.4f} +/- {result.importances_std[i]:.4f}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment