Mining and Visualizing Frequent Sequences in PythonThe spmviz toolbox

Chapter 7
Matrices, maps and sets

A pattern set has more structure than its extension tree: patterns are supported by overlapping sets of cases, they can be split into rules, they are ordered by containment, and they can be scored by interestingness measures. This chapter draws these structures as matrices (incidence, similarity, grouped rules), as points in the plane of two measures, as a terrain of pattern families, as a lattice, and as intersections of supporting sets. The pattern landscape is new.

7.1 Problem and idea

The network-based maps of chapter 4 compress the similarities of the patterns into two coordinates, and the compression costs accuracy. The matrices behind them can be drawn directly: the pattern–sequence incidence matrix, a pattern–pattern similarity matrix, or a matrix of rules. Matrix views do not overlap and scale to dense relations [73], but their message depends entirely on the order of the rows and columns, the problem of seriation [72]. Other questions call for other pictures. Which patterns are interesting rather than merely frequent is a question about measures, best answered by a scatter plot [64]; which patterns contain which is a question about an order, answered by a Hasse diagram; which patterns occur in the same cases is a question about sets, answered by an UpSet plot [67].

7.2 Theory

7.2.1 Incidence and similarity matrices

The incidence matrix 𝐓 ∈{0,1}P×N of section 4.2.3 has a row per pattern and a column per sequence. Its rows are ordered by the leaf order of a hierarchical clustering of the patterns (average linkage on 1 −𝐒 with the Jaccard similarity 𝐒, with optimal leaf ordering), its columns by the first pattern that each sequence supports. Blocks of the seriated matrix are groups of patterns supported by the same sequences; a row that spans several blocks is a pattern shared by several groups. The square similarity matrix 𝐒, seriated in the same way and shown with its dendrogram, displays the same groups without the sequences. Any similarity of the book can be used: transaction (Jaccard of supports), confidence (eq. (4.1)), content (longest common subsequence) or a fused one.

7.2.2 Sequential rules and the grouped matrix

Splitting a pattern φ = ⟨φ1⋯φm⟩ between two of its elements gives a sequential rule α ⇒β with antecedent α = ⟨φ1⋯φq⟩and consequent β = ⟨φq+1⋯φm⟩. Its confidence and lift are

c(α ⇒β) = σ(φ) σ(α),lift(α ⇒β) = c(α ⇒β)N σ(β) : (7.1)

the share of the cases containing α in which β follows, and the ratio of this share to the frequency of β alone. Rules of this kind, read from frequent sequences, have been used for sequence prediction [37] and for alarm suppression [2]. The grouped matrix of Hahsler and Karpienko puts antecedents in the columns and consequents in the rows of a balloon plot, balloon area proportional to the support and color to a measure [65, 66]; spmviz orders both axes by the total support of their rules, so that the strongest rules gather in the upper left corner.

7.2.3 Measure scatter plots

Every pattern is a point in the plane of two measures — relative support and confidence, confidence and lift, support and length — and a thin line joins each pattern to its parent, so that the extension tree is still visible [64]. Frequent but unsurprising patterns sit at the right, rare but confident ones at the top left; lift separates real dependencies from coincidences of frequent events. Color can show the start event, the length or a group contrast.

7.2.4 Pattern landscapes

Maps of several hundred patterns are crowded, and the eye looks for regions rather than points. The pattern landscape turns a map into a terrain. Given coordinates 𝐲i of the patterns, from the transaction map for instance, it draws the support-weighted Gaussian kernel density

f(𝐲) = ∑ iσ(φi)Kh(𝐲 −𝐲i) ∑ iσ(φi) , (7.2)

as contour levels, with the bandwidth h chosen by Scott’s rule or by the analyst. Families of frequent patterns that occur in the same cases become mountains, isolated combinations remain in the plains, and every peak is named after the most supported long pattern near it. The landscape inherits the distortions of the map below it but makes its clusters and their weight visible at once.

7.2.5 Lattices and set views

Containment orders the frequent sequences. Their Hasse diagram has an edge φ →ψ whenever ψ has one item more than φ and contains it, wherever the item is inserted; drawn with one row per length and the closed patterns ringed, it shows how the pattern set is built from short patterns and where the support stops changing. The lattice is denser than the extension tree, in which every pattern has a single parent, so it is drawn for a few levels and the most supported patterns of each.

The supporting sets Ti of the patterns can also be compared as sets. The UpSet plot [67] lists the intersections of the supporting sets of a few patterns as rows of a dot matrix, with a bar for the number of sequences in each exact intersection and a bar for the support of each pattern. It answers a question that no map answers exactly: in how many cases do these patterns occur together, and in how many alone?

In a nutshell: Matrices, maps and sets

  • Seriated incidence and similarity matrices show groups of patterns supported by the same cases.
  • The grouped rule matrix draws antecedents against consequents, with support and lift or confidence.
  • Measure scatter plots separate interesting patterns from merely frequent ones.
  • Pattern landscapes turn a map into a terrain of pattern families.
  • The lattice shows the containment order, the UpSet plot the overlaps of the supporting sets.

7.3 Using it in spmviz

