Example 2: Apriori

The data are two separate blocks of 50 transactions involving five different product ID's. This example shows how to apply Apriori to separate blocks of data and combine results. The minimum support percentage is set to 0.30, giving a minimum required support of 30 transactions overall.


import com.imsl.datamining.*;

public class AprioriEx2 {

    public static void main(String[] args) {
        int numProducts = 5;
        int[][] x1 = {
            {1, 3}, {1, 2}, {1, 1}, {2, 1}, {2, 2}, {2, 4}, {2, 5},
            {3, 3}, {4, 4}, {4, 3}, {4, 5}, {4, 1}, {5, 5}, {6, 1}, {6, 2},
            {6, 3}, {7, 5}, {7, 3}, {7, 2}, {8, 3}, {8, 4}, {8, 1}, {8, 5},
            {8, 2}, {9, 4}, {10, 5}, {10, 3}, {11, 2}, {11, 3}, {12, 4},
            {13, 4}, {14, 2}, {14, 3}, {14, 1}, {15, 3}, {15, 5}, {15, 1},
            {16, 2}, {17, 3}, {17, 5}, {17, 1}, {18, 5}, {18, 1}, {18, 2},
            {18, 3}, {19, 2}, {20, 4}, {21, 1}, {21, 4}, {21, 2}, {21, 5},
            {22, 5}, {22, 4}, {23, 2}, {23, 5}, {23, 3}, {23, 1}, {23, 4},
            {24, 3}, {24, 1}, {24, 5}, {25, 3}, {25, 5}, {26, 1}, {26, 4},
            {26, 2}, {26, 3}, {27, 2}, {27, 3}, {27, 1}, {27, 5}, {28, 5},
            {28, 3}, {28, 4}, {28, 1}, {28, 2}, {29, 4}, {29, 5}, {29, 2},
            {30, 2}, {30, 4}, {30, 3}, {31, 2}, {32, 5}, {32, 1}, {32, 4},
            {33, 4}, {33, 1}, {33, 5}, {33, 3}, {33, 2}, {34, 3}, {35, 5},
            {35, 3}, {36, 3}, {36, 5}, {36, 4}, {36, 1}, {36, 2}, {37, 1},
            {37, 3}, {37, 2}, {38, 4}, {38, 2}, {38, 3}, {39, 3}, {39, 2},
            {39, 1}, {40, 2}, {40, 1}, {41, 3}, {41, 5}, {41, 1}, {41, 4},
            {41, 2}, {42, 5}, {42, 1}, {42, 4}, {43, 3}, {43, 2}, {43, 4},
            {44, 4}, {44, 5}, {44, 2}, {44, 3}, {44, 1}, {45, 4}, {45, 5},
            {45, 3}, {45, 2}, {45, 1}, {46, 2}, {46, 4}, {46, 5}, {46, 3},
            {46, 1}, {47, 4}, {47, 5}, {48, 2}, {49, 1}, {49, 4}, {49, 3},
            {50, 3}, {50, 4}
        };

        int[][] x2 = {
            {1, 2}, {1, 1}, {1, 4}, {1, 3}, {2, 2}, {2, 5}, {2, 3}, {2, 1},
            {2, 4}, {3, 5}, {3, 4}, {4, 2}, {5, 4}, {5, 2}, {5, 3}, {5, 5},
            {6, 3}, {6, 5}, {7, 2}, {7, 5}, {7, 4}, {7, 1}, {7, 3}, {8, 2},
            {9, 2}, {9, 4}, {10, 4}, {10, 2}, {11, 4}, {11, 1}, {12, 3},
            {12, 1}, {12, 5}, {12, 2}, {13, 2}, {14, 3}, {14, 4}, {14, 2},
            {15, 2}, {16, 5}, {16, 2}, {16, 4}, {17, 1}, {18, 2}, {18, 3},
            {18, 4}, {19, 3}, {19, 1}, {19, 2}, {19, 4}, {20, 5}, {20, 1},
            {21, 5}, {21, 4}, {21, 1}, {21, 3}, {22, 4}, {22, 1}, {22, 5},
            {23, 1}, {23, 2}, {24, 4}, {25, 4}, {25, 3}, {26, 5}, {26, 2},
            {26, 3}, {26, 4}, {26, 1}, {27, 2}, {27, 1}, {27, 5}, {27, 3},
            {28, 1}, {28, 2}, {28, 3}, {28, 4}, {29, 5}, {29, 2}, {29, 1},
            {30, 5}, {30, 3}, {30, 2}, {30, 4}, {31, 4}, {31, 1}, {32, 1},
            {32, 2}, {32, 3}, {32, 4}, {32, 5}, {33, 3}, {33, 2}, {33, 4},
            {33, 5}, {33, 1}, {34, 3}, {34, 4}, {34, 5}, {34, 2}, {35, 2},
            {35, 3}, {36, 3}, {36, 5}, {36, 4}, {37, 1}, {37, 4}, {37, 2},
            {37, 3}, {37, 5}, {38, 5}, {38, 3}, {38, 1}, {38, 2}, {39, 2},
            {39, 5}, {40, 4}, {40, 2}, {41, 4}, {42, 4}, {43, 5}, {43, 4},
            {44, 5}, {44, 4}, {44, 3}, {44, 2}, {44, 1}, {45, 1}, {45, 2},
            {45, 3}, {45, 5}, {45, 4}, {46, 3}, {46, 4}, {47, 4}, {47, 5},
            {47, 2}, {47, 3}, {48, 5}, {48, 3}, {48, 2}, {48, 1}, {48, 4},
            {49, 4}, {49, 5}, {50, 4}, {50, 1}
        };

        // Find frequent itemsets in x1 and x2.
        Itemsets fis1
                = Apriori.getFrequentItemsets(x1, numProducts, 4, 0.30);
        Itemsets fis2
                = Apriori.getFrequentItemsets(x2, numProducts, 4, 0.30);

        // Get the union of fis1 and fis2.
        Itemsets cis = Apriori.getUnion(fis1, fis2);

        // Count the frequencies of the itemsets in the union in
        // each of the data sets.
        int[] freq1 = Apriori.countFrequency(cis, x1);
        int[] freq2 = Apriori.countFrequency(cis, x2);
        int[] freq = Apriori.sum(freq1, freq2);

        // Get the frequent itemset of the union.
        Itemsets itemsets = Apriori.updateFrequentItemsets(cis, freq);
        itemsets.print();

        // Generate and print the association rules.
        AssociationRule.print(Apriori.getAssociationRules(itemsets, 0.80, 2.0));
    }
}

Output

Frequent Itemsets (Out of 100 Transactions):
Size   Support  Itemset
1      51       {0}
1      63       {1}
1      60       {2}
1      63       {3}
1      54       {4}
2      37       {0 1}
2      38       {0 2}
2      33       {0 3}
2      35       {0 4}
2      44       {1 2}
2      38       {1 3}
2      34       {1 4}
2      38       {2 3}
2      38       {2 4}
2      37       {3 4}
3      32       {0 1 2}

Association Rules (itemset X implies itemset Y):
X = {0 1} ==> Y = {2}
  supp(X)=37, supp(Y)=60, supp(X and Y)=32
  conf= 0.86, lift=1.44
X = {0 2} ==> Y = {1}
  supp(X)=38, supp(Y)=63, supp(X and Y)=32
  conf= 0.84, lift=1.34
Link to Java source.