Generating association rules
As you saw, the function permutations
from the module itertools
can be used to quickly generate the set of all one-antecedent, one-consequent rules. You do not, of course, know which of these rules are useful. You simply know that each is a valid way to combine two items.
Let's practice generating and counting the set of all rules for a subset of the grocery dataset: coffee, tea, milk, and sugar.
This exercise is part of the course
Market Basket Analysis in Python
Exercise instructions
- Complete the
import
statement to import thepermutations
function. - Generate all association rules from the
groceries
list. - Print the set of rules.
- Print the number of rules.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import permutations from the itertools module
from itertools import ____
# Define the set of groceries
flattened = [i for t in transactions for i in t]
groceries = list(set(flattened))
# Generate all possible rules from groceries list
rules = list(permutations(____, 2))
# Print the set of rules
print(____)
# Print the number of rules
print(____(rules))