Class KernelSHAP<T>
java.lang.Object
smile.feature.importance.KernelSHAP<T>
- Type Parameters:
T- the data type of model input objects.
- All Implemented Interfaces:
Serializable, SHAP<T>
Kernel SHAP (SHapley Additive exPlanations) is a model-agnostic method
to approximate Shapley values using a specially-weighted local linear
regression over feature coalitions.
Given an input instance x with M features and a background
reference dataset B, Kernel SHAP evaluates the expected model
prediction for sampled feature coalitions (where missing features are
marginalized over the background dataset B) and solves a constrained
weighted least squares problem that enforces the Shapley efficiency axiom:
sum_{j=1}^M phi_j = f(x) - E[f(z)]
References
- Lundberg, Scott M., and Su-In Lee. A unified approach to interpreting model predictions. NIPS, 2017.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordKernelSHAP hyperparameters. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum number of background rows when summarizing a dataset.static final doubleDefault ridge regularization for numerical stability. -
Method Summary
Modifier and TypeMethodDescriptiondouble[]Returns the expected baseline predictionE[f(z)]over the background dataset.intReturns the number of features.intReturns the number of model outputs per instance (1 for regression, k for classification).static KernelSHAP<double[]> of(double[][] background, double[] weights, Function<double[], double[]> predictor, KernelSHAP.Options options) Creates a KernelSHAP explainer for a vector-valued function ondouble[]with weighted background samples.static KernelSHAP<double[]> of(double[][] background, double[] weights, ToDoubleFunction<double[]> predictor, KernelSHAP.Options options) Creates a KernelSHAP explainer for a scalar function ondouble[]with weighted background samples.static KernelSHAP<double[]> Creates a KernelSHAP explainer for a vector-valued function ondouble[].static KernelSHAP<double[]> of(double[][] background, Function<double[], double[]> predictor, KernelSHAP.Options options) Creates a KernelSHAP explainer for a vector-valued function ondouble[].static KernelSHAP<double[]> of(double[][] background, ToDoubleFunction<double[]> predictor) Creates a KernelSHAP explainer for a scalar function ondouble[].static KernelSHAP<double[]> of(double[][] background, ToDoubleFunction<double[]> predictor, KernelSHAP.Options options) Creates a KernelSHAP explainer for a scalar function ondouble[].static KernelSHAP<double[]> of(double[][] background, Classifier<double[]> model) Creates a KernelSHAP explainer for aClassifier<double[]>model.static KernelSHAP<double[]> of(double[][] background, Classifier<double[]> model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aClassifier<double[]>model.static KernelSHAP<double[]> of(double[][] background, Regression<double[]> model) Creates a KernelSHAP explainer for aRegression<double[]>model.static KernelSHAP<double[]> of(double[][] background, Regression<double[]> model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aRegression<double[]>model.static KernelSHAP<Tuple> of(DataFrame background, double[] weights, DataFrameClassifier model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aDataFrameClassifiermodel with weighted background rows.static KernelSHAP<Tuple> of(DataFrame background, double[] weights, DataFrameRegression model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aDataFrameRegressionmodel with weighted background rows.static KernelSHAP<Tuple> of(DataFrame background, DataFrameClassifier model) Creates a KernelSHAP explainer for aDataFrameClassifiermodel.static KernelSHAP<Tuple> of(DataFrame background, DataFrameClassifier model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aDataFrameClassifiermodel.static KernelSHAP<Tuple> of(DataFrame background, DataFrameRegression model) Creates a KernelSHAP explainer for aDataFrameRegressionmodel.static KernelSHAP<Tuple> of(DataFrame background, DataFrameRegression model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aDataFrameRegressionmodel.options()Returns the KernelSHAP options.static double[][]sample(double[][] data, int maxSamples) Subsamples a background array to at mostmaxSamplesrepresentative rows using deterministic uniform stride selection (preserving RNG state).static DataFrameSubsamples a background DataFrame to at mostmaxSamplesrepresentative rows using deterministic uniform stride selection (preserving RNG state).double[]Returns the average of absolute SHAP values over a data frame.double[]shap(DataFrame data, KernelSHAP.Options options) Returns the average of absolute SHAP values over a data frame with custom options.double[]Returns the SHAP values.double[]shap(T rawInput, KernelSHAP.Options options) Returns the SHAP values with customKernelSHAP.Options(ignored by exact explainers such asTreeSHAPand linear SHAP).
-
Field Details
-
DEFAULT_BACKGROUND_SAMPLES
public static final int DEFAULT_BACKGROUND_SAMPLESDefault maximum number of background rows when summarizing a dataset.- See Also:
-
DEFAULT_RIDGE
public static final double DEFAULT_RIDGEDefault ridge regularization for numerical stability.- See Also:
-
-
Method Details
-
numFeatures
public int numFeatures()Returns the number of features.- Returns:
- the number of features.
-
numOutputs
public int numOutputs()Returns the number of model outputs per instance (1 for regression, k for classification).- Returns:
- the number of model outputs.
-
baseValues
public double[] baseValues()Returns the expected baseline predictionE[f(z)]over the background dataset.- Returns:
- a copy of the baseline prediction vector.
-
options
-
shap
Returns the average of absolute SHAP values over a data frame.- Parameters:
data- the data frame.- Returns:
- the average of absolute SHAP values.
-
shap
Returns the average of absolute SHAP values over a data frame with custom options.- Parameters:
data- the data frame.options- the KernelSHAP options, ornullto use default options.- Returns:
- the average of absolute SHAP values.
-
shap
Description copied from interface:SHAPReturns the SHAP values. For regression, the length of SHAP values is same as the number of features. For classification, SHAP values are ofp x k, wherepis the number of features andkis the classes. The first k elements are the SHAP values of first feature over k classes, respectively. The rest features follow accordingly. -
shap
Description copied from interface:SHAPReturns the SHAP values with customKernelSHAP.Options(ignored by exact explainers such asTreeSHAPand linear SHAP). -
of
Creates a KernelSHAP explainer for a scalar function ondouble[].- Parameters:
background- the background reference dataset.predictor- the scalar prediction function.- Returns:
- the KernelSHAP explainer.
-
of
public static KernelSHAP<double[]> of(double[][] background, ToDoubleFunction<double[]> predictor, KernelSHAP.Options options) Creates a KernelSHAP explainer for a scalar function ondouble[].- Parameters:
background- the background reference dataset.predictor- the scalar prediction function.options- the KernelSHAP options.- Returns:
- the KernelSHAP explainer.
-
of
public static KernelSHAP<double[]> of(double[][] background, double[] weights, ToDoubleFunction<double[]> predictor, KernelSHAP.Options options) Creates a KernelSHAP explainer for a scalar function ondouble[]with weighted background samples.- Parameters:
background- the background reference dataset.weights- optional background sample weights (may benullfor uniform weights).predictor- the scalar prediction function.options- the KernelSHAP options.- Returns:
- the KernelSHAP explainer.
-
of
Creates a KernelSHAP explainer for a vector-valued function ondouble[].- Parameters:
background- the background reference dataset.predictor- the vector prediction function.- Returns:
- the KernelSHAP explainer.
-
of
public static KernelSHAP<double[]> of(double[][] background, Function<double[], double[]> predictor, KernelSHAP.Options options) Creates a KernelSHAP explainer for a vector-valued function ondouble[].- Parameters:
background- the background reference dataset.predictor- the vector prediction function.options- the KernelSHAP options.- Returns:
- the KernelSHAP explainer.
-
of
public static KernelSHAP<double[]> of(double[][] background, double[] weights, Function<double[], double[]> predictor, KernelSHAP.Options options) Creates a KernelSHAP explainer for a vector-valued function ondouble[]with weighted background samples.- Parameters:
background- the background reference dataset.weights- optional background sample weights (may benullfor uniform weights).predictor- the vector prediction function.options- the KernelSHAP options.- Returns:
- the KernelSHAP explainer.
-
of
Creates a KernelSHAP explainer for aRegression<double[]>model.- Parameters:
background- the background reference dataset.model- the regression model.- Returns:
- the KernelSHAP explainer.
-
of
public static KernelSHAP<double[]> of(double[][] background, Regression<double[]> model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aRegression<double[]>model.- Parameters:
background- the background reference dataset.model- the regression model.options- the KernelSHAP options.- Returns:
- the KernelSHAP explainer.
-
of
Creates a KernelSHAP explainer for aClassifier<double[]>model. If the classifier supports soft predictions (model.isSoft()), posterior class probabilities are explained; otherwise one-hot class indicators are explained.- Parameters:
background- the background reference dataset.model- the classification model.- Returns:
- the KernelSHAP explainer.
-
of
public static KernelSHAP<double[]> of(double[][] background, Classifier<double[]> model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aClassifier<double[]>model.- Parameters:
background- the background reference dataset.model- the classification model.options- the KernelSHAP options.- Returns:
- the KernelSHAP explainer.
-
of
Creates a KernelSHAP explainer for aDataFrameRegressionmodel.- Parameters:
background- the background reference data frame.model- the DataFrame regression model.- Returns:
- the KernelSHAP explainer.
-
of
public static KernelSHAP<Tuple> of(DataFrame background, DataFrameRegression model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aDataFrameRegressionmodel.- Parameters:
background- the background reference data frame.model- the DataFrame regression model.options- the KernelSHAP options.- Returns:
- the KernelSHAP explainer.
-
of
public static KernelSHAP<Tuple> of(DataFrame background, double[] weights, DataFrameRegression model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aDataFrameRegressionmodel with weighted background rows.- Parameters:
background- the background reference data frame.weights- optional background sample weights (may benullfor uniform weights).model- the DataFrame regression model.options- the KernelSHAP options.- Returns:
- the KernelSHAP explainer.
-
of
Creates a KernelSHAP explainer for aDataFrameClassifiermodel.- Parameters:
background- the background reference data frame.model- the DataFrame classifier model.- Returns:
- the KernelSHAP explainer.
-
of
public static KernelSHAP<Tuple> of(DataFrame background, DataFrameClassifier model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aDataFrameClassifiermodel.- Parameters:
background- the background reference data frame.model- the DataFrame classifier model.options- the KernelSHAP options.- Returns:
- the KernelSHAP explainer.
-
of
public static KernelSHAP<Tuple> of(DataFrame background, double[] weights, DataFrameClassifier model, KernelSHAP.Options options) Creates a KernelSHAP explainer for aDataFrameClassifiermodel with weighted background rows.- Parameters:
background- the background reference data frame.weights- optional background sample weights (may benullfor uniform weights).model- the DataFrame classifier model.options- the KernelSHAP options.- Returns:
- the KernelSHAP explainer.
-
sample
Subsamples a background DataFrame to at mostmaxSamplesrepresentative rows using deterministic uniform stride selection (preserving RNG state).- Parameters:
data- the source data frame.maxSamples- the maximum number of background rows to retain.- Returns:
- the subsampled data frame.
-
sample
public static double[][] sample(double[][] data, int maxSamples) Subsamples a background array to at mostmaxSamplesrepresentative rows using deterministic uniform stride selection (preserving RNG state).- Parameters:
data- the source array.maxSamples- the maximum number of background rows to retain.- Returns:
- the subsampled array.
-