Mining and Visualizing Frequent Sequences in PythonThe spmviz toolbox

Chapter 10
Benchmarks and case studies

This chapter puts the methods to work. It first measures the built-in miners on the public benchmark databases of the sequential pattern mining literature, then follows two analyses from the raw log to an interpretation: the click-stream of an online benefit application and the alarm log of a chemical plant, the two logs of the NBVFS paper. It closes with a compact procedure for the analysis of a new log.

10.1 Problem and idea

A toolbox is useful if it is fast enough for real databases and if its views lead to conclusions that can be checked. The first question is answered by benchmarks: the databases distributed with SPMF [28] are the common yardstick of the field, and running the five miners of the package on them, together with an independent implementation, tests correctness and speed at once. The second question is answered by case studies in which several views of the same data are combined, each answering the question it answers best (table 3.1), and in which the conclusions are confronted with the data.

10.2 Benchmark databases

datasets.fetch_spmf(name) downloads and caches nine databases distributed with SPMF (table 10.1). They cover the main application areas of sequential pattern mining: click streams with short sequences over medium alphabets (BMS1, BMS2, KOSARAK, MSNBC) or long sequences (FIFA), text with very large alphabets (BIBLE, LEVIATHAN), sign language (SIGN) and spatial trips (BIKE). The package first tries the SPMF web site and then public mirrors of the same files; for this book the files were obtained from the mirrors. Utility annotations of mirror files are dropped. The statistics of table 10.1 were computed from the files as read; they agree with those published by SPMF except for BMS1 (average length 2.5 against 2.4), BIKE (69 items against 67) and FIFA (average length 36.2 against 34.7), whose mirror is the sequential-utility version of the database. Pattern counts on these three databases are therefore not directly comparable with counts published for the SPMF files.

Table 10.1. Benchmark databases available through fetch_spmf: number of sequences N, number of items |I| and average length, computed from the files used in this book (— for the two databases that were not benchmarked).
Name N |I| length

Content

BIBLE 36,369 13,905 21.6

the Bible, one sentence per sequence, words as items

BMS1 59,601 497 2.5

BMS-WebView-1 (Gazelle) click streams of the KDD Cup 2000 [81]

BMS2 — — —

BMS-WebView-2 (Gazelle) click streams of the KDD Cup 2000 [81]

FIFA 20,450 2,990 36.2

click streams of the FIFA World Cup 1998 web site

KOSARAK10K 10,000 10,094 8.1

the first 10,000 click streams of a Hungarian news portal (FIMI repository)

LEVIATHAN 5,834 9,025 33.8

T. Hobbes, Leviathan (1651), one sentence per sequence

MSNBC — — —

page-category click streams of msnbc.com [82], short sequences removed

SIGN 730 267 52.0

American Sign Language utterances [83]

BIKE 21,078 69 7.3

stations of shared-bike trips

10.3 Run times

Table 10.2 reports the run times of the four exact miners that need no candidate generation and of the prefixspan package [25] as an external reference, at thresholds that give a few hundred to a few thousand patterns; table 10.3 repeats the measurement with the gap constraint gmax = 1 and adds GSP. The measurements were taken with tools/benchmark.py under Python 3.13 on one core of an Intel Xeon processor at 2.1 GHz; they are single runs, so differences below about ten percent are not meaningful.

Table 10.2. Run times (seconds) of the built-in miners and of the prefixspan package on the benchmark databases, maximum length 6, no gap constraint. All algorithms return identical pattern sets; the fastest time of each row is in bold; — marks databases with itemsets or more than 300,000 elements, on which the pure-Python prefixspan package was not run.
Database σmin patterns PrefixSpan CM-SPAM SPAM SPADE prefixspan
SIGN 40% 518 0.39 0.20 0.10 0.79 5.99
LEVIATHAN 10% 651 2.57 0.80 0.58 2.05 49.14
BIKE 1% 901 1.09 0.73 0.63 2.52 —
BMS1 0.2% 798 1.70 0.94 0.95 4.16 175
KOSARAK10K 1% 392 0.57 0.23 0.18 0.50 23.15
FIFA 15% 5,110 85.22 15.33 15.68 141 —
BIBLE 10% 174 5.08 1.41 0.78 5.62 —

