Chapter 8
Frequent patterns and association rules
A transaction database and a hypergraph are the same object seen from two sides: every transaction is a set of items, every hyperedge a set of vertices. This chapter uses the correspondence in both directions. Frequent itemsets mined from the hyperedges are themselves the hyperedges of a weighted pattern hypergraph, closed and maximal itemsets compress it, and the inclusion lattice orders it; association rules become hyperarcs of a directed hypergraph, and the weights of association-rule hypergraph partitioning turn the patterns into clusters of mutually predictive items. Because frequent is not the same as significant, the chapter ends with a statistical test that keeps only the groups that occur more often than the activity of their members explains. The examples use the synthetic news data with planted narratives and a hypergraph with planted repeated groups.
8.1 Problem and idea
Market-basket analysis, the mining of co-occurring symptoms, the search for recurring teams, and the analysis of themes in news articles share one question: which combinations of items occur together often, and which items predict others? The classical answer is frequent-itemset mining [110]: count the transactions that contain each candidate set and keep the sets whose relative frequency exceeds a threshold. The result is a list, often thousands of sets long, sorted by support. It is hard to read because the sets overlap and nest: if is frequent, so are , , , and the three singletons.
The idea of this chapter is to treat the patterns as a hypergraph and to apply the tools of the previous chapters to it (fig. 8.1). Every frequent itemset is a hyperedge weighted by its support; the drawings of chapter 4, the UpSet view of section 3.2.6, and the clustering methods of chapter 7 then show and group the patterns. Association rules are naturally directed hyperarcs from the antecedent set to the consequent set. In the other direction, any hypergraph can be mined: its hyperedges are the transactions, and its frequent itemsets are the vertex groups that recur across hyperedges, the higher-order counterpart of a pair of vertices with a large co-occurrence count. Since the most active vertices co-occur often by chance alone, a frequent group must be tested against a null model before it is interpreted.
8.2 Theory
8.2.1 Support and the itemset lattice
Let be a database of transactions over an item set , with optional weights ; for a hypergraph, the transactions are the hyperedges and the weights the hyperedge weights, so that a collapsed multi-hyperedge counts with its multiplicity. The support of an itemset is
|
| (8.1) |
and is frequent if . Support is anti-monotone: implies , so every subset of a frequent itemset is frequent, and every superset of an infrequent itemset is infrequent. This downward closure property makes the search over the subsets feasible.
Apriori. Agrawal and Srikant [110] generate the frequent itemsets level by level: the candidates of size are the unions of two frequent -itemsets that share items, a candidate is pruned if any of its -subsets is infrequent, and the survivors are counted in one pass over the database.
Eclat. Zaki [111] stores for every itemset its tid-list, the set of transactions that contain it, and explores the lattice depth first: the tid-list of is the intersection of the tid-lists of and , and its size is the support. Eclat needs no candidate counting passes and is usually faster than Apriori on dense data; both produce the same itemsets (algorithm 8.1).
Closed and maximal itemsets. The frequent itemsets are redundant. An itemset is closed if no proper superset has the same support, and maximal if no proper superset is frequent [112]. The closed itemsets determine the supports of all frequent itemsets (the support of is the largest support of a closed superset), so they are a lossless compression; the maximal itemsets determine only which sets are frequent. The closed itemsets have a second meaning: they are the intents of the formal concepts of the incidence relation [113]. A formal concept is a pair (extent, intent) of a set of hyperedges and a set of vertices such that the intent is the set of vertices shared by all hyperedges of the extent and the extent is the set of all hyperedges that contain the intent. The concepts, ordered by inclusion of their extents, form the concept lattice, and the frequent closed itemsets are the intents of the concepts with large extents.
The inclusion lattice. The itemsets, ordered by inclusion, form a partially ordered set. Its Hasse diagram has an arc from every set to each of its covers (minimal proper supersets) and is drawn in levels by size. Drawn for the frequent itemsets of one group of items, it shows at a glance how the support decays as items are added and which combinations are missing.
8.2.2 Association rules as hyperarcs
An association rule with disjoint itemsets (antecedent) and (consequent) states that transactions containing tend to contain . Its support is and its strength is measured by several interestingness measures [114]:
with . Confidence is the conditional probability of given ; lift compares it with the unconditional probability, so lift above 1 means a positive association; leverage is the difference between the observed and the expected joint support; conviction is large when the rule rarely fails. Zhang’s measure [115] lies in , is positive for association and negative for dissociation, and is less sensitive than lift to the support of the consequent. A rule is a hyperarc from the tail to the head , so a set of rules is a directed hypergraph (section 2.2.1); its weights can be any of the measures above, and its drawings and walks follow the general machinery of directed hypergraphs, including the reachability analysis of section 10.2.4.
8.2.3 Association-rule hypergraph partitioning
Han, Karypis, Kumar, and Mobasher [116, 117] proposed to cluster items through their association rules. Every frequent itemset of size at least two becomes a hyperedge whose weight is the average confidence of the rules that predict one of its items from the others,
|
| (8.3) |
and the weighted hypergraph is partitioned so that the cut weight is small. Items in the same part predict each other strongly; items in different parts are associated only weakly. The original work used the multilevel partitioner hMETIS; hatvis computes the weights with arhp_weights and clusters the weighted itemset hypergraph with the hypergraph Louvain method of section 7.2.2, which does not need the number of clusters.
8.2.4 Statistically validated hyperedges
A frequent group may simply consist of very active vertices. Musciotto, Battiston, and Mantegna [118] test every observed group against a null model that preserves the activity of each vertex and destroys everything else. The hyperedges of each size are treated separately: among the hyperedges of size , vertex appears times. Under the null hypothesis each vertex chooses its hyperedges uniformly at random among the , independently of the other vertices. For a group the number of hyperedges that contain all its members is then the size of the intersection of independent random subsets of sizes of an -element set. Its distribution is computed iteratively: the intersection of the first two subsets is hypergeometric, and each further subset intersects the current intersection hypergeometrically,
|
| (8.4) |
starting from . A group observed times receives the -value and is validated if the -value is below the significance level after correction for multiple testing. The conservative Bonferroni correction divides the level by the number of groups that could have been tested, for the vertices active at size ; the Benjamini–Hochberg procedure [119] controls the false discovery rate over the observed groups instead and is less strict. Validation separates two situations that frequency confounds: a group of very active vertices that meets a few times by chance, and a group of moderately active vertices that meets more often than their activity explains.
In a nutshell: Patterns as hypergraphs
- Hyperedges are transactions and transactions are hyperedges; support (8.1) is anti-monotone.
- Apriori (level-wise) and Eclat (depth-first, tid-lists) give the same frequent itemsets; closed itemsets compress losslessly and are the intents of formal concepts; maximal itemsets bound the frequent family; the inclusion lattice orders it.
- Rules are hyperarcs; confidence, lift, leverage, conviction, Zhang (8.2).
- ARHP: weight frequent itemsets by mean rule confidence (8.3), cluster the weighted hypergraph.
- Validation: iterated hypergeometric null (8.4) per hyperedge size, Bonferroni or FDR; frequent is not significant.
8.3 Using it in hatvis
frequent_itemsets(data, min_support=0.1, max_size=None, algorithm="eclat", weights=None, min_size=1)
- data
-
a hypergraph (its hyperedges are the transactions) or a list of transactions.
- min_support
-
relative if at most 1, otherwise an absolute (weighted) count.
- Returns
-
a DataFrame with columns itemset (frozenset), size, count, support, sorted by decreasing support.
- Related
-
closed_itemsets, maximal_itemsets, association_rules(itemsets, min_confidence), itemset_hypergraph(itemsets, min_size), patterns.rule_hypergraph(rules), patterns.arhp_weights(itemsets), patterns.inclusion_lattice(sets), patterns.formal_concepts(H), patterns.validated_hyperedges(H, alpha, correction), transactions_to_hypergraph, hypergraph_to_transactions; plots plot_upset, plot_lattice; SPMF input and output in hatvis.io.
8.4 Examples
Frequent theme combinations. With 150 synthetic articles per period (450 in total; the data are described in section 3.4), all theme combinations with a support of at least 5% are mined.
fi = hv.frequent_itemsets(T, min_support=0.05) same = fi.equals(hv.frequent_itemsets(T, min_support=0.05, algorithm="apriori")) closed = hv.closed_itemsets(fi) maximal = hv.maximal_itemsets(fi) IH = hv.itemset_hypergraph(closed[closed["support"] >= 0.08], min_size=2)
There are 88 frequent itemsets, Eclat and Apriori agree, and all 88 are closed: in these data no frequent combination always occurs together with an additional theme, so closure does not compress. The eight maximal itemsets are much more informative. The five largest are exactly the four-theme cores of five planted narratives (climate science with support 0.096, finance and impacts with 0.082 each, policy with 0.080, and energy with 0.073), and the sixth is a three-theme subset of the society narrative (youth, migration, protest, 0.056), whose fourth theme does not reach the threshold together with the other three. The pattern hypergraph of the closed itemsets with support of at least 8% (fig. 8.2) shows each narrative as a nest of overlapping hyperedges, the four-theme itemset enclosing its triples and pairs; the absence of hyperedges between the nests is the visual signature of themes that rarely combine across narratives at this support level.
Open in ColabExample plot_25_itemset_hypergraph.py in the gallery
The inclusion lattice of the frequent itemsets of the climate narrative (fig. 8.3) has all four singletons, all six pairs, all four triples, and the full set, which means that every combination of the four themes is frequent; the vertex sizes show how the support decreases level by level.
Rules and clusters. The rules with confidence at least 0.6 are mined from the itemsets with support of at least 4%, the top thirty become a directed hypergraph, and the ARHP weights are used to cluster the itemset hypergraph.
rules = hv.association_rules(fi, min_confidence=0.6) D = hv.patterns.rule_hypergraph(rules.head(30)) w = hv.patterns.arhp_weights(fi) W = hv.itemset_hypergraph(fi, min_size=2).with_weights(w.reindex(fi[fi["size"] >= 2]["itemset"]).to_numpy()) clusters = hv.hypergraph_louvain(W, random_state=0)
The strongest rule is with confidence 0.925 and lift 5.34: articles that discuss finance, loss and damage, and aid almost always mention developing countries, and they do so 5.3 times more often than articles in general. The following rules predict the fourth theme of a narrative from the other three (drought from wildfire and heatwave, confidence 0.90; summit from agreement, negotiation, and pledge, 0.88; coal from energy, oil and gas, and renewables, 0.87). The thirty strongest rules form a directed hypergraph with 30 hyperarcs on the 24 themes. The scatter plot of fig. 8.4 shows the usual trade-off: the rules with the highest lift have small support, because they involve three-theme antecedents. ARHP clustering of the itemset hypergraph returns six clusters that coincide exactly with the six planted narratives, including the society narrative, which the maximal itemsets had captured only partially: the clustering uses the pairwise and triple associations of the fourth theme even where the full four-theme combination is not frequent.
Open in ColabExample plot_26_association_rules.py in the gallery
Frequent versus significant. The last example plants four groups, , , , and , each repeated six times, among 300 random hyperedges of two to four of forty vertices, and validates every observed vertex set.
table = hv.patterns.validated_hyperedges(H, alpha=0.01, correction="bonferroni")
Exactly the four planted groups are validated (fig. 8.5). The four-vertex group has the smallest -value (; its expected multiplicity under the null is 0.042, and Bonferroni counts possible tests), followed by the two triples and the pair ( against an expected multiplicity of 0.88 and tests). Groups that occur twice by chance have -values around , far above the Bonferroni threshold. The order of significance is not the order of frequency, since all planted groups occur six times: a repeated pair is much less surprising than a repeated quadruple, and the test accounts for this. The vertex belongs to two planted groups and is therefore very active; a frequency-based method would rank many of its chance co-occurrences highly, while the validation discounts them.
Open in ColabExample plot_27_validated_hyperedges.py in the gallery
8.5 Further reading
Agrawal and Srikant [110] and Zaki [111] are the classical references for Apriori and Eclat, Pasquier et al. [112] for closed itemsets, and Ganter and Wille [113] for formal concept analysis. Tan, Kumar, and Srivastava [114] compare the interestingness measures of rules. The association-rule hypergraph of Han et al. [116, 117] is the earliest systematic use of hypergraphs for pattern clustering. Musciotto et al. [118] introduce statistically validated hypergraphs and apply them to social and biological data. The SPMF library [43] implements more than two hundred pattern-mining algorithms whose output hatvis reads directly.