facet.inspection.shap.ShapCalculator#
- class facet.inspection.shap.ShapCalculator(model, *, explainer_factory, interaction_values, n_jobs=None, shared_memory=None, pre_dispatch=None, verbose=None)[source]#
Base class for all SHAP calculators.
A SHAP calculator uses the
shap
package to calculate SHAP tensors for all observations in a given sample of feature values, then consolidates and aggregates results in a data frame.- Bases
- Generic types
~T_Model
- Metaclasses
- Parameters
model (
ShapCalculator
) – the model for which to calculate SHAP valuesexplainer_factory (
ExplainerFactory
[ShapCalculator
]) – the explainer factory used to create the SHAP explainer for this calculatorinteraction_values (
bool
) – ifTrue
, calculate SHAP interaction values, otherwise calculate SHAP valuesn_jobs (
Optional
[int
]) – number of jobs to use in parallel; ifNone
, use joblib default (default:None
)shared_memory (
Optional
[bool
]) – ifTrue
, use threads in the parallel runs; ifFalse
orNone
, use multiprocessing (default:None
)pre_dispatch (
Union
[int
,str
,None
]) – number of batches to pre-dispatch; ifNone
, use joblib default (default:None
)verbose (
Optional
[int
]) – verbosity level used in the parallel computation; ifNone
, use joblib default (default:None
)
Method summary
Calculate the SHAP values.
Check that the given feature matrix is valid for this calculator.
Attribute summary
Name for the feature index (= column index) of the resulting SHAP data frame.
Name of the index that is used to identify multiple outputs for which SHAP values are calculated.
The names of the inputs explained by this SHAP calculator, or
None
if no names are defined.[see superclass]
The main effects per observation and featuren (i.e., the diagonals of the interaction matrices), with shape \((n_\mathrm{observations}, n_\mathrm{outputs} \cdot n_\mathrm{features})\).
The names of the outputs explained by this SHAP calculator.
The SHAP interaction values per observation and feature pair, with shape \((n_\mathrm{observations} \cdot n_\mathrm{features}, n_\mathrm{outputs} \cdot n_\mathrm{features})\)
The SHAP values per observation and feature, with shape \((n_\mathrm{observations}, n_\mathrm{outputs} \cdot n_\mathrm{features})\)
n_jobs
Number of jobs to use in parallel; if
None
, use joblib default.shared_memory
If
True
, use threads in the parallel runs; ifFalse
orNone
, use multiprocessing.pre_dispatch
Number of batches to pre-dispatch; if
None
, use joblib default.verbose
Verbosity level used in the parallel computation; if
None
, use joblib default.The model for which to calculate SHAP values.
The explainer factory used to create the SHAP explainer for this calculator.
The SHAP values for all observations this calculator has been fitted to.
The names of the features for which SHAP values were calculated.
Definitions
- fit(__X, **fit_params)[source]#
Calculate the SHAP values.
- Parameters
- Return type
- Returns
self
- Raises
ValueError – if the observations are not a valid feature matrix for this calculator
- validate_features(features)[source]#
Check that the given feature matrix is valid for this calculator.
- Parameters
features (
DataFrame
) – the feature matrix to validate- Raises
ValueError – if the feature matrix is not compatible with this calculator
- Return type
- IDX_FEATURE = 'feature'#
Name for the feature index (= column index) of the resulting SHAP data frame.
- MULTI_OUTPUT_INDEX_NAME = 'output'#
Name of the index that is used to identify multiple outputs for which SHAP values are calculated. To be overloaded by subclasses.
- explainer_factory: pytools.expression.ExplainerFactory[T_Model]#
The explainer factory used to create the SHAP explainer for this calculator.
- feature_index_: Optional[pandas.Index]#
The names of the features for which SHAP values were calculated.
- abstract property input_names: Optional[List[str]]#
The names of the inputs explained by this SHAP calculator, or
None
if no names are defined.
- property main_effects: pandas.DataFrame#
The main effects per observation and featuren (i.e., the diagonals of the interaction matrices), with shape \((n_\mathrm{observations}, n_\mathrm{outputs} \cdot n_\mathrm{features})\).
- Raises
AttributeError – this SHAP calculator does not support interaction values
- Return type
- model: T_Model#
The model for which to calculate SHAP values.
- abstract property output_names: List[str]#
The names of the outputs explained by this SHAP calculator.
- shap_: Optional[pandas.DataFrame]#
The SHAP values for all observations this calculator has been fitted to.
- property shap_interaction_values: pandas.DataFrame#
The SHAP interaction values per observation and feature pair, with shape \((n_\mathrm{observations} \cdot n_\mathrm{features}, n_\mathrm{outputs} \cdot n_\mathrm{features})\)
- Raises
AttributeError – this SHAP calculator does not support interaction values
- Return type
- property shap_values: pandas.DataFrame#
The SHAP values per observation and feature, with shape \((n_\mathrm{observations}, n_\mathrm{outputs} \cdot n_\mathrm{features})\)
- Return type