svgrules-scatter
Association rules
Which item combinations co-occur often and reliably, beyond chance?
How to read it
- Read x as how common the rule is and y as how often it holds.
- Read colour as lift: how much more often than chance.
- Prefer rules that are high on all three.
- Callout 1: High confidence, but rare.
- Callout 2: Common but less reliable.
- Callout 3: Colour scale: lift.
Watch out
- Confidence can be high only because the consequent is common: use lift. Many rules means some look strong by chance.
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.
- Support = share of transactions containing all items; confidence = P(consequent | antecedent); lift = confidence ÷ P(consequent) (> 1 means the items co-occur more than by chance).
- High confidence with lift near 1 only reflects a popular consequent.
# Association rules — Codesplash Chart Lab (recipe "rules-scatter")
# 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("rules-scatter-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")
SUP = "support"
CONF = "confidence"
LIFT = "lift"
LAB = "rule"
d = df.dropna(subset=[SUP, CONF, LIFT])
cmap = LinearSegmentedColormap.from_list("seq", [SEQ[0], SEQ[-1]])
fig, ax = plt.subplots(figsize=(8, 5))
sc = ax.scatter(d[SUP], d[CONF], c=d[LIFT], cmap=cmap, s=30, edgecolors=GREY, linewidths=0.4)
for _, row in d.nlargest(5, LIFT).iterrows(): # the highest-lift rules
ax.annotate(str(row[LAB]), (row[SUP], row[CONF]), xytext=(4, 4), textcoords="offset points", fontsize=7)
fig.colorbar(sc, ax=ax, label=LIFT)
ax.set_xlabel(SUP)
ax.set_ylabel(CONF)
plt.show()Needs pandas, numpy, matplotlib · reads rules-scatter-demo.csv · app theme from the optional codesplash helper
# Association rules — Codesplash Chart Lab (recipe "rules-scatter")
# 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("rules-scatter-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")
SUP = "support"
CONF = "confidence"
LIFT = "lift"
LAB = "rule"
d = df.dropna(subset=[SUP, CONF, LIFT])
top = set(d.nlargest(5, LIFT).index)
fig = go.Figure(go.Scatter(x=d[SUP], y=d[CONF], mode="markers+text", text=[str(v) if i in top else "" for i, v in zip(d.index, d[LAB])],
textposition="top right", textfont=dict(size=9), hovertext=d[LAB].astype(str),
marker=dict(size=9, color=d[LIFT], colorscale=[[0, SEQ[0]], [1, SEQ[-1]]], colorbar=dict(title=LIFT), line=dict(color=GREY, width=0.5))))
fig.update_layout(xaxis_title=SUP, yaxis_title=CONF)
fig.show()Needs pandas, numpy, plotly · reads rules-scatter-demo.csv · app theme from the optional codesplash helper
# Association rules — Codesplash Chart Lab (recipe "rules-scatter")
# 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("rules-scatter-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["rule"]] <- factor(df[["rule"]])
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
sup_col <- "support"
conf_col <- "confidence"
lift_col <- "lift"
lab_col <- "rule"
d <- df[!is.na(df[[sup_col]]) & !is.na(df[[conf_col]]) & !is.na(df[[lift_col]]), ]
d <- data.frame(s = d[[sup_col]], cf = d[[conf_col]], lift = d[[lift_col]], lab = as.character(d[[lab_col]]))
top <- head(d[order(-d$lift), ], 5) # the highest-lift rules
p <- ggplot(d, aes(s, cf, colour = lift)) + geom_point(size = 2) +
geom_text(data = top, aes(label = lab), colour = ref_col, size = 2.6, hjust = -0.05, vjust = -0.6) +
scale_colour_gradient(low = seq_ramp[1], high = seq_ramp[length(seq_ramp)]) + labs(x = sup_col, y = conf_col, colour = lift_col) + theme_cs
print(p)Needs ggplot2 · reads rules-scatter-demo.csv · app theme from the optional codesplashr helper