Python ile Market Basket Analysis
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School

from mlxtend.frequent_patterns import association_rules, apriori
# One-hot kodlanmış veriyi yükle
onehot = pd.read_csv('datasets/movies_onehot.csv')
# Sık görülen öğe kümelerini üret
frequent_itemsets = apriori(onehot, min_support = 0.10, use_colnames = True, max_len = 2)
# Birliktelik kurallarını üret
rules = association_rules(frequent_itemsets, metric = 'support', min_threshold = 0.00)
# Kuralları koordinatlara dönüştür.
rules['antecedent'] = rules['antecedents'].apply(lambda antecedent: list(antecedent)[0])
rules['consequent'] = rules['consequents'].apply(lambda consequent: list(consequent)[0])
rules['rule'] = rules.index
# Koordinatları ve etiketi tanımla
coords = rules[['antecedent','consequent','rule']]
# Örnek yazdır
print(coords.head(1))
antecedent consequent rule
0 Dark Knight, The (2008) Inception (2010) 0
from pandas.plotting import parallel_coordinates
# Paralel koordinatlar grafiği oluştur
parallel_coordinates(coords, 'rule', colormap = 'ocean')

# Sık görülen öğe kümelerini üret
frequent_itemsets = apriori(onehot, min_support = 0.01, use_colnames = True, max_len = 2)
# Birliktelik kurallarını üret
rules = association_rules(frequent_itemsets, metric = 'lift', min_threshold = 1.00)
# Koordinatları üret ve örnek yazdır
coords = rules_to_coordinates(rules)
# Paralel koordinatlar grafiği oluştur
parallel_coordinates(coords, 'rule')

Python ile Market Basket Analysis