plot_incidence(ps) and plot_similarity_matrix(ps, similarity) draw the seriated matrices, seriate(S) returns the order; ps.incidence() is the sparse matrix 𝐓 and networks.similarity_matrix(ps, name) any of the similarities. ps.rules(min_confidence) (or sequential_rules) returns the rules as a data frame with confidence and lift, and plot_rules_matrix(rules) draws the grouped matrix. measures.pattern_measures(ps) computes support, confidence, cumulative confidence, lift, leverage, number of children and, optionally, entropy and timing for every pattern, and plot_pattern_scatter(ps, x, y) draws any two of them. plot_landscape(ps, similarity) draws the landscape (embedding.landscape returns the density grid); plot_lattice and plot_upset draw the set views, and networks.pattern_lattice returns the Hasse diagram as a graph.

7.4 Examples

Matrices of the alarm floods. Figure 7.1 is the seriated incidence matrix of the 40 most supported frequent sequences of the synthetic alarm floods of section 4.5: the four cascades appear as four blocks of rows supported by largely disjoint sets of floods; the floods with two cascades link the blocks. The seriated transaction similarity (fig. 7.2) shows the same blocks; the content similarity (fig. 7.3) groups patterns by their events rather than by their cases.

plot_incidence(ps, top=40) 
plot_similarity_matrix(ps, "transaction", top=40)
   

Figure 7.1

Figure 7.1. Seriated pattern–sequence incidence matrix of the synthetic alarm floods: four blocks, four cascades.

Open in ColabExample plot_01_incidence_similarity.py in the gallery

Figure 7.2

Figure 7.2. The seriated transaction similarity of the 40 patterns with its dendrogram.

Figure 7.3

Figure 7.3. The seriated content similarity (longest common subsequence) of the same patterns.

Rules. The rules of confidence at least 0.6 of the alarm floods (fig. 7.4) pair every alarm with the operator action that ends its cascade: 𝖯-𝖫𝖮𝖶𝖥𝖫𝖮𝖶 ⇒𝖮𝖯-𝖯𝖴𝖬𝖯𝖲𝖳𝖠𝖱𝖳 has confidence 1 and lift 2.99, 𝖪-𝖲𝖴𝖱𝖦𝖤 ⇒𝖮𝖯-𝖫𝖮𝖠𝖣𝖣𝖮𝖶𝖭 confidence 1 and lift 3.0. Figure 7.5 colors the same balloons by confidence.

rules = ps.rules(min_confidence=0.6) 
print(rules.head(8)[["rule", "support", "confidence", "lift"]]) 
plot_rules_matrix(rules, top_antecedents=14, top_consequents=10)
   

Figure 7.4

Figure 7.4. Grouped matrix of the sequential rules of the alarm floods: antecedents in columns, consequents in rows, balloon area = support, color = lift.

Open in ColabExample plot_02_rules_matrix.py in the gallery

Figure 7.5

Figure 7.5. The same rules colored by confidence.

Interesting patterns of the click-stream. Figure 7.6 places the 241 frequent sequences of the click-stream in the plane of relative support and confidence; fig. 7.7 in the plane of confidence and lift. The patterns with the highest lift are repetitions of Your situation and the pair ⟨𝖳𝗁𝖾 𝗅𝖺𝖻𝗈𝗋 𝗆𝖺𝗋𝗄𝖾𝗍𝖮𝗍𝗁𝖾𝗋 𝗂𝗇𝖿𝗈𝗋𝗆𝖺𝗍𝗂𝗈𝗇⟩: rare services that, once visited, are visited again or followed by a specific next one.

plot_pattern_scatter(ps, "rel_support", "confidence", labels=10) 
plot_pattern_scatter(ps.filter(min_length=2), "confidence", "lift", color_by="start", labels=8)
   

Figure 7.6

Figure 7.6. The click-stream patterns by relative support and confidence; grey lines join every pattern to its parent.

Open in ColabExample plot_03_measure_scatter.py in the gallery

Figure 7.7

Figure 7.7. The click-stream patterns of length at least two by confidence and lift, colored by start event.

A landscape. The transaction map of the alarm floods becomes the landscape of fig. 7.8: four mountains named after their longest patterns, one per cascade. Figure 7.9 varies the bandwidth.

plot_landscape(ps, similarity="transaction", peaks=5)
   

Figure 7.8

Figure 7.8. The pattern landscape of the synthetic alarm floods: support-weighted density of the transaction map, peaks named after their most supported long patterns.

Open in ColabExample plot_04_landscape.py in the gallery

Figure 7.9

Figure 7.9. Landscapes with bandwidths 0.15, 0.3 and 0.6.

Lattice and UpSet. Figure 7.10 is the containment lattice of the daily-routine patterns up to length four, nine patterns per level, closed patterns ringed. The UpSet plot of the seven most supported patterns of length at least two (fig. 7.11) shows how often they occur together in the same day.

plot_lattice(ps, max_length=4, top_per_level=9) 
plot_upset(ps.filter(min_length=2), top=7)
   

Figure 7.10

Figure 7.10. The containment lattice of the daily-routine patterns: one row per length, edges to one-item super-patterns, closed patterns ringed.

Open in ColabExample plot_05_lattice_upset.py in the gallery

Figure 7.11

Figure 7.11. UpSet plot of the supporting days of the seven most supported patterns of length at least two.