Market Basket Analysis in Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School
import pandas as pd
import seaborn as sns
from mlxtend.frequent_patterns import association_rules, apriori
# Load one-hot encoded MovieLens data
onehot = pd.read_csv('datasets/movies_onehot.csv')
# Generate frequent itemsets using Apriori
frequent_itemsets = apriori(onehot, min_support=0.01, use_colnames=True, max_len=2)
# Generate association rules
rules = association_rules(frequent_itemsets, metric='support', min_threshold=0.0)
sns.scatterplot(x="antecedent support", y="consequent support", data=rules)
sns.scatterplot(x="antecedent support",
y="consequent support",
size="lift",
data=rules)
Market Basket Analysis in Python