Since shap 0.45, what shape does a three-class scikit-learn RandomForestClassifier's Explanation have, and how do you plot one class?
answer
- outputs moved to the last axis
- one ndarray, no longer a list
- rows, features, classes
- slice sv[:, :, k] before plotting
- binary scikit-learn gives two outputs
basics
~20 sIn shap 0.52 a three-class random forest returns one array of shape (rows, features, 3), with base_values of shape (rows, 3). Slice a class on the last axis, sv[:, :, 2] for a beeswarm or sv[0, :, 2] for one patient's waterfall.
solid answer
~40 sSince shap 0.45.0, a model with several outputs returns one `np.ndarray` with the outputs on the **last** axis instead of a list with one array per class. For a disposition model with classes home, home care and skilled nursing, explaining 2,000 patients with 24 features gives `sv.shape == (2000, 24, 3)` and `sv.base_values.shape == (2000, 3)`. The plots want one output: `shap.plots.beeswarm(sv[:, :, 2])` for the skilled-nursing class across patients, `shap.plots.waterfall(sv[0, :, 2])` for one patient. Code written before 0.45 that reads `shap_values[2]` to get class 2 now gets patient 2's (features, classes) slice. The per-model difference matters too: a binary scikit-learn classifier returns (rows, features, 2), while a binary XGBoost or LightGBM model returns (rows, features) and a scalar base value, its margin.
code
python · 6 linessv = explainer(X_disposition) # 3-class RandomForestClassifier
print(sv.shape) # (2000, 24, 3)
print(sv.base_values.shape) # (2000, 3)
shap.plots.beeswarm(sv[:, :, 2]) # skilled nursing, all patients
shap.plots.waterfall(sv[0, :, 2]) # skilled nursing, patient 0
shap.plots.bar({"home": sv[:, :, 0], "skilled nursing": sv[:, :, 2]})go deeper
Remember that a multi-class explanation carries a third axis for the classes and that plots need one class sliced out first.
Explain the 0.45.0 change from a list per class to one (rows, features, outputs) array, the matching base_values shape, and the slices for beeswarm and waterfall.
Show you can audit old explanation code: find integer class selectors, branch on the model's output count, and catch the silent row-for-class mix-up before plots ship.
Consider pinning the shap release with the model, since an upgrade across 0.45 changes array shapes that downstream reports and dashboards depend on.
## What changed in 0.45.0 shap's release notes for **0.45.0** record the change: SHAP values for models with multiple outputs became a single `np.ndarray` rather than a list. TreeExplainer's `shap_values` documentation says the same thing and gives the shape: one output yields `(#num_samples, *X.shape[1:])`, several outputs yield `(#num_samples, *X.shape[1:], #num_outputs)`. The `Explanation` that an explainer call returns follows the same layout, and shap 0.52 keeps it. For a **three-class discharge-disposition model**, a scikit-learn `RandomForestClassifier` predicting home, home care or skilled nursing, explained on 2,000 patients with 24 features: | Object | Shape | Meaning | |---|---|---| | `sv.values` | `(2000, 24, 3)` | One SHAP value per patient, feature and class | | `sv.base_values` | `(2000, 3)` | The expected output for each class, repeated per patient | | `sv[:, :, 2]` | `(2000, 24)` | Every patient, skilled-nursing class | | `sv[0, :, 2]` | `(24,)` | Patient 0, skilled-nursing class | | `sv[0]` | `(24, 3)` | Patient 0, every class | The output index follows the order in which the classifier stores its classes, so confirm which index is skilled nursing before labelling a plot. ## How the shape differs by model The number of outputs is whatever the model produces, so two binary classifiers can come back in different shapes: - **scikit-learn binary classifier** (for example a random forest): the model outputs a probability per class, so the values are `(rows, features, 2)` and the expected value has length 2. The positive class is `sv[:, :, 1]`. - **XGBoost or LightGBM binary model** explained on its default raw output: one margin per row, so the values are `(rows, features)` and the base value is a scalar. - **Regression model:** one output, `(rows, features)`, scalar base value. Code that switches between a forest and a booster for the same task therefore needs a different slice, and a `[:, :, 1]` written for the forest fails on the booster's 2-D result, which has no third axis to index. ## What breaks in code written before 0.45 Before 0.45.0, `shap_values = explainer.shap_values(X)` on a multi-class model returned a list, and `shap_values[2]` was the class-2 matrix. On the current layout: 1. `shap_values[2]` is **patient 2's** `(features, classes)` slice. Nothing errors at that line; the mistake surfaces later as a wrong or failing plot. 2. `shap.plots.beeswarm(sv)` on the 3-D Explanation raises a `ValueError` saying it does not support explanations whose instances have more than one dimension. 3. `shap.plots.waterfall(sv[0])` raises a `ValueError` because `sv[0]` is still 2-D. Its message suggests `shap_values[0, 0]` for multi-output models; on the current layout that index means row 0, feature 0, so spell the class on the last axis instead: `sv[0, :, 2]`. 4. `sv.cohorts(2)` raises a `ValueError` asking you to pick the output first, `shap_values[..., cohort_class].cohorts(2)`. ```python sv = explainer(X_disposition) # TreeExplainer on the 3-class forest print(sv.shape) # (2000, 24, 3) snf = sv[:, :, 2] # skilled nursing, (2000, 24) shap.plots.beeswarm(snf) shap.plots.waterfall(sv[0, :, 2]) # one patient, one class ``` ## Comparing classes side by side Each class has its own additivity: for patient `i` and class `k`, `sv.base_values[i, k] + sv.values[i, :, k].sum()` equals that class's output on the explained scale, the forest's predicted probability for class `k`. To compare classes globally: - `shap.plots.bar({"home": sv[:, :, 0], "home care": sv[:, :, 1], "skilled nursing": sv[:, :, 2]})` draws one bar per class for each feature, because `shap.plots.bar` accepts a dictionary of Explanations and collapses each to mean absolute values. - The ellipsis form `sv[..., 2]` is the same slice as `sv[:, :, 2]`. - Each class's base value is the forest's average predicted probability for that class over the reference data, so the three base values in a row sum to about 1, a quick sanity check that the class axis is the one you think it is. - The legacy top-level `shap.summary_plot` still draws a stacked per-class bar when handed the three-class Explanation, but it is the pre-Explanation API. ## Checklist when a multi-output explanation looks wrong - Print `sv.shape` and `sv.base_values.shape` before plotting. - Name the output you slice in the plot title. - Search old notebooks for `shap_values[<int>]` written as a class selector. - Re-run one known patient after any shap upgrade across 0.45 and compare the slice you plot with the class you meant. - Keep the class names next to the index in code, for example a small mapping from index to disposition, so a reordered class list cannot silently relabel a plot.
- A notebook from 2023 indexes `shap_values[1]` for the positive class of a scikit-learn binary forest. What does it do on shap 0.52?It selects patient 1's (features, 2) slice instead of the positive class. Since 0.45.0 the forest's two outputs live on the last axis, so the positive class is `shap_values[:, :, 1]`, and its expected value is the second of two.
- Why does the same code work for an XGBoost binary model but fail for a scikit-learn forest on the same task?The XGBoost model is explained on one raw margin, so its values are (rows, features) with a scalar base value. The forest outputs a probability per class, so its values are (rows, features, 2). Code must branch on `sv.values.ndim` or slice per model.
- A multi-class model still returns a list with one array per class.
- shap_values[2] selects the third class of a multi-class explanation.
- Every binary classifier returns SHAP values of shape (rows, features).
- beeswarm plots all three classes automatically from a 3-D Explanation.
- The class axis comes first, so sv[2] is the skilled-nursing class.
Assumes shap 0.52; since 0.45.0 multi-output models return one (rows, features, outputs) ndarray instead of a list per class.