AlphaNova
Back to Blog
How to Calculate Feature Importance from a Trained Model in Python

How to Calculate Feature Importance from a Trained Model in Python

Dominik Keller
August 4, 2026

Calculating Feature Importance in Python

Building a predictive model is only half the battle. Understanding why it makes certain predictions is often where the real value lies—especially in quantitative finance, where deploying an unexplainable black box can lead to costly surprises. Feature importance is one of the most direct tools we have for peering inside a trained model.

Why Feature Importance Matters in Predictive Modelling

Feature importance is a technique for assigning scores that reflect each input variable's contribution to the model's predictions. Instead of treating your model as an opaque function that magically produces outputs, you get a ranked list showing which features drive decisions.

This matters for three concrete reasons. First, it aids model interpretation: you can explain to stakeholders which market signals are actually being used. Second, it helps with debugging: if a feature you know should be irrelevant scores highly, something is wrong with your data pipeline or the model's assumptions. Third, it enables you to build more efficient models by stripping out noise, reducing dimensionality, and often improving out‑of‑sample performance.

In machine‑learning‑driven quantitative finance, where data is noisy and overfitting lurks around every corner, robust feature selection is not optional—it is essential.

Understanding Feature Engineering

Feature importance tells you which inputs matter, but the inputs themselves don't appear out of nowhere. Feature engineering—the craft of designing, transforming, and selecting the raw variables that feed your model—is what determines whether there's any signal to measure in the first place. For a practitioner's view on how this works at institutional scale, the Flirting with Models podcast episode Ben Wellington – Complex Feature Engineering at Two Sigma explores how one of the world's largest quant funds manages feature ideation, collinearity, forecast horizon, and the lifecycle of features—a valuable listen alongside the techniques covered here.

What Is Feature Importance?

Before we dive into Python implementations, we need a clear, working definition of what a feature importance score actually represents.

A Universal Concept for All Model Types

A feature importance score is a numeric indicator of how useful a feature is for predicting the target variable. This concept applies to both regression and classification problems. A score of zero would mean the model found no predictive value in that feature; a higher score means the model relied on it more heavily.

Critically, these scores are always relative within a given model. An importance of 0.15 for one feature and 0.05 for another simply tells you the first was roughly three times more influential for that specific model architecture.

Common Sources of Feature Importance Scores

Depending on the model you choose, importance scores come from different sources:

  • Statistical correlations: Pre‑modelling, a simple Pearson or Spearman correlation can tell you how strongly a feature relates to the target, but this misses non‑linear interactions.
  • Linear model coefficients: In a properly scaled linear regression or logistic regression, the magnitude of coefficients can serve as a direct importance measure, assuming features are on comparable scales.
  • Tree‑based importance: Ensemble methods like Random Forest, XGBoost, and LightGBM compute intrinsic importance scores based on how much each split reduces impurity. We will explore this in detail.
  • Permutation importance: A model‑agnostic technique that works with any fitted estimator by shuffling features and measuring the performance drop.

We will focus on the last two because they are the most versatile for the tabular, potentially high‑dimensional data common in quant finance.

Importance from Tree‑Based Models

Tree‑based models are the workhorses of tabular data modelling, and they come with a built‑in importance metric that is fast to compute and widely used.

How Decision Trees Assign Importance

The fundamental idea is gain‑based (or impurity‑based) importance. In a decision tree, each node splits the data on a single feature to make the child nodes as "pure" as possible. For regression, purity is measured by variance reduction; for classification, it is typically Gini impurity or entropy. The improvement—how much the split reduced the impurity compared to the parent node—is attributed to the feature that caused the split. Over the entire tree, these improvements are summed for each feature, weighted by the number of samples that passed through that node.

Aggregated Importance in Random Forests and Gradient Boosting

In ensemble models, a single decision tree rarely tells the full story. Random Forest and gradient boosting frameworks like XGBoost and LightGBM aggregate the per‑tree importance scores across all estimators in the ensemble. The result is a single, stable vector of scores representing each feature's direct contribution to reducing prediction error during training.

In scikit‑learn, any fitted tree‑based estimator exposes this vector through the .feature_importances_ attribute. In XGBoost, you can retrieve it via model.feature_importances_ or use the xgb.plot_importance() function for a quick visual. These scores sum to 1 and answer the question: "When the model was being built, which features were used most aggressively to carve the feature space into accurate predictions?"

Permutation Feature Importance

Intrinsic model metrics are useful, but they can sometimes tell a misleading story. Permutation importance provides a direct, out‑of‑sample alternative that works with any model.

The Core Idea: Shuffling to Break Predictive Power

Permutation importance is conceptually simple. If a feature genuinely matters, scrambling its values should hurt the model's performance. If the feature is irrelevant, shuffling it will have no effect.

The procedure works as follows: take a trained model and a validation set, measure a baseline performance metric (such as R² for regression or accuracy for classification), then randomly shuffle the values of a single feature column, breaking its relationship with the target and with other features. Pass the modified data through the model and measure the performance drop. Repeat this shuffle‑and‑measure process multiple times to estimate variability, and you get an importance score with error bars.

This method directly evaluates how much the model relies on a feature to make its predictions, without needing to retrain anything.

Implementing Permutation Importance in Python with sklearn.inspection

