Masket Basket Analysis

Market-Basket Model

  • Goal: Identify items that are bought together by sufficiently many customers
  • Approach: Process the sales data collected with barcode scanners to find dependencies among items
  • Example:

  • Association Rules: People who bought {x,y,z} tend to buy {v,w}

  • Implementation:
    1. Find frequent itemsets
    2. Generate association rules
  • Applications:
    • Items = products; Baskets = sets of products someone bought in one trip to the store
    • Items = documents (containing sentences); Baskets = sentences
    • Items = drugs & side-effects; Baskets = patients

Frequent Itemsets

Question: Find the sets of items that appear together “frequently” in baskets, given a support threshold $s$, that a set of items appears in at least $s$ baskets are called as frequent itemsets.

  • Example:

Association Rules Mining

Association Rules

$\lbrace i_1, i_2, \dots, i_k \rbrace \rightarrow j $: if a basket contains all $i_1, \dots, i_k$, then it is likely to contain $j$

  • Support is the frequency of combination of items
  • Confidence of the association rule is the probability of $j$ given $I = \lbrace i_1, i_2, \dots, i_k \rbrace$
  • Interest of an association rule $I \rightarrow j$ denotes the difference between its confidence and the fraction of baskets that contain $j$. Interesting rules are those with high positive or negative interest values
  • Example:

Properties

  1. Subset of Frequent Itemset Concept

    • Frequent Itemsets: If $\lbrace i_1, i_2,…, i_k \rbrace \rightarrow j$ has high support and confidence, then both $\lbrace i_1, i_2,…, i_k \rbrace$ and $\lbrace i_1, i_2,…,i_k, j \rbrace$ will be ‘frequent’.

    • Idea: If A,B,C → D is below confidence, so is A,B → C,D
      Since Supp.(ABC) ≤ Supp.(AB)
      Thus Conf.(ABC → D) = Supp.(ABCD) / Supp.(ABC) ≥ Supp.(ABCD) / Supp.(AB) = Conf.(AB → CD)

      If Conf.(AB → CD) ≥ $s$, then Conf.(ABC → D) ≥ $s$;

      Conf.(ABC → D) ≤ $s$, Conf.(AB → CD) ≤ $s$

    • If Conf.(AB → CD) is above the threshold, we can generate the addtitional rules like ABC → D and ABD → C

  1. Maximal Frequent Itemsets
    • Based on above inference, we only need to store the maximal frequent itemsets.
    • Definition: A Frequent set for which NO immediate superset is frequent. (i.e., Impossible for adding an item to make the set more bigger.)
    • Properties:
      1. All subsets of Maximal Frequent Itemsets are frequent
      2. Every frequent itemset must be a subset of some Maximal Frequent Itemsets

Finding Frequent Itemsets

Typically, data is kept in flat files rather than in a database system: Stored on disk, basket-by-basket.
Expand baskets into pairs, triples by using k nested loops to generate all sets of size k.

  • Computing Evaluation:
    1. The true cost of mining disk-resident data is usually the number of disk I/O’s
      • we measure the cost by the number of passes an algorithm makes over the data
    2. For many frequent-itemset algorithms, main-memory is the critical resource:
      • the number of different things we can count is limited by main memory (number of counters are limited by the capacity of main memory)
      • it is a disaster for swapping counts in/out (it requires large amount of disk I/Os)

Naïve Algorithm

  • Implementation: Finding frequent pairs

    • Read file once, counting in main memory the occurrences of each pair

    • From each basket of n items, generate its nC2 pairs by two nested loops:

      Suppose a webpage with $10^5$ items using the 4-byte integers counter. Number of pairs of items:

      $10^5C2 = 10^5(10^5-1)/2 = 5\times10^9$ items, which $2\times10^{10} = 20$GB of memory needed

  • Approaches:

    1. Matrix Approach: Count all pairs using a matrix

      • requires 4 bytes per pair
    2. Triangular Matrix: Reduce space wasting

      • Keep pair counts in lexicographic order: {1,2},..., {1,n}, {2,3}, ..., {2,n}, {3,4}, ...
      • Pair {i, j} is at position: $(i - 1)(n - i/2) + j - 1$, total byte = $2n^2$
    3. Count all pairs using a hash table of triples [i, j, c] with keys {i,j} and count c

      • requires 12 bytes per pair (only for pairs that count > 0)

A-Priori Algorithm

A-Priori Algorithm uses frequent item sets to generate association rules. It based on the concept that a subset of a frequent itemset must also be a frequent itemset.

  • Algorithm:

    • Pass 1: Read baskets and count in main memory the occurrences of each individual item

      • Requests memory proportional to number of items
    • Pass 2: Read baskets again and count in main memory only those pairs where both elements are frequent (from Pass 1)

      • Requests memory proportional to square of number of items (for counters)
      • Requests a list of frequent items
    • Noted: Pass is defined as the action of “reading from disk” (not include the action of reading main memory)

  • Execution Flow:

    • $C_k$ denotes candidate k-tuples (those that might be frequent sets based on information from the pass for k–1)
    • $L_k$ denotes the set of truly frequent k-tuples
  • Demonstration:

    1. 1st Iteration:

    2. 2nd Iteration:

    3. Pruning: Every subset of a frequent itemset must also be a frequent itemset.

    4. 3nd Iteration:

PCY Algorithm

  • Algorithm:

    • Pass 1:

      1
      2
      3
      4
      5
      6
      FOR (each basket):
      FOR (each item in the basket):
      add 1 to item's counter
      FOR (each pair of items):
      hash the pair to a bucket
      add 1 to the counter for that bucket
    • Pass 2: Count all pairs {i,j} that meet the conditions for being a candidate pair:

      1. Both i and j are frequent items
      2. The pair {i,j} hashes to a bucket whose bit in a bit vector is 1