Importing the library

In order to perform validation we will import the library and build the transactions as shown here:

import pyfpgrowth

We build our transactions like so:

transaction = [["bread", "butter", "cereal"],
["butter", "milk"],
["bread", "milk"],
["butter", "cereal", "milk"],
["egg", "bread"],
["egg", "butter"],
["cereal", "milk"],
["bread", "butter", "cereal", "egg"],
["cereal", "bread", "butter"]]

Minimum support is defined now to find the pattern. find_frequent_patterns(), where transactions are the list of items bought at each transaction, and 2 is the minimum threshold set for support count:

patterns = pyfpgrowth.find_frequent_patterns(transaction, 2)

Finally, we have to define the confidence to get the rules. Rules are generated based on the patterns and 0.5 is the minimum threshold set for confidence. Then, we store the rules in a dataframe named rules. rules initially consists of an antecedent, a consequent, and the confidence value:

rules = pyfpgrowth.generate_association_rules(patterns, 0.5)
print(rules)

We get the output as follows:

This is how we get the rules. FP-growth tends to have the edge over Apriori as it is faster and more efficient.