Question? Leave a message!




Mining Association Rules in Large Databases

Mining Association Rules in Large Databases
Mining Association Rules in Large Databases WWW.ThesisScientist.comAssociation 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 MarketBasket transactions Example of Association Rules TID Items Diaper  Beer, 1 Bread, Milk Milk, Bread  Eggs,Coke, 2 Bread, Diaper, Beer, Eggs Beer, Bread  Milk, 3 Milk, Diaper, Beer, Coke 4 Bread, Milk, Diaper, Beer Implication means cooccurrence, not causality 5 Bread, Milk, Diaper, Coke WWW.ThesisScientist.comDefinition: Frequent Itemset  Itemset  A collection of one or more items  Example: Milk, Bread, Diaper TID Items  kitemset  An itemset that contains k items 1 Bread, Milk  Support count () 2 Bread, Diaper, Beer, Eggs  Frequency of occurrence of an 3 Milk, Diaper, Beer, Coke itemset 4 Bread, Milk, Diaper, Beer  E.g. (Milk, Bread,Diaper) = 2 5 Bread, Milk, Diaper, Coke  Support  Fraction of transactions that I assume that itemsets are contain an itemset ordered lexicographically  E.g. s(Milk, Bread, Diaper) = 2/5  Frequent Itemset  An itemset whose support is WWW.ThesisScientist.com greater than or equal to a minsup thresholdDefinition: Association Rule Let D be database of transactions  e.g.: Transaction ID Items Bought 2000 A,B,C 1000 A,C 4000 A,D 5000 B,E,F  Let I be the set of items that appear in the database, e.g., I=A,B,C,D,E,F  A rule is defined by X  Y, where XI, YI, and XY=  e.g.: B,C  E is a rule WWW.ThesisScientist.comDefinition: Association Rule TID Items  Association Rule 1 Bread, Milk  An implication expression of the 2 Bread, Diaper, Beer, Eggs form X  Y, where X and Y are 3 Milk, Diaper, Beer, Coke itemsets 4 Bread, Milk, Diaper, Beer  Example: 5 Bread, Milk, Diaper, Coke Milk, Diaper  Beer Example:  Rule Evaluation Metrics Milk,Diaper Beer  Support (s)  Fraction of transactions that contain both X and Y  (Milk,Diaper,Beer) 2 s 0.4  Confidence (c) T 5  Measures how often items in Y  (Milk, Diaper,Beer) 2 appear in transactions that c 0.67 contain X  (Milk,Diaper) 3 WWW.ThesisScientist.comRule Measures: Support and Confidence Customer Customer Find all the rules X  Y with buys both buys diaper minimum confidence and support  support, s, probability that a transaction contains X  Y  confidence, c, conditional Customer probability that a transaction buys beer having X also contains Y Let minimum support 50, Transaction ID Items Bought and minimum confidence 2000 A,B,C 50, we have 1000 A,C  A  C (50, 66.6) 4000 A,D  C  A (50, 100) 5000 B,E,F WWW.ThesisScientist.comExample TID date itemsbought 100 10/10/99 F,A,D,B Remember: 200 15/10/99 D,A,C,E,B sup(X  Y) 300 19/10/99 C,A,B,E conf(X  Y) = sup(X) 400 20/10/99 B,A,D  What is the support and confidence of the rule: B,D  A  Support: 75  percentage of tuples that contain A,B,D =  Confidence: number of tuples that contain A,B,D  100 WWW.ThesisScientist.com number of tuples that contain B,DAssociation 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 Bruteforce approach:  List all possible association rules  Compute the support and confidence for each rule  Prune rules that fail the minsup and minconf thresholds WWW.ThesisScientist.com  Computationally prohibitiveMining Association Rules TID Items Example of Rules: 1 Bread, Milk Milk,Diaper  Beer (s=0.4, c=0.67) 2 Bread, Diaper, Beer, Eggs Milk,Beer  Diaper (s=0.4, c=1.0) 3 Milk, Diaper, Beer, Coke Diaper,Beer  Milk (s=0.4, c=0.67) 4 Bread, Milk, Diaper, Beer Beer  Milk,Diaper (s=0.4, c=0.67) 5 Bread, Milk, Diaper, Coke Diaper  Milk,Beer (s=0.4, c=0.5) Milk  Diaper,Beer (s=0.4, c=0.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 WWW.ThesisScientist.comMining Association Rules  Twostep 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 WWW.ThesisScientist.comFrequent 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 ABCD ABCE ABDE ACDE BCDE d are 2 possible candidate itemsets WWW.ThesisScientist.com ABCDEFrequent Itemset Generation  Bruteforce approach:  Each itemset in the lattice is a candidate frequent itemset  Count the support of each candidate by scanning the database List of Transactions Candidates TID Items 1 Bread, Milk 2 Bread, Diaper, Beer, Eggs 3 Milk, Diaper, Beer, Coke M N 4 Bread, Milk, Diaper, Beer 5 Bread, Milk, Diaper, Coke w  Match each transaction against every candidate d  Complexity O(NMw) = Expensive since M = 2 WWW.ThesisScientist.comComputational Complexity Given d unique items: d  Total number of itemsets = 2  Total number of possible association rules: d d k  d1 dk  R   k1 j1 k j   d d1  3 21 If d=6, R = 602 rules WWW.ThesisScientist.comFrequent Itemset Generation Strategies Reduce the number of candidates (M) d  Complete search: M=2  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 and verticalbased 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 WWW.ThesisScientist.comReducing 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 antimonotone property of WWW.ThesisScientist.com supportExample TID Items 1 Bread, Milk 2 Bread, Diaper, Beer, Eggs s(Bread) s(Bread, Beer) 3 Milk, Diaper, Beer, Coke s(Milk) s(Bread, Milk) 4 Bread, Milk, Diaper, Beer 5 Bread, Milk, Diaper, Coke s(Diaper, Beer) s(Diaper, Beer, Coke) WWW.ThesisScientist.comIllustrating Apriori Principle n nu ullll A A B B C C D D E E A AB B A AC C A AD D A AE E B BC C B BD D B BE E C CD D C CE E D DE E Found to be Infrequent A AB BC C A AB BD D A AB BE E A AC CD D A AC CE E A AD DE E B BC CD D B BC CE E B BD DE E C CD DE E A AB BC CD D A AB BC CE E A AB BD DE E A AC CD DE E B BC CD DE E Pruned WWW.ThesisScientist.com A AB BC CD DE E supersetsIllustrating Apriori Principle Item Count Items (1itemsets) Bread 4 Coke 2 Milk 4 Pairs (2itemsets) Itemset Count Beer 3 Bread,Milk 3 Diaper 4 Bread,Beer 2 (No need to generate Eggs 1 Bread,Diaper 3 candidates involving Coke Milk,Beer 2 or Eggs) Milk,Diaper 3 Beer,Diaper 3 Minimum Support = 3 Triplets (3itemsets) Itemset Count If every subset is considered, 6 6 6 Bread,Milk,Diaper 3 C + C + C = 41 1 2 3 With supportbased pruning, 6 + 6 + 1 = 13 WWW.ThesisScientist.comThe Apriori Algorithm (the general idea) 1. Find frequent 1items and put them to L (k=1) k 2. Use L to generate a collection of candidate k itemsets C with size (k+1) k+1 3. Scan the database to find which itemsets in C k+1 are frequent and put them into L k+1 4. If L is not empty k+1  k=k+1  GOTO 2 R. Agrawal, R. Srikant: "Fast Algorithms for Mining Association Rules", WWW.ThesisScientist.com Proc. of the 20th Int'l Conference on Very Large Databases, Santiago, Chile, Sept. 1994. The Apriori Algorithm  Pseudocode: C : Candidate itemset of size k k L : frequent itemset of size k k L = frequent items; 1 for (k = 1; L =; k++) do begin k C = candidates generated from L ; k+1 k // join and prune steps for each transaction t in database do increment the count of all candidates in C k+1 that are contained in t L = candidates in C with minsupport (frequent) k+1 k+1 end return L ; k k  Important steps in candidate generation:  Join Step: C is generated by joining L with itself k+1 k  Prune Step: Any kitemset that is not frequent cannot be a subset of a frequent (k+1)itemset WWW.ThesisScientist.comThe Apriori Algorithm — Example Database D itemset sup. itemset sup. L 1 TID Items C 1 2 1 1 2 2 3 100 1 3 4 2 3 Scan D 3 3 200 2 3 5 3 3 300 1 2 3 5 4 1 5 3 400 2 5 5 3 minsup=2=50 itemset C C 2 itemset sup 2 1 2 itemset sup L 1 2 1 Scan D 2 1 3 1 3 2 1 3 2 1 5 1 5 1 2 3 2 2 3 2 3 2 2 5 3 2 5 2 5 3 3 5 2 3 5 3 5 2 C L itemset itemset sup 3 3 Scan D WWW.ThesisScientist.com 2 3 5 2 3 5 2How to Generate Candidates  Suppose the items in L are listed in an order k  Step 1: selfjoining L (IN SQL) k insert into C k+1 select p.item , p.item , …, p.item , q.item 1 2 k k from L p, L q k k where p.item =q.item , …, p.item =q.item , p.item 1 1 k1 k1 k q.item k  Step 2: pruning forall itemsets c in C do k+1 forall ksubsets s of c do if (s is not in L ) then delete c from C k k+1 WWW.ThesisScientist.comExample of Candidates Generation  L =abc, abd, acd, ace, bcd 3  Selfjoining: L L 3 3 a,c,d a,c,e  abcd from abc and abd a,c,d,e X  acde from acd and ace cde acd ace ade X   Pruning:  acde is removed because ade is not in L 3  C =abcd 4 WWW.ThesisScientist.comHow to Count Supports of Candidates  Why counting supports of candidates a problem  The total number of candidates can be huge  One transaction may contain many candidates  Method:  Candidate itemsets are stored in a hashtree  Leaf node of hashtree contains a list of itemsets and counts  Interior node contains a hash table  Subset function: finds all the candidates contained in a transaction WWW.ThesisScientist.comExample of the hashtree for C 3 Hash function: mod 3 H st H Hash on 1 item 1,4,.. 2,5,.. 3,6,.. nd 234 Hash on 2 item H H 567 145 345 356 367 H rd Hash on 3 item 689 368 124 125 159 457 458 WWW.ThesisScientist.comExample of the hashtree for C 3 2345 345 Hash function: mod 3 look for 2XX look for 3XX 12345 H st H Hash on 1 item 12345 1,4,.. 2,5,.. 3,6,.. look for 1XX nd 234 Hash on 2 item H H 567 145 345 356 367 H rd Hash on 3 item 689 368 124 125 159 457 458 WWW.ThesisScientist.comExample of the hashtree for C 3 2345 345 Hash function: mod 3 look for 2XX look for 3XX 12345 H st H Hash on 1 item 12345 1,4,.. 2,5,.. 3,6,.. look for 1XX nd 234 Hash on 2 item H H 567 12345  look for 12X 145 345 356 367 H 689 368 12345 look for 13X (null) 124 125 159 457 458 12345 look for 14X WWW.ThesisScientist.comAprioriTid: Use D only for first pass st  The database is not used after the 1 pass.  Instead, the set C ’ is used for each step, C ’ = k k TID, X : each X is a potentially frequent k k itemset in transaction with id=TID.  At each step C ’ is generated from C ’ at the k k1 pruning step of constructing C and used to k compute L . k  For small values of k, C ’ could be larger than k the database WWW.ThesisScientist.comAprioriTid Example (minsup=2) L 1 C’ Database D 1 itemset sup. TID Sets of itemsets TID Items 100 1,3,4 1 2 100 1 3 4 200 2,3,5 2 3 200 2 3 5 300 1,2,3,5 3 3 300 1 2 3 5 400 2,5 5 3 400 2 5 C’ 1 itemset L 2 TID Sets of itemsets 1 2 itemset sup 100 1 3 C 2 1 3 200 2 3,2 5,3 5 1 3 2 1 5 300 1 2,1 3,1 5, 2 2 3 2 3,2 5,3 5 2 3 2 5 3 400 2 5 2 5 3 5 2 3 5 L TID Sets of itemsets 3 itemset sup 200 2 3 5 itemset C 3 300 2 3 5 2 3 5 2 C’ 2 3 5 3 WWW.ThesisScientist.comMethods to Improve Apriori’s Efficiency  Hashbased itemset counting: A kitemset whose  corresponding hashing bucket count is below the threshold cannot be frequent  Transaction reduction: A transaction that does not contain  any frequent kitemset is useless in subsequent scans  Partitioning: Any itemset that is potentially frequent in DB must be frequent in at least one of the partitions of DB  Sampling: mining on a subset of given data, lower support threshold + a method to determine the completeness  Dynamic itemset counting: add new candidate itemsets only when all of their subsets are estimated to be frequent WWW.ThesisScientist.comMaximal Frequent Itemset An itemset is maximal frequent if none of its immediate supersets is frequent null Maximal A B C D E Itemsets 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 Border ABCD E WWW.ThesisScientist.comClosed Itemset  An itemset is closed if none of its immediate supersets has the same support as the itemset Itemset Support A 4 TID Items Itemset Support B 5 1 A,B A,B,C 2 C 3 2 B,C,D A,B,D 3 D 4 3 A,B,C,D A,C,D 2 A,B 4 4 A,B,D B,C,D 3 A,C 2 5 A,B,C,D A,B,C,D 2 A,D 3 B,C 3 B,D 4 C,D 3 WWW.ThesisScientist.comMaximal vs Closed Itemsets Transaction Ids null 124 123 1234 245 345 A B C D E TID Items 1 ABC 12 124 24 123 4 2 3 24 34 2 ABCD 45 AB AC AD AE BC BD BE CD CE DE 3 BCE 4 ACDE 12 24 2 2 4 4 3 4 5 DE ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE 4 2 ABCD ABCE ABDE ACDE BCDE Not supported by any transactions ABCDE WWW.ThesisScientist.comMaximal vs Closed Frequent Itemsets Closed but null Minimum support = 2 not maximal 124 123 1234 245 345 A B C D E Closed and maximal 12 124 24 123 4 2 3 24 34 45 AB AC AD AE BC BD BE CD CE DE 12 24 2 2 4 4 3 4 ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE 4 2 ABCD ABCE ABDE ACDE BCDE Closed = 9 Maximal = 4 ABCDE WWW.ThesisScientist.comMaximal vs Closed Itemsets Frequent Itemsets Closed Frequent Itemsets Maximal Frequent Itemsets WWW.ThesisScientist.comFactors 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) WWW.ThesisScientist.comRule 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, k If L = k, then there are 2 – 2 candidate association rules (ignoring L  and  L) WWW.ThesisScientist.comRule 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 antimonotone property  e.g., L = A,B,C,D: c(ABC  D)  c(AB  CD)  c(A  BCD)  Confidence is antimonotone w.r.t. number of items WWW.ThesisScientist.com on the RHS of the ruleRule Generation for Apriori Algorithm Lattice of rules A AB BC CD D= = Low Confidence Rule B BC CD D= = A A A AC CD D= = B B A AB BD D= = C C A AB BC C= = D D C CD D= = A AB B B BD D= = A AC C B BC C= = A AD D A AD D= = B BC C A AC C= = B BD D A AB B= = C CD D D D= = A AB BC C C C= = A AB BD D B B= = A AC CD D A A= = B BC CD D Pruned Rules WWW.ThesisScientist.comRule Generation for Apriori Algorithm Candidate rule is generated by merging two rules that share the same prefix in the rule consequent CD=AB BD=AC join(CD=AB,BD=AC) would produce the candidate rule D = ABC D=ABC Prune rule D=ABC if its subset AD=BC does not have WWW.ThesisScientist.com high confidenceIs Apriori Fast Enough — Performance Bottlenecks  The core of the Apriori algorithm:  Use frequent (k – 1)itemsets to generate candidate frequent k itemsets  Use database scan and pattern matching to collect counts for the candidate itemsets  The bottleneck of Apriori: candidate generation  Huge candidate sets: 4 7  10 frequent 1itemset will generate 10 candidate 2 itemsets  To discover a frequent pattern of size 100, e.g., a , a , …, 1 2 100 30 a , one needs to generate 2 10 candidates. 100  Multiple scans of database:  Needs (n +1 ) scans, n is the length of the longest pattern WWW.ThesisScientist.comFPgrowth: Mining Frequent Patterns Without Candidate Generation  Compress a large database into a compact, FrequentPattern tree (FPtree) structure  highly condensed, but complete for frequent pattern mining  avoid costly database scans  Develop an efficient, FPtreebased frequent pattern mining method  A divideandconquer methodology: decompose mining tasks into smaller ones  Avoid candidate generation: subdatabase test only WWW.ThesisScientist.comFPtree Construction from a Transactional DB minsupport = 3 TID Items bought (ordered) frequent items 100 f, a, c, d, g, i, m, p f, c, a, m, p Item frequency 200 a, b, c, f, l, m, o f, c, a, b, m f 4 300 b, f, h, j, o f, b c 4 400 b, c, k, s, p c, b, p a 3 b 3 500 a, f, c, e, l, p, m, n f, c, a, m, p m 3 p 3 Steps: 1. Scan DB once, find frequent 1itemsets (single item patterns) 2. Order frequent items in descending order of their frequency 3. Scan DB again, construct FPtree WWW.ThesisScientist.comFPtree Construction minsupport = 3 TID freq. Items bought Item frequency 100 f, c, a, m, p f 4 200 f, c, a, b, m c 4 300 f, b a 3 400 c, p, b root b 3 500 f, c, a, m, p m 3 p 3 f:1 c:1 a:1 m:1 p:1 WWW.ThesisScientist.comFPtree Construction minsupport = 3 TID freq. Items bought Item frequency 100 f, c, a, m, p f 4 200 f, c, a, b, m c 4 300 f, b a 3 400 c, p, b root b 3 500 f, c, a, m, p m 3 p 3 f:2 c:2 a:2 m:1 b:1 p:1 m:1 WWW.ThesisScientist.comFPtree Construction minsupport = 3 TID freq. Items bought Item frequency 100 f, c, a, m, p f 4 200 f, c, a, b, m c 4 300 f, b a 3 400 c, p, b root b 3 500 f, c, a, m, p m 3 p 3 f:3 c:1 c:2 b:1 b:1 a:2 p:1 m:1 b:1 p:1 m:1 WWW.ThesisScientist.comFPtree Construction minsupport = 3 TID freq. Items bought 100 f, c, a, m, p Item frequency 200 f, c, a, b, m f 4 300 f, b c 4 400 c, p, b a 3 root 500 f, c, a, m, p b 3 m 3 Header Table f:4 p 3 c:1 Item frequency head f 4 c:3 b:1 b:1 c 4 a 3 a:3 p:1 b 3 m 3 p 3 m:2 b:1 p:2 m:1 WWW.ThesisScientist.comBenefits of the FPtree Structure  Completeness:  never breaks a long pattern of any transaction  preserves complete information for frequent pattern mining  Compactness  reduce irrelevant information—infrequent items are gone  frequency descending ordering: more frequent items are more likely to be shared  never be larger than the original database (if not count nodelinks and counts)  Example: For Connect4 DB, compression ratio could be over 100 WWW.ThesisScientist.comMining Frequent Patterns Using FPtree  General idea (divideandconquer)  Recursively grow frequent pattern path using the FPtree  Method  For each item, construct its conditional patternbase, and then its conditional FPtree  Repeat the process on each newly created conditional FP tree  Until the resulting FPtree is empty, or it contains only one path (single path will generate all the combinations of its subpaths, each of which is a frequent pattern) WWW.ThesisScientist.comMining Frequent Patterns Using the FPtree (cont’d)  Start with last item in order (i.e., p).  Follow node pointers and traverse only the paths containing p.  Accumulate all of transformed prefix paths of that item to form a conditional pattern base f:4 c:1 Conditional pattern base for p fcam:2, cb:1 c:3 b:1 Construct a new FPtree based a:3 p:1 on this pattern, by merging all p paths and keeping nodes that m:2 appear sup times. This leads to only one branch c:3 p:2 Thus we derive only one frequent pattern cont. p. Pattern cp WWW.ThesisScientist.comMining Frequent Patterns Using the FPtree (cont’d)  Move to next least frequent item in order, i.e., m  Follow node pointers and traverse only the paths containing m.  Accumulate all of transformed prefix paths of that item to form a conditional pattern base mconditional pattern base: f:4 fca:2, fcab:1 All frequent patterns c:3 that include m m, a:3 m  f:3 fm, cm, am, m:2 b:1 c:3 fcm, fam, cam, fcam m:1 a:3 mconditional FPtree (contains only path fca:3) WWW.ThesisScientist.comProperties of FPtree for Conditional Pattern Base Construction  Nodelink property  For any frequent item a , all the possible frequent patterns i that contain a can be obtained by following a 's nodelinks, i i starting from a 's head in the FPtree header i  Prefix path property  To calculate the frequent patterns for a node a in a path P, i only the prefix subpath of a in P need to be accumulated, i and its frequency count should carry the same count as node a . i WWW.ThesisScientist.comConditional PatternBases for the example Conditional patternbase Conditional FPtree Item p (fcam:2), (cb:1) (c:3)p m (fca:2), (fcab:1) (f:3, c:3, a:3)m b (fca:1), (f:1), (c:1) Empty a (fc:3) (f:3, c:3)a c (f:3) (f:3)c f Empty Empty WWW.ThesisScientist.comWhy Is Frequent Pattern Growth Fast  Performance studies show  FPgrowth is an order of magnitude faster than Apriori, and is also faster than treeprojection  Reasoning  No candidate generation, no candidate test  Uses compact data structure  Eliminates repeated database scan  Basic operation is counting and FPtree building WWW.ThesisScientist.comFPgrowth vs. Apriori: Scalability With the Support Threshold Data set T25I20D10K 100 D1 FPgrow th runtime 90 D1 Apriori runtime 80 70 60 50 40 30 20 10 0 0 0.5 1 1.5 2 2.5 3 Support threshold() WWW.ThesisScientist.com Run time(sec.)