Table 10.3. Run times (seconds) with the gap constraint gmax = 1 (contiguous patterns) at half the threshold of table 10.2; the three algorithms return the same number of patterns. GSP was not run on BIBLE, BMS1 and KOSARAK10K.
Database σmin patterns PrefixSpan CM-SPAM GSP
SIGN 20% 98 0.07 0.06 22.36
LEVIATHAN 5% 106 0.28 0.25 13.70
BIKE 0.5% 365 0.38 0.34 10.55
BMS1 0.1% 672 0.38 0.62 —
KOSARAK10K 0.5% 228 0.13 0.14 —
FIFA 7.5% 151 1.33 0.99 310
BIBLE 5% 65 1.27 0.78 —

Three observations hold across the databases. First, all algorithms return identical pattern sets, the external package included. Second, without constraints the bitmap algorithms are the fastest: SPAM in five of the seven cases and CM-SPAM in the other two, between about two and nine times faster than PrefixSpan and SPADE. A Python integer packs the bitmaps of all sequences, so one shift and one intersection process the whole database; PrefixSpan and SPADE loop over sequences and occurrences in the interpreter. The prefixspan package, also pure Python, is 60 to 185 times slower than SPAM on the four databases on which it ran. Third, with a gap constraint the search space is much smaller: PrefixSpan and CM-SPAM finish in at most 1.3 seconds, and the level-wise GSP is 30 to 370 times slower. Pure-Python miners cannot compete with the compiled implementations of SPMF on very large databases, but at the thresholds that produce readable pattern sets — a few hundred patterns — they run in seconds, which is what interactive visual analysis needs.

Figure 10.1

Figure 10.1. The SIGN benchmark: the CM map of its frequent sequences of length at least two (left) and the sunburst of the first three signs of its 730 utterances (right).

Open in ColabExample plot_01_benchmarks.py in the gallery

try: 
    db = sv.datasets.fetch_spmf("SIGN") 
except OSError as exc:  # no network: fall back to a synthetic database of the same size 
    print("download failed:", exc) 
    db = sv.datasets.make_quest(730, n_items=267, avg_elements=52, avg_itemset=1.0, random_state=0) 
print(db.stats()) 
ps = sv.mine(db, min_support=0.5, max_length=5) 
print(ps)
   

10.4 Case study: an online benefit application

The click-stream of the NBVFS paper records 343 sessions of applicants for unemployment benefit on the portal of the Dutch Employee Insurance Agency, part of the BPI Challenge 2016 data [29]. Every click opens one of 14 services of the application, among them Your personal details, Your last employer, The dismissal, Hours worked, Your situation, Your income and The labor market. Where do the applicants start, which paths are typical, where do they hesitate, and which next step can be predicted?

The Sankey diagram of the first five steps (fig. 10.2) shows that half of the sessions start with Your personal details (90 sessions) or Your last employer (84), and that the paths diverge quickly. The directly-follows graph (fig. 10.3) shows the services that applicants revisit, and the continuation-entropy tree (fig. 10.4) where the next service is hard to predict. The 241 frequent sequences mined as in the paper (section 4.4) were drawn as WN, CM and TM views in chapter 4; their measure scatter plot (fig. 7.7) singled out the repeated visits of Your situation. Finally, the prediction view (fig. 10.5) answers the operational question: after ⟨𝖸𝗈𝗎𝗋 𝗅𝖺𝗌𝗍 𝖾𝗆𝗉𝗅𝗈𝗒𝖾𝗋𝖳𝗁𝖾 𝖽𝗂𝗌𝗆𝗂𝗌𝗌𝖺𝗅⟩, the longest frequent sequence that matches the session so far, the most likely next services are The dismissal again (confidence 0.45, 26 sessions) and Hours worked (0.34, 20 sessions). Repeated visits of the same service are frequent in this log: 55% of the sessions open the same service twice in a row. Whether this reflects multi-page forms or applicants going back to correct their entries cannot be told from the log alone, but it is the first question to ask the owners of the application.

obs = [0, 1]  # Your last employer, The dismissal 
print(sv.predict_next(ps, obs)) 
plot_prediction(ps, obs)
   

Figure 10.2

Figure 10.2. The first five steps of the 343 sessions as a Sankey diagram.

Open in ColabExample plot_02_clickstream_story.py in the gallery

Figure 10.3

Figure 10.3. The directly-follows graph of the 14 services.

Figure 10.4

