For feature importance in regression, the key metric is R-squared (R²) and Mean Squared Error (MSE). R² tells us how well the features explain the variation in the target. MSE shows how close the predictions are to actual values. When checking feature importance, we look at how these metrics change when we remove or shuffle a feature. A big drop in R² or rise in MSE means that feature is important.
Feature importance in regression in ML Python - Model Metrics & Evaluation
Regression does not use a confusion matrix because it predicts continuous values, not categories. Instead, we use error metrics like MSE or visualize feature importance with bar charts showing the impact of each feature on prediction error.
Feature Importance Example (higher value means more important):
Feature A: |||||||||||| (0.35)
Feature B: ||||||| (0.20)
Feature C: ||| (0.10)
Feature D: | (0.05)
These numbers represent how much the error increases when the feature is removed or shuffled.
In regression, we don't have precision or recall. Instead, the tradeoff is between model complexity and feature selection. Using too many features can cause overfitting (model fits training data well but fails on new data). Using too few features can cause underfitting (model misses important patterns). Feature importance helps find the right balance by showing which features truly help prediction.
Example: If a house price model includes many features but only a few like size and location matter, removing less important features keeps the model simple and accurate.
Good feature importance results show:
- Clear ranking of features by importance.
- Removing top features causes a noticeable drop in R² or rise in MSE.
- Low importance features have little effect on metrics when removed.
Bad results show:
- All features have similar importance, making it hard to tell which matter.
- Removing features does not change R² or MSE, meaning features may be irrelevant or model is not sensitive.
- Feature importance values are unstable across different data samples.
- Data leakage: If features leak information from the future or target, importance looks artificially high.
- Overfitting: Complex models may assign high importance to noise features, misleading interpretation.
- Correlation: Highly correlated features can share importance, making it hard to separate their effects.
- Ignoring metric changes: Looking only at importance scores without checking how metrics like R² or MSE change can mislead.
Your regression model has an R² of 0.85. When you remove Feature X, R² drops to 0.50. Is Feature X important? Why?
Answer: Yes, Feature X is important because removing it causes a large drop in R², meaning the model loses much of its ability to explain the target variation without it.
Practice
feature importance tell us in a regression model?Solution
Step 1: Understand feature importance meaning
Feature importance measures how much each input feature affects the model's output.Step 2: Compare options to definition
Only Which input features most influence the model's predictions correctly describes this concept as the influence of input features on predictions.Final Answer:
Which input features most influence the model's predictions -> Option CQuick Check:
Feature importance = input influence [OK]
- Confusing feature importance with model accuracy
- Thinking it shows model speed or size
- Mixing it up with number of model layers
Solution
Step 1: Recall Random Forest attribute for feature importance
Random Forest regression models in scikit-learn provide feature importance via the attributefeature_importances_.Step 2: Eliminate other options
predict()makes predictions,fit()trains the model, andscore()evaluates performance, none return feature importance.Final Answer:
model.feature_importances_ -> Option DQuick Check:
Feature importance attribute = feature_importances_ [OK]
- Using predict() instead of feature_importances_
- Confusing fit() with importance retrieval
- Trying to get importance from score()
from sklearn.ensemble import RandomForestRegressor import numpy as np X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) y = np.array([10, 20, 30, 40]) model = RandomForestRegressor(random_state=0) model.fit(X, y) print(model.feature_importances_)
Solution
Step 1: Understand the dataset and model
The features are two columns with increasing values, but the first feature has a slightly stronger linear relationship with the target.Step 2: Interpret feature importance output
Random Forest will assign higher importance to the feature with stronger influence, which is the first feature here.Final Answer:
[0.7 0.3] -> Option BQuick Check:
Feature 1 stronger -> higher importance [OK]
- Assuming equal importance without checking data
- Expecting exact 0.5 values
- Confusing feature importance with coefficients
from sklearn.ensemble import RandomForestRegressor model = RandomForestRegressor() print(model.feature_importances_)What is the problem?
Solution
Step 1: Identify cause of error
Accessingfeature_importances_before training causes an AttributeError because the model has no learned features yet.Step 2: Correct usage
Callfit()on training data before accessingfeature_importances_.Final Answer:
The model must be trained with fit() before accessing feature_importances_ -> Option AQuick Check:
Train model first, then get feature importance [OK]
- Trying to access feature_importances_ before fit()
- Thinking feature_importances_ needs separate import
- Believing it's only for classification
Solution
Step 1: Interpret low feature importance meaning
Features with near-zero importance contribute little to predictions and may add noise or complexity.Step 2: Decide action based on importance
Removing unimportant features simplifies the model and can improve performance and interpretability.Final Answer:
Remove those low-importance features and retrain the model -> Option AQuick Check:
Drop low-importance features to simplify model [OK]
- Adding more similar low-importance features
- Ignoring importance and keeping all features
- Only increasing trees without feature selection
Ready to prove it?
Pass the test and this pattern moves unknown
