Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Five algorithms, one answer#
The package implements PrefixSpan (pattern growth), SPAM and CM-SPAM (vertical
bitmaps, the algorithm used by Bántay & Abonyi, 2024), SPADE (vertical id-lists)
and GSP (level-wise). They differ in speed, never in the result: the same
patterns with the same supports and supporting sequences. External miners
(the prefixspan package, seq2pat, gsppy, SPMF) return the same
PatternSet and can be compared in the same way.
import time
import matplotlib.pyplot as plt
import numpy as np
import spmviz as sv
db = sv.datasets.make_customer_journeys(1000, random_state=1)
Run all built-in miners#
results, times = {}, {}
for alg in ["prefixspan", "cmspam", "spam", "spade", "gsp"]:
t0 = time.perf_counter()
results[alg] = sv.mine(db, min_support=0.03, algorithm=alg, max_length=6)
times[alg] = time.perf_counter() - t0
ref = results["prefixspan"]
for alg, ps in results.items():
same = ps.patterns == ref.patterns and np.array_equal(ps.support, ref.support)
print(f"{alg:10s} {len(ps):5d} patterns {times[alg]:6.3f} s identical={same}")
prefixspan 890 patterns 0.088 s identical=True
cmspam 890 patterns 0.051 s identical=True
spam 890 patterns 0.042 s identical=True
spade 890 patterns 0.107 s identical=True
gsp 890 patterns 0.770 s identical=True
An external back end#
The prefixspan package (PyPI) is used when installed.
try:
ext = sv.mine(db, 0.03, algorithm="prefixspan-py", max_length=6)
print("prefixspan-py:", len(ext), "patterns, identical:", dict(zip(ext.patterns, ext.support)) == dict(zip(ref.patterns, ref.support)))
except ImportError:
print("the prefixspan package is not installed")
the prefixspan package is not installed
Run times#
fig, ax = plt.subplots(figsize=(6, 3))
ax.barh(list(times), list(times.values()), color="#285780")
ax.set_xlabel("seconds")
ax.set_title("same patterns, different algorithms", loc="left", fontsize=10)
for s in ("top", "right"):
ax.spines[s].set_visible(False)

Parameter study: top-k instead of a threshold#
When no sensible threshold is known, ask for the k most frequent patterns.
top = sv.topk(db, k=10, max_length=6)
print(top.to_frame()[["pattern", "support"]])
plt.show()
pattern support
0 category 443
1 exit 841
2 home 1000
3 product 617
4 search 473
5 home → category 443
6 home → exit 841
7 home → product 617
8 home → search 473
9 product → exit 458
10 home → product → exit 458
Total running time of the script: (0 minutes 1.180 seconds)