Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Market Basket Analysis in Python

Cursus bekijken

Oefeninstructies

  • Complete the import statement to import the permutations function.
  • Generate all association rules from the groceries list.
  • Print the set of rules.
  • Print the number of rules.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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))
Code bewerken en uitvoeren