Example 1: Apriori

The data are 50 transactions involving five different product ID's. This example applies Apriori to find the frequent itemsets and strong association rules. The minimum support percentage is set to 0.30, giving a minimum required support of 15 transactions.


import com.imsl.datamining.*;

public class AprioriEx1 {

    public static void main(String[] args) {
        int nprods = 5;
        int[][] x = {
            {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}
        };

        // Run the algorithm on transactions X.
        Itemsets itemsets = Apriori.getFrequentItemsets(x, nprods, 10, 0.30);
        itemsets.print();

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

Output

Frequent Itemsets (Out of 50 Transactions):
Size   Support  Itemset
1      27       {0}
1      30       {1}
1      33       {2}
1      27       {3}
1      27       {4}
2      20       {0 1}
2      22       {0 2}
2      16       {0 3}
2      19       {0 4}
2      22       {1 2}
2      16       {1 3}
2      15       {1 4}
2      16       {2 3}
2      19       {2 4}
2      17       {3 4}
3      17       {0 1 2}
3      15       {0 2 4}

Association Rules (itemset X implies itemset Y):
X = {0} ==> Y = {2}
  supp(X)=27, supp(Y)=33, supp(X and Y)=22
  conf= 0.81, lift=1.23
X = {0 1} ==> Y = {2}
  supp(X)=20, supp(Y)=33, supp(X and Y)=17
  conf= 0.85, lift=1.29
Link to Java source.