Identifying frequent itemsets with Apriori
The aggregation exercise you performed for the online retailer proved helpful. It offered a starting point for understanding which categories of items appear frequently in transactions. The retailer now wants to explore the individual items themselves to find out which are frequent.
In this exercise, you'll apply the Apriori algorithm to the online retail dataset without aggregating first. Your objective will be to prune the itemsets using a minimum value of support and a maximum item number threshold. Note that pandas
has been imported as pd
and the one-hot encoded data is available as onehot
.
This exercise is part of the course
Market Basket Analysis in Python
Exercise instructions
- Pass
onehot
to the Apriori algorithm. - Set the minimum support value to 0.006.
- Set the maximum itemset length to 3.
- Print a preview of the first five itemsets.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import apriori from mlxtend
from mlxtend.frequent_patterns import apriori
# Compute frequent itemsets using the Apriori algorithm
frequent_itemsets = apriori(____,
____ = ____,
max_len = ____,
use_colnames = True)
# Print a preview of the frequent itemsets
print(____.head())