Figure 10.4. The continuation-entropy tree of the first four steps.

Figure 10.5

Figure 10.5. What comes next after ⟨𝖸𝗈𝗎𝗋 𝗅𝖺𝗌𝗍 𝖾𝗆𝗉𝗅𝗈𝗒𝖾𝗋𝖳𝗁𝖾 𝖽𝗂𝗌𝗆𝗂𝗌𝗌𝖺𝗅⟩: the extensions of the longest matching frequent sequence, with their confidences and supports.

10.5 Case study: early warning in alarm management

The alarm log of the alkylation plant (section 4.4) has 3,956 traces of alarms and operator actions. The research of the group used frequent sequences of such logs to suppress alarms that follow others predictably [2], to predict the next alarm by sequence compression and alignment [3], and to design operator interfaces [4]. Two of the views of this book support an operator directly.

The scenario tree of the most frequent start event, O1084 (fig. 10.6), is the pattern tree of the frequent sequences that start with it, reduced to the three most frequent continuations of every node; it lists the typical scenarios after this action, with their supports. The prediction view (fig. 10.7) works online: given the events seen so far, it finds the longest frequent sequence that matches their end and ranks its extensions by confidence. After ⟨𝖮1084𝖮1085⟩, the start of the most supported frequent sequence of length at least three, the action O1084 follows in 52% of the cases, O1085 in 25%, O1133 in 24%. The confidences do not add up to one: with a maximum gap of two, a trace may continue the sequence in several ways. A first check of the predictions on the 421 traces that contain the observed pair gives a top-1 accuracy of 0.38. The check is in-sample, because the patterns were mined from the same traces, so it is optimistic; an honest evaluation would mine on one part of the log and predict on another, as in the alignment-based study of the alarm log [3].

long = ps.filter(min_length=3).sort("support").patterns[0] 
observed = [e[0] for e in long[:2]] 
print("observed:", [db.label(i) for i in observed]) 
print(sv.predict_next(ps, observed)) 
plot_prediction(ps, observed)
   

Figure 10.6

Figure 10.6. Frequent scenarios starting with the operator action O1084: the three most frequent continuations of every node, edge width proportional to the support.

Open in ColabExample plot_03_alarm_early_warning.py in the gallery

Figure 10.7

Figure 10.7. Online prediction after ⟨𝖮1084𝖮1085⟩: the extensions of the longest matching frequent sequence ranked by confidence.

10.6 Analysing a new log

The examples of the book suggest a procedure for a new log (fig. 10.8). Its steps can be repeated in any order, and every step has a view that checks the previous one.

Figure 10.8

Figure 10.8. A procedure for the analysis of a new log. The dashed arrow is the most frequent iteration: a pattern seen in the data that does not match its picture usually means that a constraint was too loose or too tight.
1.
Look at the data before mining. The statistics of the database, a sunburst of the beginnings and an index plot of a sample show the lengths, the alphabet and the obvious regularities.
2.
Choose the constraints. The distributions of the gaps and spans between events (plot_gap_distribution) tell which distances are meaningful; most logs need a gap constraint, and logs with timestamps often a time window.
3.
Choose the threshold. The explosion curve (measures.pattern_counts) shows how the number of patterns changes with the threshold; a few hundred patterns are readable, a few thousand need condensation. Top-k mining avoids the choice.
4.
Condense. Closed patterns lose nothing, maximal patterns lose the supports, an MDL summary keeps only the patterns that explain the data.
5.
Find the structure. The WN shows how the sequences grow, the CM map which belong to the same scenario, the TM map and the communities which occur together.
6.
Answer the questions with the views listed in table 3.1.
7.
Check in the data. Every pattern that leads to a conclusion should be found in the sequences, with an index plot or a dotted chart.
8.
Export and report. Graphs go to Gephi or Cytoscape, tables to spreadsheets, interactive figures to HTML pages.

In a nutshell: Benchmarks and case studies

  • On the SPMF benchmarks the five miners and the prefixspan package return identical pattern sets; SPAM and CM-SPAM are the fastest in pure Python.
  • At thresholds that produce readable pattern sets, all databases are mined in seconds.
  • In the click-stream, the views locate the typical starts, the revisited services and the hard-to-predict steps; in the alarm log, scenario trees and online prediction support the operators.
  • Conclusions drawn from a picture should be checked in the sequences themselves.