Machine Learning · Lesson 5 of 5
Feature Importance & Interpretation
~14 min
The Concept
Your random forest predicts flood risk well, but the regional water authority wants to know why, which factors should they focus mitigation efforts on? Planting cover crops helps with soil erosion but costs money; diverting drainage channels is expensive but faster. Which intervention will most reduce flood probability?
The answer is encoded in the forest itself: you can measure how much each feature contributed to the splits across all 100 trees, and rank them. The ranking tells you which variables the model relied on most, and, with care, something about what the underlying system is actually doing.
Feature importance scores (a ranking of which input variables the model actually leaned on to make its decisions) tell you how much predictive power each variable provided. A variable used in many splits near the root of many trees gets a high score; a variable rarely chosen or only used in low-impact leaf splits gets a low score.
Important caveat: high feature importance does not mean the variable causes flooding. It means the variable was statistically useful for predicting it. Slope and upstream area are causally related to flooding. A variable like 'parcel ID' would get zero importance, unless parcel numbers happened to encode geography, in which case it might score high for entirely the wrong reason.
The Analogy
Imagine grading which ingredients mattered most for a recipe's success by looking at a hundred past attempts. If 'used fresh basil' shows up in every single successful dish, it scores high in importance. That does not necessarily mean basil alone causes success, maybe the cooks who bothered with fresh basil also did everything else more carefully too.
Code (optional)
import matplotlib.pyplot as plt
import shap # SHapley Additive exPlanations
# Standard mean-decrease-impurity importance (comes free with sklearn)
importances = rf.feature_importances_
# SHAP values: more rigorous, shows direction of effect
explainer = shap.TreeExplainer(rf)
shap_values = explainer.shap_values(X_test)
# Summary plot: each point is one prediction, coloured by feature value
shap.summary_plot(shap_values[1], X_test, feature_names=features)
# Points to the right = feature pushed prediction toward 'flooded'
# Red = high feature value, blue = lowLine by line
- rf.feature_importances_ gives a single importance score per feature, quick but doesn't tell you direction of effect.
- SHAP values (Shapley Additive exPlanations) decompose each individual prediction into contributions from each feature, showing both magnitude and direction.
- shap.summary_plot shows all test predictions at once: red points mean high feature value, right position means it pushed the prediction toward flooding. This reveals e.g. high slope pushing away from flooding (water runs off faster) while high upstream area pushes toward it.
Why Real Researchers Care
SHAP values are now standard practice in environmental ML papers because they provide model-agnostic, theoretically grounded explanations that stakeholders can interrogate. Regulators increasingly require explanations alongside predictions for any model informing environmental policy decisions.
Quick Check
Q1. If 'slope' has the highest feature importance in the flood prediction model, does this prove slope causes flooding?
Your Goal
Imagine slope had low feature importance but upstream drainage area had very high importance. What would you tell the water authority about where to focus flood mitigation investment?
Hint: High upstream area = large drainage network feeding the point. What kind of infrastructure intervention addresses that?
Teach It Back
In three sentences, explain what feature importance tells you and what it does NOT tell you, using the flood prediction example.