skip to content

How do you read a `shap.plots.beeswarm` of a readmission model, and what do its default ordering and `max_display` leave out?

level: middleimportance: should knowfreq 24%

answer

  1. one dot per patient per feature
  2. x is SHAP value, colour is feature value
  3. rows ranked by mean absolute value
  4. nine rows plus a summed tail
  5. summary_plot is the legacy name

basics

~20 s

In a shap beeswarm each row is a feature and each dot a patient: x is the SHAP value, colour the feature's value. Rows rank by mean absolute SHAP value, and by default only nine are named.

solid answer

~40 s

`shap.plots.beeswarm(sv)` takes a many-row, single-output Explanation. Each dot is one patient's SHAP value for one feature, placed on the x-axis and coloured by that patient's value of the feature, blue for low and red for high. So red dots far right on `prior_admissions_12m` read as "many prior admissions push readmission up". Rows are ordered by `order=Explanation.abs.mean(0)`, the mean absolute SHAP value, and `max_display=10` shows nine features plus a "Sum of N other features" row. That default hides a feature that matters a lot for a few patients but little on average; `order=sv.abs.max(0)` brings such features up and `max_display=None` shows everything. `shap.plots.bar(sv)` gives the same ranking as plain mean-absolute bars, and the top-level `shap.summary_plot` is the legacy function, not an alias.

code

python · 4 lines
python
shap.plots.beeswarm(sv)                        # 9 rows + "Sum of 21 other features"
shap.plots.beeswarm(sv, order=sv.abs.max(0))   # rank by largest single effect
shap.plots.beeswarm(sv, max_display=None)      # show all 30 features
shap.plots.bar(sv)                             # mean(|SHAP value|) per feature

go deeper

for a junior

Recall what each element encodes: rows are features, dots are patients, x is the SHAP value and colour is the feature's own value.

for a middle

Explain the defaults, mean-absolute ordering and max_display=10 with a summed row, and the arguments that change them, plus why summary_plot is the legacy function.

for a senior

Show you notice what the defaults hide, rare high-impact features and the grouped tail, and that you pick bar cohorts, clustering or a heatmap when the question needs them.

for a principal

Set reporting conventions for global plots across models, such as fixed ordering, display counts and scale labels, so reviewers compare like with like.