Scikit‑learn provides a battle‑tested implementation in sklearn.inspection.permutation_importance. You supply the fitted estimator, the validation data (features and target), the scoring metric, and the number of repeated shuffles. The function returns a dictionary containing the mean importance and standard deviation for each feature.

A typical call looks like this:

from sklearn.inspection import permutation_importance

result = permutation_importance(
    model, X_val, y_val,
    scoring='neg_mean_squared_error',
    n_repeats=10,
    random_state=42
)

The output result.importances_mean is an array of average performance drops. Sorting these descending gives a direct ranking of feature reliance. The standard deviations in result.importances_std let you filter out features whose importance is swamped by noise.

Comparing Gain‑Based and Permutation Importance

Both methods give you feature rankings, but they measure subtly different things. Choosing the right tool for the question at hand matters.

When Gain‑Based Importance Can Mislead

Gain‑based importance, being intrinsic to the training process, is computationally free once the model is fitted. But it has known biases. It can inflate scores for high‑cardinality features (features with many unique values, like a categorical ID column) because the tree has more opportunities to find splits that reduce impurity by chance during training. It also can give non‑zero scores to pure noise features that the model overfit to. This means that if you blindly trust .feature_importances_, you might retain a useless identifier column while discarding a genuinely predictive but low‑variance signal.

Advantages of Permutation Importance for Model Evaluation

Permutation importance is inherently an out‑of‑sample evaluation. Because it measures the consequence of removing a feature's information on unseen data, it better reflects true predictive value. Features that the model memorised during training but cannot generalise will show near‑zero permutation importance. This makes it the superior choice for feature selection when your goal is to build a model that performs well on new data. The trade‑off is computational cost: you must run the model many times over the shuffled dataset, which can be slow for large models and big validation sets.

MethodSourceComputationBest Use
Gain‑BasedIntrinsic to tree trainingFast, single passQuick model inspection
PermutationModel‑agnostic, out‑of‑sampleSlower, repeated passesReliable feature selection, debugging overfitting

Practical Feature Selection Using Importance Scores

Knowing which features matter is useful; acting on that knowledge is transformative.

Removing Low‑Importance Features

Once you have stable importance scores, the simplest strategy is to define a threshold and discard features whose importance falls below it. For permutation importance, you might eliminate features where the mean importance minus one standard deviation is less than zero—meaning you cannot confidently say the feature helps. For gain‑based importance, a common heuristic is to drop the tail of features that collectively account for only a few percentage points of total importance.

Iterative Feature Selection Pipelines

Thresholding is a good first pass, but more sophisticated approaches exist. Recursive Feature Elimination (RFE) uses a model's internal ranking to iteratively prune the weakest feature, refit, and re‑evaluate. Even without RFE, a manual iterative pipeline works well: fit a model, compute permutation importance, drop the least important feature, and repeat until performance on a held‑out validation set begins to degrade. This reduces complexity, often speeds up training times by an order of magnitude, and can improve generalisation by stripping away the noise that complex models are tempted to overfit to.

Feature Importance in Quantitative Finance

These techniques are not academic exercises. They solve real problems when building strategies that must survive rigorous out‑of‑sample testing.

Why Feature Interpretation Matters for Signal Forecasting

In quantitative finance, data is notoriously noisy and non‑stationary. A model that learns spurious patterns in training data can produce impressive backtests that completely collapse when deployed. Feature importance analysis cuts through this. When you see that a handful of stable features drive your signal, while 50 others contribute nothing, you have a simpler, more robust hypothesis. You can investigate why those top features work, building intuition about the market dynamic you have captured rather than blindly trusting an ensemble.

The article on The 'Walk‑Forward' Test: The Only Backtest That Matters explains how proper validation frameworks expose overfitting. Feature importance works hand‑in‑hand with walk‑forward validation: features that remain top‑ranked across multiple validation periods are truly persistent signals.

Building a Robust Predictor for AlphaNova Competitions

AlphaNova's competitions provide an ideal proving ground for these methods. Participants receive obfuscated tabular financial data—multiple assets per period, with masked feature names—and must submit a pure Python Predictor class that ranks assets by expected returns. Submissions are evaluated out‑of‑sample using the Sharpe ratio, and a greedy quality‑selection process admits only genuinely uncorrelated, overfit‑filtered signals.

This environment directly rewards disciplined feature selection. By applying permutation importance on a local walk‑forward validation split, you can identify the subset of obfuscated features that genuinely generalise. Discarding noisy columns leads to a leaner Predictor that is less likely to latch onto ephemeral patterns, resulting in better out‑of‑sample Sharpe ratios and a higher chance of passing the platform's anti‑overfitting screens. Participants keep full intellectual property and can develop these skills risk‑free, as no staking or token exposure is involved. For problems where Random Forest's robustness to noisy data might be an asset, the comparison in XGBoost vs Random Forest: When to Use Each is directly relevant to model choice before you even begin feature selection.

Conclusion and Next Steps

Gain‑based and permutation importance together give a complete picture of feature contributions. The first is fast and tells you how your tree‑based model was built; the second is rigorous and tells you which features the model truly depends on for generalisation. Python libraries like scikit‑learn and XGBoost make extraction and visualisation straightforward.

The next step is to apply these techniques to a project where robustness is scored mercilessly. Whether you are refining an existing strategy or building a new one from scratch, feature importance analysis will help you separate signal from noise. To put these skills to work on real‑world, obfuscated financial data and compete on the quality of your predictive signals, Join the latest AlphaNova competition.