svgstacking-weights
Ensemble weights
How much does each member contribute to the ensemble?
How to read it
- Read each bar as the member's share of the ensemble.
- “Not used” members add nothing beyond the others.
- Don't read a weight as a member's quality: correlated members share weight.
- Callout 1: The largest share.
- Callout 2: Not used: nothing to add beyond the others.
Watch out
- Weights shift with small data changes when members are similar.
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.
- Weights sum to one. Stacking weights are non-negative least-squares coefficients on out-of-fold predictions, normalized (SuperLearner's NNLS).
# Ensemble weights — Codesplash Chart Lab (recipe "stacking-weights")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
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("stacking-weights-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
L = "member"
W = "weight"
d = df[[L, W]].dropna().sort_values([W, L], ascending=[True, False]) # largest on top
fig, ax = plt.subplots(figsize=(7, 0.5 * len(d) + 1.2))
ax.barh(d[L].astype(str), d[W], color=PALETTE[0])
for y, w in enumerate(d[W]):
ax.text(w, y, f" {w:.1%}" if w > 0 else " not used", va="center", fontsize=8)
ax.xaxis.set_major_formatter(lambda v, _: f"{v:.0%}")
ax.set_xlabel("Share of the ensemble")
plt.show()Needs pandas, numpy, matplotlib · reads stacking-weights-demo.csv · app theme from the optional codesplash helper
# Ensemble weights — Codesplash Chart Lab (recipe "stacking-weights")
# 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("stacking-weights-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
L = "member"
W = "weight"
d = df[[L, W]].dropna().sort_values([W, L], ascending=[True, False])
fig = go.Figure(go.Bar(x=d[W], y=d[L].astype(str), orientation="h", marker_color=PALETTE[0], text=[f"{w:.1%}" if w > 0 else "not used" for w in d[W]]))
fig.update_layout(xaxis_title="Share of the ensemble", xaxis_tickformat=".0%")
fig.show()Needs pandas, numpy, plotly · reads stacking-weights-demo.csv · app theme from the optional codesplash helper
# Ensemble weights — Codesplash Chart Lab (recipe "stacking-weights")
# 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("stacking-weights-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["member"]] <- factor(df[["member"]])
L <- "member"
W <- "weight"
d <- data.frame(label = as.character(df[[L]]), weight = df[[W]])
d <- d[is.finite(d$weight), ]
d$label <- factor(d$label, levels = d$label[order(d$weight)])
p <- ggplot(d, aes(weight, label)) +
geom_col(fill = cs_cat[[1]]) +
geom_text(aes(label = ifelse(weight > 0, sprintf("%.1f%%", 100 * weight), "not used")), hjust = -0.1, size = 2.8) +
scale_x_continuous(labels = function(v) paste0(round(100 * v), "%"), expand = expansion(mult = c(0, 0.15))) +
labs(x = "Share of the ensemble", y = NULL) + theme_cs
print(p)Needs ggplot2 · reads stacking-weights-demo.csv · app theme from the optional codesplashr helper