svgshap-dependence
SHAP dependence
How does one feature's value change its contribution, and does another feature modify it?
How to read it
- Read the shape: how a feature's value changes its contribution.
- Read the vertical spread at one value: another feature is modifying it.
- Read the colour to see which one.
- Callout 1: Crossing zero: the feature's effect flips sign here.
- Callout 2: Rising with age.
- Callout 3: Colour scale: the interacting feature.
Watch out
- Few rows at the extremes make those shapes unreliable.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Draws the run's numbers as given (computed by the host's cs-ml evaluation or the Python/R engine); the chart computes nothing statistical.
- The engine picks the colour feature (the strongest SHAP interaction, as the shap library does) or the user does; numeric colours run on a sequential ramp, categories on the categorical palette.
- Vertical spread at a fixed x is the effect of other features — colour separating that spread suggests an interaction.
# SHAP dependence — Codesplash Chart Lab (recipe "shap-dependence")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]
df = pd.read_csv("shap-dependence-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
V = "age (years)"
SHAP = "SHAP value (log-odds)"
COL = "BMI"
d = df.dropna(subset=[V, SHAP])
fig, ax = plt.subplots(figsize=(7, 4.8))
ax.axhline(0, color="grey", linewidth=0.8)
cmap = LinearSegmentedColormap.from_list("shap", [DIV[-1], DIV[0]])
sc = ax.scatter(d[V], d[SHAP], c=d[COL] if COL else PALETTE[0], cmap=cmap if COL else None, s=14, alpha=0.8)
if COL:
fig.colorbar(sc, ax=ax, label=COL)
ax.set_xlabel(V)
ax.set_ylabel(SHAP)
plt.show()Needs pandas, numpy, matplotlib · reads shap-dependence-demo.csv · app theme from the optional codesplash helper
# SHAP dependence — Codesplash Chart Lab (recipe "shap-dependence")
# controls: defaults
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else px.colors.qualitative.Plotly
df = pd.read_csv("shap-dependence-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
V = "age (years)"
SHAP = "SHAP value (log-odds)"
COL = "BMI"
d = df.dropna(subset=[V, SHAP])
marker = dict(size=6, opacity=0.8, color=d[COL], colorscale=[[0, DIV[-1]], [1, DIV[0]]], colorbar=dict(title=COL)) if COL else dict(size=6, color=PALETTE[0])
fig = go.Figure(go.Scatter(x=d[V], y=d[SHAP], mode="markers", marker=marker))
fig.add_hline(y=0, line=dict(color="grey", width=1))
fig.update_layout(xaxis_title=V, yaxis_title=SHAP)
fig.show()Needs pandas, numpy, plotly · reads shap-dependence-demo.csv · app theme from the optional codesplash helper
# SHAP dependence — Codesplash Chart Lab (recipe "shap-dependence")
# controls: defaults
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("shap-dependence-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
tok <- if (has_cs) codesplashr::cs_palette(dark = FALSE) else list()
seq_ramp <- if (is.null(tok$sequential)) c("#eff6fd", "#1a469d") else tok$sequential # low → high
div_ramp <- if (is.null(tok$diverging)) c("#c13e2e", "#f3f5f8", "#0859ac") else tok$diverging # −, 0, +
ref_col <- if (is.null(tok$reference)) "#4e5661" else tok$reference
grey_col <- if (is.null(tok$dim)) "#ced1d6" else tok$dim
v_col <- "age (years)"
shap_col <- "SHAP value (log-odds)"
col_col <- "BMI"
d <- df[!is.na(df[[v_col]]) & !is.na(df[[shap_col]]), ]
d <- data.frame(x = d[[v_col]], s = d[[shap_col]], k = if (is.null(col_col)) NA_real_ else d[[col_col]])
p <- ggplot(d, aes(x, s, colour = k)) + geom_hline(yintercept = 0, colour = "grey60") + geom_point(alpha = 0.8, size = 1.4) +
scale_colour_gradient(low = div_ramp[length(div_ramp)], high = div_ramp[1], na.value = cs_cat[1], guide = if (is.null(col_col)) "none" else "colourbar") +
labs(x = v_col, y = shap_col, colour = col_col) + theme_cs
print(p)Needs ggplot2 · reads shap-dependence-demo.csv · app theme from the optional codesplashr helper