DATA MINING - 1DL36 Fall 212" An introductory class in data mining http://www.it.uu.se/edu/course/homepage/infoutv/ht12 Kjell Orsborn Uppsala Database Laboratory Department of Information Technology, Uppsala University, Uppsala, Sweden 1/12/12 1
Data Mining Association Analysis: Basic Concepts and Algorithms (Tan, Steinbach, Kumar ch. 6)" Kjell Orsborn Department of Information Technology Uppsala University, Uppsala, Sweden 1/12/12 2
Market basket analysis - Association rule mining" Given a set of transactions, find rules that will predict the occurrence of an item based on the occurrences of other items in the transaction Market-Basket transactions TID Items 1 Bread, Milk 2 Bread, Diaper, Beer, Eggs 3 Milk, Diaper, Beer, Coke 4 Bread, Milk, Diaper, Beer 5 Bread, Milk, Diaper, Coke Example of Association Rules {Diaper} {Beer}, {Milk, Bread} {Eggs,Coke}, {Beer, Bread} {Milk}, Implication means co-occurrence, not causality! 1/12/12 3
Definition: frequent itemset" Itemset A collection of one or more items Example: {Milk, Bread, Diaper} k-itemset An itemset that contains k items Support count (σ) Frequency of occurrence of an itemset E.g. σ({milk, Bread,Diaper}) = 2 Support Fraction of transactions that contain an itemset E.g. s({milk, Bread, Diaper}) = 2/5 Frequent Itemset An itemset whose support is greater than or equal to a minsup threshold TID Items 1 Bread, Milk 2 Bread, Diaper, Beer, Eggs 3 Milk, Diaper, Beer, Coke 4 Bread, Milk, Diaper, Beer 5 Bread, Milk, Diaper, Coke 1/12/12 4
Definition: association rule" Association Rule An implication expression of the form X Y, where X and Y are itemsets Example: {Milk, Diaper} {Beer} Rule Evaluation Metrics Support (s) Fraction of transactions that contain both X and Y Confidence (c) Measures how often items in Y appear in transactions that contain X TID Example: Items 1 Bread, Milk 2 Bread, Diaper, Beer, Eggs 3 Milk, Diaper, Beer, Coke 4 Bread, Milk, Diaper, Beer 5 Bread, Milk, Diaper, Coke { Milk, Diaper} (Milk, Diaper,Beer) s = σ = T σ (Milk, Diaper,Beer) c = = σ (Milk, Diaper) 2 3 2 5 = Beer =.4.67 1/12/12 5
Association rule mining task" Given a set of transactions T, the goal of association rule mining is to find all rules having support minsup threshold confidence minconf threshold Brute-force approach: List all possible association rules Compute the support and confidence for each rule Prune rules that fail the minsup and minconf thresholds Computationally prohibitive! 1/12/12 6
Mining association rules" TID Items 1 Bread, Milk 2 Bread, Diaper, Beer, Eggs 3 Milk, Diaper, Beer, Coke 4 Bread, Milk, Diaper, Beer 5 Bread, Milk, Diaper, Coke Example of Rules: {Milk,Diaper} {Beer} (s=.4, c=.67) {Milk,Beer} {Diaper} (s=.4, c=1.) {Diaper,Beer} {Milk} (s=.4, c=.67) {Beer} {Milk,Diaper} (s=.4, c=.67) {Diaper} {Milk,Beer} (s=.4, c=.5) {Milk} {Diaper,Beer} (s=.4, c=.5) Observations: All the above rules are binary partitions of the same itemset: {Milk, Diaper, Beer} Rules originating from the same itemset have identical support but can have different confidence Thus, we may decouple the support and confidence requirements 1/12/12 7
Mining association rules" Two-step approach: 1. Frequent Itemset Generation Generate all itemsets whose support minsup 2. Rule Generation Generate high confidence rules from each frequent itemset, where each rule is a binary partitioning of a frequent itemset Frequent itemset generation is still computationally expensive 1/12/12 8
Frequent itemset generation" null A B C D E AB AC AD AE BC BD BE CD CE DE ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE Given d items, there are 2 d possible candidate itemsets ABCD ABCE ABDE ACDE BCDE ABCDE 1/12/12 9
Frequent itemset generation" Brute-force approach: Each itemset in the lattice is a candidate frequent itemset Count the support of each candidate by scanning the database N Transactions TID Items 1 Bread, Milk 2 Bread, Diaper, Beer, Eggs 3 Milk, Diaper, Beer, Coke 4 Bread, Milk, Diaper, Beer 5 Bread, Milk, Diaper, Coke w List of Candidates M Match each transaction against every candidate Complexity ~ O(NMw), according to Tan et al. => Expensive since M = 2 d!!! (actually ~ O(NMw 2 /2) is probably a better estimation of the brute force approach) 1/12/12 1
Computational complexity" Given d unique items: Total number of itemsets = 2 d Total number of possible association rules: d 1+ " d R = % d k" d k%. - $ ' $ ' =,# k& # i &/ k=1 i=1 3 d 2 d +1 +1 For example if: d = 6, R = 62 rules 1/12/12 11
Frequent itemset generation strategies" Reduce the number of candidates (M) Complete search: M = 2 d Use pruning techniques to reduce M Reduce the number of transactions (N) Reduce size of N as the size of itemset increases Used by DHP (dynamic hashing and pruning) and vertical-based mining algorithms Reduce the number of comparisons (NM) Use efficient data structures to store the candidates or transactions No need to match every candidate against every transaction 1/12/12 12
Reducing number of candidates" Apriori principle: If an itemset is frequent, then all of its subsets must also be frequent Apriori principle holds due to the following property of the support measure: X, Y : ( X Y ) s( X ) s( Y ) Support of an itemset never exceeds the support of its subsets This is known as the anti-monotone property of support 1/12/12 13
Illustrating Apriori principle" null A B C D E AB AC AD AE BC BD BE CD CE DE Found to be Infrequent ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE ABCD ABCE ABDE ACDE BCDE Pruned supersets ABCDE 1/12/12 14
Illustrating Apriori principle" Item Count Bread 4 Coke 2 Milk 4 Beer 3 Diaper 4 Eggs 1 Minimum Support = 3 Items (1-itemsets) Itemset Count {Bread,Milk} 3 {Bread,Beer} 2 {Bread,Diaper} 3 {Milk,Beer} 2 {Milk,Diaper} 3 {Beer,Diaper} 3 Pairs (2-itemsets) (No need to generate candidates involving Coke or Eggs) Triplets (3-itemsets) If every subset is considered, 6 C 1 + 6 C 2 + 6 C 3 = 41 With support-based pruning, 6 + 6 + 1 = 13 Itemset Count {Bread,Milk,Diaper} 3 1/12/12 15
Apriori algorithm" Method: Let k=1 Generate frequent itemsets of length 1 Repeat until no new frequent itemsets are identified Generate length (k+1) candidate itemsets from length k frequent itemsets Prune candidate itemsets containing subsets of length k that are infrequent Count the support of each candidate by scanning the DB Eliminate candidates that are infrequent, leaving only those that are frequent 1/12/12 16
Reducing number of comparisons" Candidate counting: Scan the database of transactions to determine the support of each candidate itemset To reduce the number of comparisons, store the candidates in a hash structure Instead of matching each transaction against every candidate, match it against candidates contained in the hashed buckets Transactions Hash Structure N TID Items 1 Bread, Milk 2 Bread, Diaper, Beer, Eggs 3 Milk, Diaper, Beer, Coke 4 Bread, Milk, Diaper, Beer 5 Bread, Milk, Diaper, Coke k Buckets 1/12/12 17
Generate hash tree" Suppose you have 15 candidate itemsets of length 3: {1 4 5}, {1 2 4}, {4 5 7}, {1 2 5}, {4 5 8}, {1 5 9}, {1 3 6}, {2 3 4}, {5 6 7}, {3 4 5}, {3 5 6}, {3 5 7}, {6 8 9}, {3 6 7}, {3 6 8} You need: Hash function Max leaf size: max number of itemsets stored in a leaf node (if number of candidate itemsets exceeds max leaf size, split the node) Hash function 3,6,9 1,4,7 2,5,8 1 4 5 1 2 4 4 5 7 1 2 5 4 5 8 2 3 4 5 6 7 3 4 5 3 5 6 1 3 6 3 5 7 6 8 9 1 5 9 3 6 7 3 6 8 1/12/12 18
Association rule discovery: hash tree" Hash Function Candidate Hash Tree 1,4,7 3,6,9 2,5,8 Hash on 1, 4 or 7 1 4 5 1 3 6 1 2 4 1 2 5 1 5 9 4 5 7 4 5 8 2 3 4 5 6 7 3 4 5 3 5 6 3 5 7 6 8 9 3 6 7 3 6 8 1/12/12 19
Association rule discovery: hash tree" Hash Function Candidate Hash Tree 1,4,7 3,6,9 2,5,8 Hash on 2, 5 or 8 1 4 5 1 3 6 1 2 4 1 2 5 1 5 9 4 5 7 4 5 8 2 3 4 5 6 7 3 4 5 3 5 6 3 5 7 6 8 9 3 6 7 3 6 8 1/12/12 2
Association rule discovery: hash tree" Hash Function Candidate Hash Tree 1,4,7 3,6,9 2,5,8 Hash on 3, 6 or 9 1 4 5 1 3 6 1 2 4 1 2 5 1 5 9 4 5 7 4 5 8 2 3 4 5 6 7 3 4 5 3 5 6 3 5 7 6 8 9 3 6 7 3 6 8 1/12/12 21
Subset operation" Given a transaction t, what are the possible subsets of size 3? Transaction, t 1 2 3 5 6 Level 1 1 2 3 5 6 2 3 5 6 3 5 6 Level 2 1 2 3 5 6 1 3 5 6 1 5 6 2 3 5 6 2 5 6 3 5 6 1 2 3 1 2 5 1 2 6 1 3 5 1 3 6 1 5 6 2 3 5 2 3 6 2 5 6 3 5 6 Level 3 Subsets of 3 items 1/12/12 22
Subset operation using hash tree" 1 2 3 5 6 transaction Hash Function 1 + 2 3 5 6 2 + 3 5 6 1,4,7 3,6,9 3 + 5 6 2,5,8 2 3 4 5 6 7 1 4 5 1 3 6 1 2 4 1 2 5 1 5 9 4 5 7 4 5 8 3 4 5 3 5 6 3 6 7 3 5 7 3 6 8 6 8 9 1/12/12 23
Subset operation using hash tree" 1 2 3 5 6 transaction Hash Function 1 2 + 1 3 + 3 5 6 5 6 1 + 2 3 5 6 2 + 3 5 6 3 + 5 6 1,4,7 2,5,8 3,6,9 1 5 + 6 2 3 4 5 6 7 1 4 5 1 3 6 1 2 4 1 2 5 1 5 9 4 5 7 4 5 8 3 4 5 3 5 6 3 6 7 3 5 7 3 6 8 6 8 9 1/12/12 24
Subset operation using hash tree" 1 2 3 5 6 transaction Hash Function 1 2 + 1 3 + 3 5 6 5 6 1 + 2 3 5 6 2 + 3 5 6 3 + 5 6 1,4,7 2,5,8 3,6,9 1 5 + 6 2 3 4 5 6 7 1 4 5 1 3 6 1 2 4 1 2 5 1 5 9 4 5 7 4 5 8 3 4 5 3 5 6 3 5 7 6 8 9 3 6 7 3 6 8 Match transaction against 9 out of 15 candidates 1/12/12 25
Factors affecting complexity" Choice of minimum support threshold lowering support threshold results in more frequent itemsets this may increase number of candidates and max length of frequent itemsets Dimensionality (number of items) of the data set more space is needed to store support count of each item if number of frequent items also increases, both computation and I/O costs may also increase Size of database since Apriori makes multiple passes, run time of algorithm may increase with number of transactions Average transaction width transaction width increases with denser data sets This may increase max length of frequent itemsets and traversals of hash tree (number of subsets in a transaction increases with its width) 1/12/12 26
Compact representation of frequent itemsets" Some itemsets are redundant because they have identical support as their supersets TID A1 A2 A3 A4 A5 A6 A7 A8 A9 A1 B1 B2 B3 B4 B5 B6 B7 B8 B9 B1 C1 C2 C3 C4 C5 C6 C7 C8 C9 C1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 4 1 1 1 1 1 1 1 1 1 1 5 1 1 1 1 1 1 1 1 1 1 6 1 1 1 1 1 1 1 1 1 1 7 1 1 1 1 1 1 1 1 1 1 8 1 1 1 1 1 1 1 1 1 1 9 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 1 1 1 1 1 1 1 1 12 1 1 1 1 1 1 1 1 1 1 13 1 1 1 1 1 1 1 1 1 1 14 1 1 1 1 1 1 1 1 1 1 15 1 1 1 1 1 1 1 1 1 1 Number of frequent itemsets Need a compact representation = ' 1$ " = & # 3 1 % k 1 k 1/12/12 27
Maximal frequent itemset" An itemset is maximal frequent if none of its immediate supersets is frequent null Maximal Itemsets A B C D E AB AC AD AE BC BD BE CD CE DE ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE ABCD ABCE ABDE ACDE BCDE Infrequent Itemsets ABCD E Border 1/12/12 28
Closed itemset" An itemset is closed if none of its immediate supersets has the same support as the itemset TID Items 1 {A,B} 2 {B,C,D} 3 {A,B,C,D} 4 {A,B,D} 5 {A,B,C,D} Itemset Support {A} 4 {B} 5 {C} 3 {D} 4 {A,B} 4 {A,C} 2 {A,D} 3 {B,C} 3 {B,D} 4 {C,D} 3 Itemset Support {A,B,C} 2 {A,B,D} 3 {A,C,D} 2 {B,C,D} 3 {A,B,C,D} 2 1/12/12 29
Maximal vs closed itemsets" null Transaction Ids TID Items 1 ABC 2 ABCD 3 BCE 4 ACDE 5 DE 124 123 1234 245 345 A B C D E 12 124 24 4 123 2 3 24 34 45 AB AC AD AE BC BD BE CD CE DE 12 2 24 4 4 2 3 4 ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE 2 4 ABCD ABCE ABDE ACDE BCDE Not supported by any transactions ABCDE 1/12/12 3
Maximal vs closed frequent itemsets" Minimum support = 2 null Closed but not maximal 124 123 1234 245 345 A B C D E Closed and maximal 12 124 24 4 123 2 3 24 34 45 AB AC AD AE BC BD BE CD CE DE 12 2 24 4 4 2 3 4 ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE 2 4 ABCD ABCE ABDE ACDE BCDE # Closed = 9 # Maximal = 4 ABCDE 1/12/12 31
Maximal vs closed itemsets" Frequent Itemsets Closed Frequent Itemsets Maximal Frequent Itemsets 1/12/12 32
Rule Generation" Given a frequent itemset L, find all non-empty subsets f L such that f L f satisfies the minimum confidence requirement If {A,B,C,D} is a frequent itemset, candidate rules: ABC D, ABD C, ACD B, BCD A, A BCD, B ACD, C ABD, D ABC AB CD, AC BD, AD BC, BC AD, BD AC, CD AB, If L = k, then there are 2 k 2 candidate association rules (ignoring L and L) 1/12/12 33
Rule Generation" How to efficiently generate rules from frequent itemsets? In general, confidence does not have an anti-monotone property c(abc D) can be larger or smaller than c(ab D) But confidence of rules generated from the same itemset has an anti-monotone property e.g., L = {A,B,C,D}: c(abc D) c(ab CD) c(a BCD) Confidence is anti-monotone w.r.t. number of items on the RHS of the rule 1/12/12 34
Lattice of rules Low Confidence Rule Rule generation for Apriori algorithm" ABCD=>{ } BCD=>A ACD=>B ABD=>C ABC=>D CD=>AB BD=>AC BC=>AD AD=>BC AC=>BD AB=>CD Pruned Rules D=>ABC C=>ABD B=>ACD A=>BCD 1/12/12 35
Rule generation for Apriori algorithm" Candidate rule is generated by merging two rules that share the same prefix in the rule consequent join(cd=>ab,bd=>ac) would produce the candidate rule D => ABC CD=>AB BD=>AC Prune rule D=>ABC if its subset AD=>BC does not have high confidence D=>ABC 1/12/12 36
Rule generation algorithm" Key fact: Moving items from the antecedent to the consequent never changes support, and never increases confidence Algorithm For each itemset I with minsup: Find all minconf rules with a single consequent of the form (I - L 1 L 1 ) Repeat: Guess candidate consequents C k by appending items from I - L k-1 to L k-1 Verify confidence of each rule I - C k C k using known itemset support values 1/12/12 37
Algorithm to generate association rules" 1/12/12 38
Pattern evaluation" Association rule algorithms tend to produce too many rules many of them are uninteresting or redundant redundant if {A,B,C} {D} and {A,B} {D} have same support & confidence Interestingness measures can be used to prune/rank the derived patterns In the original formulation of association rules, support & confidence are the only measures used 1/12/12 39
Application of Interestingness measure" Interestingness Measures Patterns Knowledge Postprocessing Selected Data Preprocessed Data Featur Featur e Featur Featur e e Featur e Featur Featur e e Featur Featur e e Featur e e Prod Prod uct Prod uct Prod uct Prod uct Prod uct Prod uct Prod uct Prod uct Prod uct uct Mining Data Preprocessing Selection 1/12/12 4
Computing Interestingness measure" Given a rule X Y, information needed to compute rule interestingness can be obtained from a contingency table Contingency table for X Y Y Y X f 11 f 1 f 1+ X f 1 f f o+ f +1 f + T or N f 11 : support of X and Y f 1 : support of X and Y f 1 : support of X and Y f : support of X and Y Used to define various measures support, confidence, lift, Gini, J-measure, etc. 1/12/12 41
Drawback of Confidence" Coffee Coffee Tea 15 5 2 Tea 75 5 8 9 1 1 Association Rule: Tea Coffee Confidence = P(Coffee Tea) = s(t&c)/s(t) =.75 but P(Coffee) =.9 Although confidence is high, rule is misleading P(Coffee Tea) =.9375 1/12/12 42
Statistical independence" Population of 1 students 6 students know how to swim (S) 7 students know how to bike (B) 42 students know how to swim and bike (S,B) P(S B) = 42/1 =.42 P(S) P(B) =.6.7 =.42 P(S B) = P(S) P(B) => Statistical independence P(S B) > P(S) P(B) => Positively correlated P(S B) < P(S) P(B) => Negatively correlated 1/12/12 43
Statistical-based measures" Measures that take into account statistical dependence ( ) Lift = P Y X = c(x Y ) P( Y ) s(y ) Interest(I) = P ( X,Y ) P( X)P Y = Nf 11 / ( f 1+ f +1 ) ( ) = Nf / f 11 ( f 1+ +1) PS = P(X,Y) P(X)P(Y) P(X,Y) P(X)P(Y) φ coefficient = P(X)[1 P(X)]P(Y)[1 P(Y)] 1/12/12 44
Example: Lift/Interest" Coffee Coffee Tea 15 5 2 Tea 75 5 8 9 1 1 Association Rule: Tea Coffee Confidence = P(Coffee Tea) =.75 However: P(Coffee) =.9 Lift =.75/.9=.8333 (< 1, therefore is slightly negatively correlated) 1/12/12 45
Drawback of Lift & Interest" Y Y Y Y X 1 1 X 9 9 1 9 1 X 9 9 X 1 1 9 1 1.1 Lift = = 1.9 Lift = = 1. 11 (.1)(.1) (.9)(.9) Statistical independence: If P(X,Y)=P(X)P(Y) => Lift = 1 1/12/12 46
There are lots of measures proposed in the literature Some measures are good for certain applications, but not for others How can we determine whether a measure is good or bad?
Property under variable permutation" Does M(A,B) = M(B,A)? Symmetric measures: support, lift, collective strength, cosine, Jaccard, etc Asymmetric measures: confidence, conviction, Laplace, J-measure, etc 1/12/12 48
Property under Row/Column Scaling" Grade-Gender example (Mosteller, 1968): Male Female Male Female High 2 3 5 Low 1 4 5 3 7 1 High 4 3 34 Low 2 4 42 6 7 76 2x 1x Mosteller: Underlying association should be independent of the relative number of male and female students in the samples 1/12/12 49
1/12/12 5 Property under Inversion operation" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 A B C D (a) (b) 1 1 1 1 1 1 1 1 1 (c) E F Transaction 1 Transaction N.....
Example: φ-coefficient" φ-coefficient is analogous to correlation coefficient for continuous variables Y Y X 6 1 7 X 1 2 3 7 3 1 Y Y X 2 1 3 X 1 6 7 3 7 1 φ = =.6.7.7.7.3.7.3.5238 φ =.2.3.3.7.3.7.3.5238 φ Coefficient is the same for both tables = 1/12/12 51
Property under Null addition" Invariant measures: cosine, Jaccard, etc Non-invariant measures: support, correlation, Gini, odds ratio, etc 1/12/12 52
Symmetric objective measures" Also support: S = f 11 / f 11 + f 1 + f 1 + f = f 11 / N 1/12/12 53
Assymetric objective measures" Also Confidence: C = f 11 / f 11 + f 1 = f 11 / f 1+ 1/12/12 54
Properties of symmetric/asymmetric measures" Asymmetric measures, such as confidence, conviction, Laplace and J- measure, do not preserve their values under inversion and row/col scaling but are invariant under the null addition operation 1/12/12 55
Rankings of symmetric measures" 1/12/12 56
Rankings of asymmetric measures" 1/12/12 57
Subjective Interestingness Measure" Objective measure: Rank patterns based on statistics computed from data e.g. measures of association (support, confidence, Laplace, Gini, mutual information, Jaccard, etc). Subjective measure: Rank patterns according to user s interpretation A pattern is subjectively interesting if it contradicts the expectation of a user (Silberschatz & Tuzhilin) A pattern is subjectively interesting if it is actionable (Silberschatz & Tuzhilin) 1/12/12 58