Core objects¶
Types and coercion helpers used across all plotting functions to normalize SHAP values, interaction values, and enums.
Explanation
dataclass
¶
Explanation(
values: FloatArray,
base_values: FloatArray,
feature_names: tuple[str, ...],
data: FloatArray | None = None,
)
Normalized SHAP explanation used internally by every plot builder.
All arrays are stored two-dimensionally as (n_samples, n_features) so
plot code never has to special-case single instances.
Attributes:
| Name | Type | Description |
|---|---|---|
values |
FloatArray
|
SHAP values, shape |
base_values |
FloatArray
|
Model expected value per sample, shape |
feature_names |
tuple[str, ...]
|
Feature labels, length |
data |
FloatArray | None
|
Original feature values aligned with |
__post_init__ ¶
__post_init__() -> None
Validate array shapes are mutually consistent.
Source code in src/shaply/explanation.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
mean_abs ¶
mean_abs() -> FloatArray
Mean absolute SHAP value per feature (global importance).
Source code in src/shaply/explanation.py
94 95 96 | |
select_sample ¶
select_sample(index: int) -> Explanation
Return a single-sample explanation for index.
Source code in src/shaply/explanation.py
98 99 100 101 102 103 104 105 | |
to_explanation ¶
to_explanation(
values: ExplanationLike | ArrayLike | object,
*,
base_values: object = None,
data: ArrayLike | None = None,
feature_names: Sequence[str] | None = None,
output_index: int | None = None,
) -> Explanation
Coerce supported inputs into an :class:Explanation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ExplanationLike | ArrayLike | object
|
A |
required |
base_values
|
object
|
Model expected value(s). Scalar or per-sample. Defaults to zeros.
Ignored when |
None
|
data
|
ArrayLike | None
|
Original feature values aligned with the SHAP values. |
None
|
feature_names
|
Sequence[str] | None
|
Feature labels. Inferred from a DataFrame or generated when omitted. |
None
|
output_index
|
int | None
|
Class index to select when |
None
|
Returns:
| Type | Description |
|---|---|
Explanation
|
The normalized internal representation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided arrays have inconsistent or unsupported shapes. |
Source code in src/shaply/explanation.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
InteractionValues
dataclass
¶
InteractionValues(
values: FloatArray, feature_names: tuple[str, ...]
)
Normalized SHAP interaction tensor used by the interaction heatmap.
Attributes:
| Name | Type | Description |
|---|---|---|
values |
FloatArray
|
Interaction values, shape |
feature_names |
tuple[str, ...]
|
Feature labels, length |
__post_init__ ¶
__post_init__() -> None
Validate the tensor is a stack of square interaction matrices.
Source code in src/shaply/interaction.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
mean_abs_matrix ¶
mean_abs_matrix() -> FloatArray
Mean absolute interaction per feature pair, shape (n_features, n_features).
Source code in src/shaply/interaction.py
60 61 62 | |
to_interaction_values ¶
to_interaction_values(
values: ArrayLike | object,
*,
feature_names: Sequence[str] | None = None,
) -> InteractionValues
Coerce an interaction tensor (or .values-carrying object) into a model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ArrayLike | object
|
A |
required |
feature_names
|
Sequence[str] | None
|
Feature labels; inferred from the object or generated when omitted. |
None
|
Returns:
| Type | Description |
|---|---|
InteractionValues
|
The normalized internal representation. |
Source code in src/shaply/interaction.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
ColorScale ¶
Bases: StrEnum
Named color scales available for continuous encodings.
RED_BLUE reproduces the canonical SHAP diverging scheme
(blue for low feature values, red for high).
FeatureOrdering ¶
Bases: StrEnum
Strategy used to order features along the categorical axis of a plot.
Attributes:
| Name | Type | Description |
|---|---|---|
IMPORTANCE |
Order by mean absolute SHAP value (most important first). |
|
MAX_ABSOLUTE |
Order by the single largest absolute SHAP value across samples. |
|
ORIGINAL |
Keep the order of |
|
ALPHABETICAL |
Order features alphabetically by name. |
PlotType ¶
Bases: StrEnum
Kind of SHAP figure that shaply can render.