## What a beeswarm shows A beeswarm is the standard many-row view in shap. It takes an `Explanation` with one row per explained instance and one column per feature, a **2-D, single-output** Explanation, and draws: - **one horizontal row per feature**, labelled with the feature name; - **one dot per patient** in each row, placed on the x-axis at that patient's SHAP value for the feature; - **colour from the feature's value**, read from `.data`: blue for low, red for high, with a colour bar labelled "Feature value"; - **vertical piling** where many patients share similar SHAP values, which shows density. For the readmission model, which is explained on its log-odds margin, the x-axis is in log-odds. A row for `prior_admissions_12m` with red dots on the right and blue dots on the left reads: patients with many prior admissions get pushed up, patients with none get pushed down. A row whose colours are mixed on both sides says the feature's value alone does not determine the direction; something else in the row is involved. ## The defaults and what they hide `shap.plots.beeswarm(shap_values, max_display=10, order=Explanation.abs.mean(0), ...)`: 1. **Ordering.** `Explanation.abs.mean(0)` ranks features by mean absolute SHAP value across the batch. A feature that changes the margin a lot for 2% of patients and not at all for the rest ranks low. 2. **The cut.** With `max_display=10`, nine features get their own row and the tenth row reads "Sum of N other features", the summed SHAP values of everything below. On a 30-feature model that is "Sum of 21 other features". 3. **The input check.** A single row raises `ValueError` asking for many instances, and a three-class Explanation raises `ValueError` because each instance is not 1-D; slice one output first. Ways to change what is shown: - `order=sv.abs.max(0)` ranks by the largest single effect, so rare but strong features rise. - `max_display=None` shows every feature; `group_remaining_features=False` drops the summed row. - `shap.plots.heatmap(sv)` keeps patients visible as columns, ordered by `instance_order=Explanation.hclust()`, with `max_display=10` features as rows. ```python shap.plots.beeswarm(sv) # 9 rows + "Sum of 21 other features" shap.plots.beeswarm(sv, order=sv.abs.max(0)) # rare but large effects rise shap.plots.beeswarm(sv, max_display=None) # every feature shap.plots.bar(sv) # mean(|SHAP value|) per feature shap.plots.bar(sv.cohorts(2).abs.mean(0)) # two cohorts side by side ``` ## The bar plot beside it `shap.plots.bar` is the compact companion: | Call | Input | What it draws | |---|---|---| | `shap.plots.bar(sv)` | many rows | mean absolute SHAP value per feature; the 2-D input is collapsed with `.abs.mean(0)` | | `shap.plots.bar(sv[17])` | one row | that patient's signed SHAP values | | `shap.plots.bar(sv.cohorts(2).abs.mean(0))` | cohorts | one bar per cohort per feature; `cohorts(2)` splits patients with a two-leaf decision tree that predicts the SHAP values from the feature values | | `shap.plots.bar(sv, clustering=..., clustering_cutoff=0.5)` | many rows plus a feature clustering | the bars plus a dendrogram of redundant features | Like the beeswarm, it defaults to `max_display=10` and labels the remainder "Sum of N other features". Whether a mean-absolute ranking agrees with other importance measures is a separate question; this plot only draws the shap ranking. ## Narrating one in an interview Interviewers often hand over a beeswarm and ask for a reading. A clear narration follows the plot's own structure: 1. **State the scale and the sample.** "This is the readmission model on its log-odds margin, over last month's 4,000 discharges." 2. **Read the top rows by direction.** "Many prior admissions push readmission up; a booked follow-up pushes it down for almost everyone." 3. **Point out spread and mixing.** "Age has a long right tail of red dots: for a few older patients it matters a lot." 4. **Name what the plot hides.** "Nine features are shown; the summed row holds 21 more, and rare large effects may rank low under the mean-absolute ordering." 5. **Stop at the model.** "This describes how the model uses these features, not what drives readmission in patients." ## The legacy names In shap 0.52 the top-level `shap.summary_plot`, `shap.dependence_plot` and `shap.bar_plot` are the **legacy** functions, kept beside the `shap.plots` API: - `shap.summary_plot` was built for a values array plus a features matrix and shows **20** features by default, against the beeswarm's ten. - `shap.bar_plot` emits a `DeprecationWarning` (since 0.47.0) pointing to `shap.plots.bar`. - `shap.force_plot` and `shap.waterfall_plot`, by contrast, are plain aliases of the new functions. A team copying an older notebook gets a plot that looks similar but shows a different number of features, which is worth knowing before two versions of a report are compared.

  • How would you show in a shap bar plot that two readmission features carry overlapping information?
    Pass a feature clustering, `shap.plots.bar(sv, clustering=shap.utils.hclust(X, y))`. The plot draws a dendrogram beside the bars, and `clustering_cutoff=0.5` (the default) controls how much of the tree is shown. It signals redundancy; whether the credit split between the two features is meaningful is a separate judgement.
  • When would you choose `shap.plots.heatmap` over a beeswarm?
    When you need to see groups of patients rather than one dot cloud per feature. The heatmap puts patients on the x-axis, ordered by `instance_order=Explanation.hclust()` so similar explanations sit together, and colours each cell by its SHAP value for the top `max_display=10` features, which exposes subgroups the beeswarm averages over.

  • The top beeswarm row is the feature with the largest single effect on any patient.
  • Dot colour shows the size of the SHAP value.
  • The summed bottom row holds features that do not matter.
  • shap.summary_plot is just another name for shap.plots.beeswarm.
  • A beeswarm can take a three-class Explanation and plot every class.

Assumes shap 0.52, where shap.summary_plot is the legacy function and shap.bar_plot has warned with DeprecationWarning since 0.47.0.