Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Comparing two groups: contrast sunburst#
Mobile and desktop visitors walk through the same shop differently. The contrast sunburst colors every prefix by the log-ratio of its relative frequency in the two groups (blue: more frequent on desktop, vermilion: on mobile). The contrast table of the frequent sequences adds Fisher’s exact test (Dong & Li, 1999, emerging patterns).
import matplotlib.pyplot as plt
import spmviz as sv
from spmviz.viz import plot_sunburst
db = sv.datasets.make_customer_journeys(1500, random_state=2)
plot_sunburst(db, max_depth=4, color_by="contrast", groups="device", positive="desktop",
title="desktop (blue) versus mobile (vermilion)")
ps = sv.mine(db, 0.03, max_gap=1, max_length=5)
table = sv.measures.contrast(ps, "device", positive="desktop")
print(table.head(8)[["pattern", "supp[desktop]", "supp[mobile]", "log2_ratio", "p_value"]])

pattern supp[desktop] ... log2_ratio p_value
0 cart 0.393124 ... 0.650178 3.822731e-09
1 product → cart 0.313901 ... 0.721760 3.605202e-08
2 checkout 0.263079 ... 0.759139 3.089071e-07
3 cart → checkout 0.263079 ... 0.759139 3.089071e-07
4 product → cart → checkout 0.207773 ... 0.844087 1.471497e-06
5 payment 0.200299 ... 0.732833 3.055575e-05
6 checkout → payment 0.200299 ... 0.732833 3.055575e-05
7 cart → checkout → payment 0.200299 ... 0.732833 3.055575e-05
[8 rows x 5 columns]
Conversion: converted versus lost visitors#
plot_sunburst(db, max_depth=3, color_by="contrast", groups=db.attributes["converted"].map({True: "converted", False: "lost"}),
positive="converted", title="converted (blue) versus lost (vermilion) visitors")
plt.show()

Total running time of the script: (0 minutes 0.690 seconds)