Rank features without believing MDI - it is in-sample, it favours columns with many distinct values, and it splits credit between substitutable features; MDA under-states collinear pairs and leaks outright on a shuffled k-fold. TRIGGER - feature_importances_, feature importance, MDI, mean decrease impurity, Gini importance, MDA, mean decrease accuracy, permutation importance, permutation_importance, single feature importance, SFI, clustered feature importance, "which features matter", "my random forest says this random column is important", "importance changes every run", correlated features importance, substitution effect, Lopez de Prado chapter 8, AFML feature importance. SKIP for purged and embargoed cross-validation itself (lib-purgedcv), for the overlapping-label weights (sample-weights-and-uniqueness), for feature construction and causality (signal-construction), for factor-return attribution (factor-models), and for the trials a feature search spends (backtest-validation).
Ships in fin-ml. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Three methods, three different lies, and the worst of them is the default. Advances in
Financial Machine Learning (Lopez de Prado 2018), chapter 8. feature_importances_ is one
attribute access away and it is in-sample, biased toward high-cardinality columns, and split
between features that substitute for each other. Permutation importance fixes the first two and
not the third. And every one of them is meaningless if the fold was shuffled.
Every number below is printed by scripts/feature_importance.py (numpy, seed 0, 23 s,
scikit-learn optional — the script carries its own CART and bagged forest, so the claims hold
on a bare install and are checked against sklearn when it is present).
n = 1,500. y = 1.00 * x0 + 0.60 * x2 + e, where e is a rolling mean of 25 shocks — each row's
target is fully explained by its own features, and neighbouring rows share most of their
residual. That is what an overlapping label horizon does, and it is what makes section 5 happen.
| feature | distinct values | corr with x0 | corr with y | truth |
|---|---|---|---|---|
| x0 | 1,500 | 1.000 | 0.425 | informative, coefficient 1.00 |
| x1 | 1,500 | 0.995 | 0.426 | a noisy copy of x0 — no independent information |
| x2 | 1,500 | 0.005 | 0.293 | informative, coefficient 0.60 |
| x3 | 1,500 | 0.032 | 0.016 | irrelevant, continuous |
| x4 | 2 | 0.007 | 0.011 | irrelevant, binary |
| x5 | 5 | 0.018 | 0.024 | irrelevant, five levels |
| x6 | 1,500 | 0.017 | -0.092 | irrelevant, a random walk |
feature_importances_ actually computesThe per-node formula lives in the compiled _tree.pyx, which the wheel does not ship — so it is
verified numerically here, by rebuilding it from the public tree_ arrays:
imp[feature[i]] += wn[i]*impurity[i] - wn[L]*impurity[L] - wn[R]*impurity[R] # internal nodes
imp /= wn[root]; imp /= imp.sum()
✅ Against sklearn 1.4.2 on the same data: max abs diff 0.00e+00 for a single
DecisionTreeRegressor, and 0.00e+00 for a RandomForestRegressor when the per-tree
importances are aggregated as _forest.py does — ✅ source-verified there:
np.mean(all_importances, axis=0) over trees with node_count > 1, then / np.sum(...).
✅ Source-verified in sklearn/tree/_classes.py and sklearn/ensemble/_forest.py, the docstring
of feature_importances_ already says it: "impurity-based feature importances can be misleading
for high cardinality features (many unique values)", and points at
sklearn.inspection.permutation_importance. The warning is in the docstring nobody opens, not
in a runtime warning.
✅ Measured, 25 trees, depth 5, max_features 3 of 7, min_samples_leaf 20:
| feature | MDI | rank | truth |
|---|---|---|---|
| x0 informative | 0.3023 | 2 | informative |
| x1 collinear copy | 0.3054 | 1 | no information |
| x2 informative | 0.2396 | 3 | informative |
| x3 noise, high card | 0.0261 | 5 | irrelevant |
| x4 noise, binary | 0.0063 | 7 | irrelevant |
| x5 noise, 5 levels | 0.0075 | 6 | irrelevant |
| x6 noise, random walk | 0.1127 | 4 | irrelevant |
✅ sklearn's RandomForestRegressor on the same data with the same hyper-parameters:
0.3604 0.2718 0.2336 0.0234 0.0022 0.0094 0.0992 — same shape, same conclusions.
MDA = the drop in out-of-sample R² when one column is permuted. SFI = the out-of-sample R² of a model fitted on that column alone. ✅ Both on an embargoed forward split (3 folds, 30 rows embargoed either side, 3 permutations per feature):
| feature | MDA | rank | SFI | rank |
|---|---|---|---|---|
| x0 informative | 0.1165 | 2 | 0.1037 | 1 |
| x1 collinear copy | 0.0585 | 3 | 0.0913 | 2 |
| x2 informative | 0.1269 | 1 | 0.0203 | 3 |
| x3 noise, high card | -0.0018 | 7 | -0.0806 | 6 |
| x4 noise, binary | -0.0011 | 5 | -0.0543 | 4 |
| x5 noise, 5 levels | 0.0016 | 4 | -0.0545 | 5 |
| x6 noise, random walk | -0.0015 | 6 | -0.4324 | 7 |
Cluster the features first (single linkage on |correlation| ≥ 0.8 — ✅ it recovers exactly
[[x0, x1], [x2], [x3], [x4], [x5], [x6]]), then permute each cluster jointly:
| cluster | clustered MDA | rank |
|---|---|---|
| x0+x1 | 0.3367 | 1 |
| x2 | 0.1223 | 2 |
| x5 | 0.0009 | 3 |
| x3 | -0.0004 | 4 |
| x4 | -0.0010 | 5 |
| x6 | -0.0018 | 6 |
✅ The pair scores 0.3367 against 0.1165 for its best single member — a factor of 2.9 — and x2, which is in a cluster of its own, is unchanged (0.1223 against 0.1269, inside the noise). This is the answer to substitution: stop asking which of two interchangeable features matters and ask whether the group does.
Same MDA, same model, one difference — a shuffled k-fold instead of an embargoed forward split:
| feature | MDA, shuffled k-fold | MDA, embargoed forward |
|---|---|---|
| x0 informative | 0.0758 | 0.1165 |
| x1 collinear copy | 0.0937 | 0.0585 |
| x2 informative | 0.1228 | 0.1269 |
| x3 noise, high card | 0.0007 | -0.0018 |
| x4 noise, binary | -0.0002 | -0.0011 |
| x5 noise, 5 levels | -0.0023 | 0.0016 |
| x6 noise, random walk | +0.0521 | -0.0015 |
🚨 The shuffled fold makes a random walk the third most important feature, at 41 % of x0's score. The residual is a 25-row rolling mean, so a shuffled test row's neighbours are in the training set, and a slowly-varying column identifies which neighbours. ✅ The overall out-of-sample R² tells the same story: 0.2659 shuffled against 0.1626 embargoed — the shuffled number is 64 % too high, and it is the number that would have gone in the report.
The embargoed forward split in this script is the minimum honest version. The real fix needs
each label's actual resolution time and belongs to
../../../fin-libraries/skills/lib-purgedcv/SKILL.md. Do not re-implement it.
feature_importances_. Section 3. In-sample, cardinality-biased,
substitution-split, and it ranks a random walk fourth.../../../fin-core/skills/backtest-validation/scripts/trial_ledger.py.../sample-weights-and-uniqueness/SKILL.md).scripts/feature_importance.py — a CART regression tree with exact variance-reduction splits
and MDI accumulation, a bagged forest, MDA (single and clustered), SFI, single-linkage
correlation clustering, shuffled and embargoed splits, and the numerical reconstruction of
sklearn's feature_importances_ from tree_. numpy only; sklearn is imported inside one
function. Seed 0, 23 s.../../../fin-libraries/skills/lib-purgedcv/SKILL.md owns purged and embargoed cross-validation —
section 6 is the reason it matters here, and the fix lives there.
../sample-weights-and-uniqueness/SKILL.md owns the weights that go with overlapping labels.
../triple-barrier-labeling/SKILL.md produces the labels whose overlap causes section 6.
../../../fin-core/skills/signal-construction/SKILL.md owns building the features causally in the
first place. ../../../fin-models/skills/factor-models/SKILL.md owns attribution of returns to
factors, which is a different question from attribution of a model to its inputs.
../../../fin-core/skills/backtest-validation/SKILL.md owns the trial count of a feature search.
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
7 skills in this plugin
/bet-sizingTurn a predicted probability into a position - the 2*Phi(z)-1 size curve, averaging concurrent bets instead of adding them, discretising to buy turnover, and the concurrency budget whose divisor is usually a look-ahead. TRIGGER - bet sizing, getSignal, get_signal, getBetSize, bet size from probability, avgActiveSignals, average active bets, discreteSignal, discrete signal, step size, position from predict_proba, "how big should this trade be", "my positions flip every bar", turnover from a probability, concurrent bets leverage, budgeting bets, Lopez de Prado chapter 10, AFML bet sizing. SKIP for the Kelly fraction and how large the book should be overall (position-sizing-kelly - it owns Kelly, do not repeat it), for the secondary model that produces the probability (meta-labeling), for volatility targeting and risk budgets (portfolio-and-risk), and for the execution schedule once the size is chosen (execution-algorithms)./fractional-differentiationMake a price series stationary without throwing away the memory a model needs - the weight recursion, the fixed-width window, and the scan for the smallest d that passes ADF. TRIGGER - fractional differentiation, fractional differencing, fracdiff, frac_diff, frac_diff_ffd, get_weights_ffd, plotMinFFD, plot_min_ffd, "minimum d", ARFIMA, long memory, (1-B)^d, binomial weights, fixed-width window fracdiff; "my model only sees returns", "prices are non-stationary so I differenced them", "ADF says my feature is non-stationary", "should I feed prices or returns to the model", Lopez de Prado chapter 5, AFML fracdiff. SKIP for the cointegration ADF table and the fitted-residual null (stat-arb-cointegration), for unit-root and ARIMA model selection generally (time-series-forecasting-models), for making a feature causal and its warm-up (signal-construction), and for labels rather than features (triple-barrier-labeling)./fin-ml:feature-importance-financialfrom fin-ml
npx claudepluginhub howard-lynn-ye/fin-rsi --plugin fin-ml