Chapter 5
Trees of prefixes and patterns
Merging the common beginnings of the sequences gives a tree that every analyst of event data knows in some form. This chapter defines the prefix tree and the pattern tree, the anchored trees behind word trees, and the drawings of a tree as a node-link diagram, a sunburst, an icicle and a time-aware LifeFlow. It introduces two new colorings of the tree: by the entropy of the next step, which marks the points where a process branches, and by the contrast between two groups of cases.
5.1 Problem and idea
How do the cases begin? Which second step follows a given first step, and how often does a case end there? A tree answers these questions without any mining: every prefix that occurs in the data is a node, the number of cases passing through it is its weight, and the children of a node are the steps that follow it. The tree is the aggregation behind the earliest visual tools for web sessions [53], behind LifeFlow [54], EventFlow [55] and CoreFlow [56], and, grown from a chosen word, behind the word tree of text visualization [57]. Its weakness is that it only sees prefixes: the same three steps in the middle of two cases fall into different branches. Two remedies exist. The tree can be grown from an anchor, a pattern chosen by the analyst, in both directions (the double tree [58, 59]). Or the tree can be built from the frequent sequences instead of the raw prefixes: the extension tree of section 2.2.2 is a tree of patterns that may start anywhere in a case.
5.2 Theory
5.2.1 Prefix trees and pattern trees
The prefix tree (trie) of a database has a root and a node for every prefix that occurs at the start of at least one sequence; is a child of the node of . Its weight is the number of sequences that start with the prefix, and the number of those that end there. The weights satisfy , the conservation law that makes the tree drawable as a space-filling diagram. A depth limit and a minimum weight keep the tree readable; spmviz also offers to keep only the heaviest children of every node.
The pattern tree of a prefix-closed pattern set is the extension tree with the supports as weights. It differs from the prefix tree in two ways: a pattern may start anywhere in a sequence and skip events, and the weights do not satisfy the conservation law, because a sequence can support several children of the same node. Both trees are drawn by the same functions; which of the two is drawn depends on whether a database or a pattern set is passed.
An anchored tree starts every sequence at the first occurrence of an anchor pattern . The forward tree is the prefix tree of the suffixes after the occurrence, the backward tree the prefix tree of the reversed prefixes before it; together they form the double tree of [58]. Sequences that do not contain are left out.
5.2.2 Drawing a tree
A node-link drawing places the depths on one axis and orders the nodes of each depth on the other, giving every leaf an equal share and every inner node the mean position of its children; edges are drawn as Bézier curves as wide as the weight of the child. It shows the topology best and labels well, but it wastes space on wide trees. Space-filling drawings give every node an area proportional to its weight. The icicle stacks the nodes of each depth as rectangles in a column [52], the sunburst as wedges of a ring, angle proportional to weight [51]; the share of the cases that end at a node is the empty part below it (fig. 5.1). Sunbursts are memorable and compact but make angles hard to compare; icicles compare better and label better.
LifeFlow replaces the depth axis by time: every node is placed at the mean time elapsed from the start of the sequences to the event, so horizontal gaps show how long the steps take [54]. A word tree draws the forward tree of an anchor with font sizes proportional to the weights [57]; the double tree adds the backward tree on the left [58].
5.2.3 The continuation-entropy tree
Where is the next step of a process predictable, and where do the cases scatter? Let be the share of the sequences through node that continue with event (the child ), and the share that end. The continuation entropy of the node is
| (5.1) |
zero when the next step is certain and when continuations are equally likely. Coloring the prefix tree by , from green (predictable) to vermilion (branching), makes the decision points of a process visible at a glance (fig. 5.1b). For monitoring and process improvement these are the places where a warning, a recommendation or a redesign pays off; for prediction they are the places where no model can be sure. The same quantity is defined for a pattern of a pattern set, with the supports of its extensions in place of the child weights (measures.continuation_entropy); comparing the mean entropy of patterns of increasing length tells whether a longer history makes the next step more predictable.
5.2.4 The contrast tree
When the cases fall into groups — mobile and desktop visitors, converted and lost customers, shifts of a plant — the tree can show where the groups differ. Let and be the numbers of sequences of the positive group (size ) and of the rest (size ) that pass through . The contrast tree colors each node by the smoothed log-ratio of the relative frequencies,
| (5.2) |
on a diverging scale: blue nodes are typical of the positive group, vermilion nodes of the rest, white nodes of neither. The pseudocounts keep finite where one group never passes. For frequent sequences, measures.contrast adds the growth rate of Dong and Li’s emerging patterns [76] and the -value of Fisher’s exact test for every pattern, so that the visual impression can be checked statistically.
In a nutshell: Trees
- The prefix tree aggregates the beginnings of the cases; the pattern tree draws the frequent sequences, which may start anywhere.
- Node-link trees show topology, icicles and sunbursts show proportions, LifeFlow shows durations, word and double trees show the context of an anchor.
- The continuation-entropy tree marks where the next step is unpredictable.
- The contrast tree marks where two groups of cases differ; the contrast table adds growth rates and Fisher’s exact test.
5.3 Using it in spmviz
networks.prefix_tree(db, max_depth, min_count, anchor, direction) returns the tree as a NetworkX DiGraph with the attributes item, depth, count, n_end, path and, with timestamps, mean_time; networks.double_tree returns the two halves of a double tree. The drawing functions plot_prefix_tree, plot_sunburst and plot_icicle accept a database (prefix tree) or a pattern set (pattern tree) and share the options max_depth, min_count, max_children, anchor and color_by ("item", "entropy" or "contrast" with groups and positive); plot_icicle(time_aware=True) gives LifeFlow, plot_double_tree(db, anchor) the double tree. interactive.sunburst_figure and icicle_figure return zoomable plotly versions.
5.4 Examples
A spectacular sunburst. load_chess_openings draws two thousand games from a small opening book of 33 main lines; every game is the sequence of its first moves (plies). In the sunburst of fig. 5.2 the familiar families appear as large slices: the open games after 1.e4 e5, the Sicilian Defence after 1.e4 c5, the closed games after 1.d4 d5 and the Indian systems after 1.d4 Nf6. The icicle of fig. 5.3 shows the same tree with labels that can be read, and fig. 5.4 the effect of the depth limit. The contiguous frequent sequences of the games (, 5% support) recover the main lines: is played in 468 games, the Sicilian in 156.
db = sv.datasets.load_chess_openings(2000, random_state=0) plot_sunburst(db, max_depth=5, min_angle=7, title="the first five plies of 2,000 games")
Open in ColabExample plot_01_chess_sunburst.py in the gallery
Where does the shop branch? The thousand synthetic journeys of a web shop start at the home page and continue to search, category and product pages, reviews, the cart, checkout and payment, or leave. In the continuation-entropy tree of fig. 5.5 the product pages are vermilion: after a product page visitors scatter almost evenly between the cart, leaving and reading reviews (after the three shares are 0.29, 0.25 and 0.21). The mean entropy of the frequent sequences hardly falls with their length (1.08 bits for single pages, 0.94 bits for four pages): in this shop the history says little about the next page.
db = sv.datasets.make_customer_journeys(1000, random_state=0) plot_prefix_tree(db, max_depth=4, min_count=25, color_by="entropy", title="continuation entropy of the web-shop journeys")
Word trees. What happens before and after a visitor puts something into the cart? The double tree of fig. 5.6 answers in one picture. The same view works for melodies: fig. 5.7 shows the context of the motif in the phrases of thirteen public-domain songs, and fig. 5.8 the context of the longer pivot .
shop = sv.datasets.make_customer_journeys(1000, random_state=0) plot_double_tree(shop, "cart", max_depth=3, min_count=15)
Contrast. Desktop visitors of the synthetic shop reach the cart, checkout and payment more often than mobile visitors. The contrast sunburst of fig. 5.9 shows where the paths diverge, and the contrast table confirms the impression: is supported by 39% of the desktop sessions, with a log-ratio of 0.65 against the mobile ones and a Fisher -value of . Figure 5.10 contrasts converted and lost visitors.
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"]])
Open in ColabExample plot_04_contrast_tree.py in the gallery
Time. The time-aware icicle of the daily routines (fig. 5.11) places every activity at the mean time since waking up. Split by persona (fig. 5.12), it shows the three mornings that the generator encodes: remote workers start work about half an hour after waking up, commuters reach work only after two hours, one of them on the road, and early birds go for a run before their coffee.
plot_icicle(db, max_depth=5, min_count=15, time_aware=True, title="mean time since waking up (hours)")
Open in ColabExample plot_05_lifeflow_icicle.py in the gallery