Machine Learning
Feature importance
Learn Feature importance: 37 practice questions and answers with an interactive quiz — from the offline AI Learning app.
Questions & Answers
Tap a question to reveal its answer. Written for self-study and interview preparation.
What is feature importance in machine learning?
Feature importance is a score that indicates how much each input variable (feature) contributes to a model's predictions. Higher scores mean the feature has more influence on the output. It helps data scientists understand which variables matter most and can be used to simplify models by removing low-importance features, saving computation time and sometimes improving generalization.
Which of the following is NOT a benefit of computing feature importance?
Feature importance helps with model interpretability, guiding feature selection, and informing data collection priorities. It does not, however, guarantee improved model accuracy — removing features based on importance can sometimes hurt performance if those features capture weak but useful signals. Feature importance is a diagnostic and interpretability tool, not a direct accuracy booster.
How does a Decision Tree measure the importance of each feature?
A Decision Tree measures feature importance by summing how much each feature reduces node impurity (Gini impurity or entropy) across every split where it is used. Features that produce cleaner, purer splits — especially near the root — accumulate higher importance scores. These scores are then normalized so they all sum to 1, making them easy to compare.
What does Gini impurity measure, and how is it linked to feature importance in Decision Trees?
Gini impurity measures how often a randomly selected element from a node would be incorrectly classified if labeled randomly according to the class distribution in that node. For a node with class proportions p₁, p₂, …, the Gini impurity is G = 1 − Σ(pᵢ²). A feature's importance equals the total reduction in Gini impurity it achieves across all splits in the tree, so features that consistently create purer child nodes are ranked higher.
Which sklearn attribute gives you the feature importances of a trained Random Forest?
The `feature_importances_` attribute of a fitted Random Forest (or any sklearn tree ensemble) returns an array of Mean Decrease in Impurity (MDI) scores for each feature, normalized to sum to 1. You access it after calling `model.fit(X, y)`. Features with higher values contributed more to reducing impurity across all trees in the forest.
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
import pandas as pd
X, y = load_iris(return_X_y=True, as_frame=True)
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X, y)
importances = pd.Series(model.feature_importances_, index=X.columns)
print(importances.sort_values(ascending=False))Why is Random Forest feature importance generally more reliable than a single Decision Tree's importance?
A single Decision Tree makes greedy local split decisions; small changes in training data can cause completely different splits, making importance scores highly variable. Random Forest averages importance scores across hundreds of trees, each trained on a random bootstrap sample with a random feature subset at each split. This ensemble averaging reduces variance and produces stable, generalizable importance estimates that are less sensitive to noise in the data.
What is permutation importance and how does it differ from impurity-based (MDI) importance?
Permutation importance measures how much a model's performance drops on a held-out validation set when one feature's values are randomly shuffled, destroying its relationship with the target. A large performance drop means the feature is crucial. Unlike MDI (computed during training from split statistics), permutation importance is evaluated on unseen data, making it a better measure of real predictive power. It is also model-agnostic and can be applied to any trained model.
What are SHAP values and what makes them uniquely powerful for feature importance?
SHAP (SHapley Additive exPlanations) values originate from cooperative game theory and measure each feature's contribution to a specific individual prediction by considering all possible orderings of features. Unlike global importance scores, SHAP provides both local (per-prediction) explanations and a global summary (mean absolute SHAP value). This dual capability means you can see not just which features matter overall, but precisely how each feature pushed a specific prediction up or down from the model's baseline.
Can a feature with near-zero correlation to the target still have high feature importance? Explain why.
Yes — correlation and feature importance measure different things. Pearson correlation only captures linear relationships, while feature importance in tree-based models captures non-linear patterns and feature interactions. A feature might interact strongly with another feature (e.g., it is only informative when a second feature is large) without showing a linear trend with the target on its own. Relying solely on correlation to filter features before model training can therefore discard genuinely useful predictors.
What is Mean Decrease in Impurity (MDI) and what is its well-known bias?
MDI (also called Gini importance) sums how much each feature reduces node impurity across all trees and all splits, then averages over the forest. A key bias is that MDI tends to overestimate the importance of high-cardinality features — those with many unique values — because they offer more candidate split thresholds and are more likely to be selected by chance even if their true predictive power is modest. Permutation importance does not share this bias because it is computed on out-of-sample data.
What is Mean Decrease in Accuracy (MDA) and how is it computed in a Random Forest?
MDA measures how much each feature contributes to the model's accuracy by recording the baseline out-of-bag (OOB) accuracy, then randomly shuffling each feature one at a time and measuring the resulting accuracy drop. A feature that causes a large accuracy drop when shuffled is considered highly important. Because MDA is evaluated on the OOB samples (data not used in training that tree), it reflects out-of-sample performance and is less susceptible to overfitting bias than MDI.
In XGBoost, what is the difference between 'gain' and 'weight' (frequency) importance?
'Weight' (frequency) counts how many times each feature is used as a split node across all trees — a feature used in many splits gets a high weight score. 'Gain' measures the average improvement in the objective function per split where a feature is used. A feature used rarely but in very informative splits will have low weight but high gain. Gain is generally preferred because it directly reflects how much each split improves predictions on average, not just how often the feature appears.
What is Information Gain and how is it used to rank features in Decision Trees?
Information Gain measures the reduction in entropy (uncertainty) achieved by splitting on a feature. Entropy for a node is H = −Σ pᵢ log₂(pᵢ), where pᵢ is the proportion of each class. Information Gain = H(parent) − weighted average H(children). Features that produce high information gain create splits with more homogeneous child nodes and are ranked as more important. The ID3 algorithm uses information gain as its split criterion; C4.5 uses a variant called gain ratio to correct for ID3's bias toward high-cardinality features.
How does L1 (Lasso) regularization act as a feature selector through feature importance?
L1 regularization adds a penalty equal to the sum of absolute values of the model's coefficients to the loss function: Loss_new = Loss + λΣ|wᵢ|. During optimization, this penalty drives many coefficients to exactly zero, effectively removing those features from the model. Features with non-zero coefficients after Lasso training are the ones the model considers important. Unlike L2 (Ridge) regularization, which only shrinks coefficients, L1 can fully zero them out, making it a natural built-in feature selector.
What is Recursive Feature Elimination (RFE) and how does it leverage feature importance?
RFE iteratively trains a model, ranks all features by their importance (using the model's built-in scoring like coefficients or impurity reduction), removes the least important feature(s), then retrains. This cycle repeats until a target number of features remains. RFE uses the model's own importance criterion to make each removal decision, making it model-aware. The main drawback is computational cost: with n features, you must train the model O(n) times.
from sklearn.feature_selection import RFE
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
model = RandomForestClassifier(n_estimators=50, random_state=42)
rfe = RFE(estimator=model, n_features_to_select=2)
rfe.fit(X, y)
print("Selected features:", rfe.support_)
print("Feature ranking:", rfe.ranking_)Why can multicollinearity mislead feature importance scores, and how does it affect different model types differently?
Multicollinearity means two or more features are highly correlated with each other. In linear models, it makes coefficient estimates unstable — a correlated feature pair may each show small coefficients even though together they explain the target well. In tree-based models, importance is split between correlated features: if one feature is chosen for a split, the other (which would work equally well) is less likely to be selected later, so both appear less important than they truly are. Solutions include removing redundant features, PCA, or using SHAP which better distributes credit among correlated predictors.
What is the key difference between SHAP values and permutation importance in terms of granularity of explanation?
Permutation importance gives a single global number per feature — the average performance drop when that feature is shuffled — but reveals nothing about individual predictions. SHAP values provide both local (per-prediction) and global (average of absolute values) importance. SHAP also distributes importance more fairly among correlated features by considering all orderings, while permutation importance can assign inflated importance to one member of a correlated pair. When you need to explain why a specific prediction was made, SHAP is the right tool; for a quick global ranking, permutation importance suffices.
How can we estimate feature importance for a neural network, which lacks a built-in importance attribute?
Neural networks have no native feature_importances_ attribute, so we use model-agnostic or gradient-based techniques. Permutation importance can be applied post-training by shuffling each input and measuring the performance drop. Gradient-based saliency maps compute the gradient of the output with respect to each input: |∂ŷ/∂xᵢ| — larger gradients indicate higher sensitivity. SHAP's DeepExplainer or GradientExplainer can compute Shapley values efficiently for neural networks. All these methods treat the trained network as a black box and require no changes to its architecture.
Why does tree-based MDI importance overestimate the importance of high-cardinality continuous or categorical features?
When a Decision Tree searches for the best split, it evaluates every candidate threshold. A continuous feature with many unique values presents many more thresholds than a binary feature, giving it more opportunities to appear as the best split purely by chance — even if its true predictive signal is no stronger. This selection bias causes high-cardinality features to accumulate more impurity reduction over the tree ensemble, inflating their MDI scores. Permutation importance avoids this because it is evaluated on held-out data where chance splits cannot inflate scores.
Does the type of problem (classification vs. regression) change how Random Forest computes feature importance?
The underlying concept is identical — each feature's importance equals the total reduction in node impurity it achieves across all trees. What changes is the impurity measure: classification forests use Gini impurity or entropy, while regression forests use Mean Squared Error (variance reduction). Both normalize scores to sum to 1. The interpretation also remains the same: higher score → the feature is more influential in separating classes (classification) or explaining variance (regression).
What is drop-column importance, and what makes it accurate but impractical for large feature sets?
Drop-column importance retrains the model completely after removing one feature and measures the resulting performance drop compared to the full model. Because retraining exposes the model to the full interaction of the remaining features, it gives an accurate estimate of how much each feature contributes. The severe drawback is cost: with p features, you must train p+1 models total. For a dataset with 500 features and a slow model, this becomes prohibitively expensive. Permutation importance approximates this without retraining, making it far more practical.
Why can target encoding a high-cardinality categorical feature artificially inflate its feature importance?
Target encoding replaces each category with the mean target value for that category. When computed on the training set itself without cross-validation, the encoded values correlate extremely closely with the target by construction — this is a form of data leakage. Models assign very high feature importance to such a leaky encoded feature because it essentially encodes the answer. Always use cross-validated or leave-one-out target encoding to ensure the encoding is computed only from samples not used in that particular fold's model training.
How can using training-set feature importances for feature selection lead to overfitting?
If feature importance is computed on the same data used to train the model, and that importance is then used to select features before evaluating on the same training set, you create a circular loop that can overfit to training-set patterns. Features important on training data may not generalize to new data. Best practice is to compute feature importance inside each cross-validation fold (only on the fold's training split) or on a dedicated validation set, then evaluate the final pipeline on a completely separate test set.
Why might feature importance rankings be unstable across different training runs, and how do you stabilize them?
When two features have similar predictive power or are correlated, which one gets selected for a split can vary between runs due to random bootstrapping or initialization. This causes importance rankings to flip, especially for mid-tier features. To improve stability: fix the random seed for reproducibility, average importances over many independent model runs, or use cross-validated importance (compute importance in each fold and average across folds). High instability between two features is also a signal that they may be interchangeable.
What is a Partial Dependence Plot (PDP) and how does it complement feature importance scores?
A Partial Dependence Plot shows how a model's predicted output changes as one feature's value sweeps across its range, while all other features are averaged over the dataset. Feature importance gives a single number summarizing how much a feature contributes; a PDP shows the shape and direction of that contribution (linear, non-linear, monotone, or threshold-like). Together they give a fuller picture: importance tells you how strong a feature's effect is; PDP tells you what that effect looks like.
What is variance threshold feature selection and when is it most useful?
Variance threshold removes features whose values barely change across samples (near-constant features). If a feature is almost always the same value, it carries almost no information to distinguish between data points. For example, a feature where 99% of values are 0 has very low variance and can be dropped cheaply. This is a fast, unsupervised pre-processing step useful for removing near-constant or single-value columns, but it does not detect weakly predictive or interacting features — for those, model-based importance is needed.
from sklearn.feature_selection import VarianceThreshold
import numpy as np
X = np.array([[1,0,1],[0,0,1],[1,0,0],[0,0,1]])
# Remove features with variance below 0.2
selector = VarianceThreshold(threshold=0.2)
X_reduced = selector.fit_transform(X)
print(X_reduced)How do the coefficients of a standardized linear regression model represent feature importance?
In a linear model y = w₁x₁ + w₂x₂ + … + b, the coefficient wᵢ represents how much the prediction changes for a one-unit increase in xᵢ. If all features are standardized (zero mean, unit variance), then the scale differences are removed and larger absolute coefficients directly indicate more important features. Without standardization, a feature measured in thousands might have a tiny coefficient even if it is very influential, making raw coefficients a misleading importance measure.
Does feature scaling (standardization or normalization) affect feature importance in a Random Forest? Why or why not?
No, feature scaling has no effect on tree-based feature importance. Decision Trees and ensembles like Random Forest split based on the relative ordering of feature values, not their absolute magnitudes. Multiplying or shifting all values of a feature by a constant does not change which threshold produces the best split, so importance scores remain identical before and after scaling. In contrast, linear model coefficients and distance-based methods (like KNN) are sensitive to scaling, so scaling is necessary for those approaches.
What is 'data leakage' in the context of feature importance, and give a concrete example?
Data leakage occurs when a feature encodes information about the target that would not be available at prediction time in the real world, causing the model to assign it artificially high importance. For example, if you are predicting whether a bank loan will default and include the feature 'loan_recovery_amount' — which is only known after a default occurs — the model assigns it enormous importance, but using it in production is impossible. Leaky features appear critical in training but make the model fail in deployment.
Why is computing feature importance for time-series data more challenging than for independent tabular data?
In time series, observations are temporally ordered and correlated — past values influence future ones. Standard permutation importance shuffles feature values randomly, which destroys the temporal structure and produces misleading scores. Lag features and rolling averages are also highly correlated, exacerbating multicollinearity bias in MDI. Proper practice requires time-aware cross-validation (walk-forward splits), permuting only within each walk-forward window, and being careful to prevent lookahead bias when computing any importance metric.
How can you aggregate feature importances from multiple diverse models to get a more robust ranking?
Compute feature importance separately from each base model (e.g., a Random Forest and a Lasso), normalize each model's scores to sum to 1 so they are on the same scale, then average the normalized scores across models. Features that rank highly across multiple models with different inductive biases are robustly important. You can also measure agreement using Spearman rank correlation between model importance lists — high agreement means the ranking is reliable; disagreement flags features that may be model-specific or interact with other features differently.
What does a SHAP summary plot show, and how do you read it?
A SHAP summary plot displays all features on the y-axis (ordered by mean absolute SHAP value) and shows individual SHAP values as colored dots along the x-axis. The x-position shows whether that feature pushed the prediction up (positive) or down (negative) for a given sample. Dot color indicates the feature's actual value (red = high, blue = low), revealing the direction of influence. Features at the top of the plot are most important globally; the spread of dots shows how the importance varies across samples.
How does one-hot encoding a high-cardinality categorical feature affect tree-based feature importance?
One-hot encoding a categorical feature with k categories creates k binary columns. The original feature's total importance is now fragmented across k sparse columns, each capturing only one category. The model assigns small importance to each binary column, making the original feature appear unimportant overall even if it is genuinely predictive. Alternative encodings (target encoding, ordinal encoding, or using native categorical support in LightGBM/CatBoost) keep the feature unified and produce more interpretable importance scores.
What is 'feature importance stability' and why should you test it before deploying a model?
Feature importance stability refers to how consistently the same features are ranked as important across different training runs, data subsets, or time periods. Unstable importances suggest the model is sensitive to data fluctuations and the top features may change with new data, undermining trust in any business or clinical decisions made based on those rankings. Before deployment, test stability by computing importance across multiple cross-validation folds or bootstrap samples; features consistently appearing in the top group are safe to act on, while volatile rankings warrant further investigation.
How does sklearn's permutation_importance function differ from the built-in feature_importances_ attribute of a Random Forest?
The built-in `feature_importances_` attribute returns MDI scores computed during training — it averages impurity reductions over all trees and is fast but biased toward high-cardinality features and can overfit training patterns. `permutation_importance` from sklearn computes post-training importance on a specified dataset (usually a validation set) by shuffling each feature and measuring the change in a given scoring metric. It is slower but is evaluated on out-of-sample data, making it more reliable as a measure of true generalization value.
from sklearn.inspection import permutation_importance
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
X, y = load_iris(return_X_y=True)
X_train, X_val, y_train, y_val = train_test_split(X, y, random_state=42)
model = RandomForestClassifier(n_estimators=100, random_state=42).fit(X_train, y_train)
result = permutation_importance(model, X_val, y_val, n_repeats=10, random_state=42)
print(result.importances_mean)In a Random Forest, if you observe that two highly correlated features each show low feature importance, what might be happening?
This is the classic multicollinearity split problem. When two features are highly correlated, the forest uses whichever happens to be selected (due to random feature subsampling) at each split. The total importance that would belong to a single strong predictor is divided between the two correlated features across different trees, making each appear only half as important. This can cause you to mistakenly discard both. Checking their combined importance (or using permutation importance or SHAP) will reveal their true collective value.
What is the relationship between entropy and feature importance in Decision Trees that use information gain?
Entropy measures the disorder or uncertainty in a set of labels: H = −Σ pᵢ log₂(pᵢ). A feature's information gain at a split is the difference in entropy before and after the split, weighted by the fraction of samples going to each child: IG = H(parent) − Σ (nᵢ/n) × H(childᵢ). Over all splits in the tree, a feature's total accumulated information gain is its importance. Features that produce splits where children are pure (low entropy) achieve high information gain and rank as most important.
Related topics in Machine Learning
Put it into practice with AI Learning
This topic — and hundreds more — comes from our free, offline AI Learning app. Practice Q&A, quizzes and cheat sheets anywhere, no account needed.
- Free
- No account
- Works offline