Feature Importance#
Aggregation, extraction, and stability analysis of feature importance scores across outer folds.
FeatureImportanceAggregator#
- class nestkit.importance.FeatureImportanceAggregator(results, method='auto', feature_names=None, shap_type='auto', normalize=True)[source]#
Bases:
objectAggregate feature importances across nested CV outer folds.
Extracts importance scores from each outer-fold estimator, optionally normalizes them, and computes summary statistics including mean, standard deviation, coefficient of variation, and rank-based diagnostics. Also supports SHAP-based model-agnostic importances.
- Parameters:
results (_BaseNestedCVResults) – Fitted nested CV results object. Must have been produced with
return_estimator=Trueso that per-fold estimators are available.method ({"auto", "model", "shap"}, default="auto") –
Importance extraction strategy.
"auto"/"model"– usefeature_importances_orcoef_from the fitted estimator."shap"– compute SHAP values on the outer test fold.
feature_names (list[str] or None, optional) – Human-readable feature names. If
None, inferred fromresults.feature_names_in_when available, otherwisefeature_0,feature_1, etc.shap_type ({"tree", "kernel", "linear", "auto"}, default="auto") – SHAP explainer backend. Only used when method is
"shap".normalize (bool, default=True) – If
True, absolute importances are rescaled to sum to 1 within each fold.
- ranks_matrix_#
Shape
(n_folds, n_features). Rank of each feature per fold.- Type:
- Raises:
ValueError – If results does not contain fitted estimators.
- Parameters:
Examples
>>> agg = FeatureImportanceAggregator(results) >>> agg.compute() >>> agg.summary_.head()
See also
nestkit.importance.extractors.extract_model_importance,nestkit.importance.extractors.compute_shap_importance,nestkit.importance.stability.nogueira_stability_index- compute(X=None, y=None)[source]#
Extract and aggregate importances across all outer folds.
Iterates over each outer-fold result, extracts importances using the configured method, optionally normalizes, and builds the
importances_matrix_andsummary_attributes.- Parameters:
X (array-like or None, optional) – Full feature matrix. Required only when
method="shap"so that the outer test-fold subset can be sliced for the SHAP explainer.y (array-like or None, optional) – Target vector. Currently unused; reserved for future supervised importance methods.
- Returns:
self, to allow method chaining.- Return type:
- Raises:
ValueError – If
method="shap"and X isNone.ValueError – If method is not one of the recognised strategies.
Notes
When
method="shap", SHAP values are computed only on the outer test fold of each split to avoid data leakage.See also
extract_model_importanceModel-native extraction.
compute_shap_importanceSHAP-based extraction.
- stability_index(top_k=10)[source]#
Compute the Nogueira et al. (2018) stability index for the top-k features.
Measures how consistently the same features appear in the top-k set across outer folds. A value of 1 indicates perfect agreement; 0 indicates random selection.
- Parameters:
top_k (int, default=10) – Number of top features to consider per fold.
- Returns:
Stability index in
[-1, 1].- Return type:
See also
nestkit.importance.stability.nogueira_stability_indexReferences
[1]Nogueira, S., Sechidis, K., and Brown, G. (2018). “On the Stability of Feature Selection Algorithms.” JMLR, 18(174), 1–54.
- consensus_features(criterion='top_k', top_k=10, min_frequency=0.8)[source]#
Identify features that are consistently important across folds.
Two selection strategies are available:
"top_k"– return the top_k features by mean importance (fromsummary_)."frequency"– return features that appear in the per-fold top-k set in at least min_frequency fraction of all folds.
- Parameters:
criterion ({"top_k", "frequency"}, default="top_k") – Selection strategy.
top_k (int, default=10) – Number of top features per fold (used by both criteria).
min_frequency (float, default=0.8) – Minimum fraction of folds in which a feature must appear in the top-k set. Only used when
criterion="frequency".
- Returns:
Feature names that satisfy the criterion.
- Return type:
- Raises:
ValueError – If criterion is not recognised.
Examples
>>> agg.compute() >>> agg.consensus_features("frequency", top_k=5, min_frequency=0.9)
- pairwise_rank_correlation()[source]#
Compute Spearman rank correlation of feature importances between all fold pairs.
High correlations indicate that the relative ordering of features is stable across outer folds.
- Returns:
One row per fold pair with columns
fold_i,fold_j,spearman_r, andp_value.- Return type:
Examples
>>> agg.compute() >>> agg.pairwise_rank_correlation()