SHAPSelector<T>

Models & Types · AiDotNet.Preprocessing.FeatureSelection.ModelAgnostic

SHAP (SHapley Additive exPlanations) based feature selection.

Inheritance: TransformerBase<T, Matrix<T>, Matrix<T>> → SHAPSelector<T>

Implements: IFixedCountFeatureSelector<T>, ISupervisedFeatureSelector<T>

For Beginners

SHAP answers: “How much did each feature contribute to this prediction?”

Imagine a team of features making a prediction. SHAP figures out each player’s contribution fairly - not just by removing them, but by considering every possible team combination they could have been in.

Features with high absolute SHAP values (positive or negative) are important. SHAP provides both global importance and local explanations per sample.

How It Works

SHAP uses Shapley values from game theory to explain feature importance. Each feature’s importance is its average contribution to predictions across all possible feature combinations.

The algorithm (Kernel SHAP approximation):

  1. Sample feature coalitions (subsets of features)
  2. Weight coalitions by SHAP kernel
  3. Predict with masked features (baseline substitution)
  4. Solve weighted least squares for Shapley values

Constructors

Constructor Summary
SHAPSelector(Func<Matrix<T>, Vector<T>> predictFunc, int nFeaturesToSelect, int nSamples, int nCoalitions, int? randomState, int[] columnIndices) Creates a new instance of SHAPSelector.

Properties

Property Summary
Vector<T> ShapValues { get; } Gets the computed SHAP values (mean absolute).
bool SupportsInverseTransform { get; } Gets whether this transformer supports inverse transformation.
int NFeaturesToSelect { get; } Gets the number of features to select.
int[] SelectedIndices { get; } Gets the indices of selected features.

Methods

Method Summary
Matrix<T> FitTransform(Matrix<T> data, Vector<T> target) Fits and transforms the data.
bool[] GetSupportMask() Gets a boolean mask indicating which features are selected.
string[] GetFeatureNamesOut(string[] inputFeatureNames) Gets the output feature names after transformation.
void Fit(Matrix<T> data, Vector<T> target) Fits SHAP selector by computing Shapley values for each feature.