LoslegenKostenlos loslegen

Advanced filtering with multiple metrics

Earlier, we used data from an online novelty gift store to find antecedents that could be used to promote a targeted consequent. Since the set of potential rules was large, we had to rely on the Apriori algorithm and multi-metric filtering to narrow it down. In this exercise, we'll examine the full set of rules and find a useful one, rather than targeting a particular antecedent.

Note that the data has been loaded, preprocessed, and one-hot encoded, and is available as onehot. Additionally apriori() and association_rules() have been imported from mlxtend. In this exercise, you'll apply the Apriori algorithm to identify frequent itemsets. You'll then recover the set of association rules from the itemsets and apply multi-metric filtering.

Diese Übung ist Teil des Kurses

Market Basket Analysis in Python

Kurs anzeigen

Anleitung zur Übung

  • Apply the Apriori algorithm to the one-hot encoded itemsets with a minimum support threshold of 0.001.
  • Extract association rules using a minimum support threshold of 0.001.
  • Set the antecedent_support at 0.002 and consequent_support to 0.01.
  • Set confidence to be higher than 0.60 and lift to be higher than 2.50.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Apply the Apriori algorithm with a minimum support threshold of 0.001
frequent_itemsets = ____(onehot, min_support = ____, use_colnames = True)

# Recover association rules using a minium support threshold of 0.001
rules = ____(frequent_itemsets, metric = '____', min_threshold = 0.001)

# Apply a 0.002 antecedent support threshold, 0.60 confidence threshold, and 2.50 lift threshold
filtered_rules = rules[(rules['antecedent support'] > ____) &
						(____['consequent support'] > 0.01) &
						(rules['____'] > ____) &
						(____ > 2.50)]

# Print remaining rule
print(filtered_rules[['antecedents','consequents']])
Code bearbeiten und ausführen