Machine Learning Tutorial Part 14: Feature Importance & Model Interpretation
🔍 Why Interpretability Matters
Understanding why a model makes certain predictions helps build trust, improve performance, detect bias, and comply with regulations. Interpretability is crucial in healthcare, finance, and other critical domains.
🌲 Built-in Feature Importance (Tree-Based Models)
Models like RandomForest
and XGBoost
offer built-in feature importances based on metrics like Gini importance or gain.
from sklearn.ensemble import RandomForestClassifier
import matplotlib.pyplot as plt
model = RandomForestClassifier()
model.fit(X_train, y_train)
importances = model.feature_importances_
features = X_train.columns
# Plotting
plt.barh(features, importances)
plt.xlabel("Importance")
plt.title("Feature Importance from Random Forest")
plt.show()
🔁 Permutation Importance
This method evaluates the drop in model performance when a feature's values are randomly shuffled.
from sklearn.inspection import permutation_importance
result = permutation_importance(model, X_test, y_test, n_repeats=10, random_state=42)
for i in result.importances_mean.argsort()[::-1]:
print(f"{X_test.columns[i]}: {result.importances_mean[i]:.4f}")
⚖️ SHAP (SHapley Additive exPlanations)
SHAP values explain the output of any model by assigning each feature an importance value for a particular prediction.
This is especially useful for deep model understanding and individual-level explanations.
import shap
explainer = shap.Explainer(model, X_test)
shap_values = explainer(X_test)
# Summary plot
shap.summary_plot(shap_values, X_test)
👤 SHAP for Individual Predictions
Explain specific predictions using SHAP force plots:
# Force plot for a single instance
shap.initjs()
shap.force_plot(explainer.expected_value, shap_values[0], X_test.iloc[0])
🛠 Tools for Model Interpretation
- SHAP: For deep explanation of complex models.
- ELI5: Works well with scikit-learn models.
- LIME: Local explanations around a prediction.
- Sklearn: Built-in tools like
permutation_importance
.
📌 Summary
- Feature importance helps you understand how models use inputs.
- Tree-based models offer native feature importance scores.
- Permutation importance evaluates the impact on accuracy when features are shuffled.
- SHAP offers detailed, consistent interpretability for any model.
Comments
Post a Comment