平行座標圖

Python 的 Market Basket Analysis

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

什麼是平行座標圖?

此圖示範使用 MovieLens 資料集的平行座標圖。

Python 的 Market Basket Analysis

何時使用平行座標圖

 

  • 平行座標 vs. 熱度圖。
    • 不需強度資訊。
    • 只想知道是否存在規則。
    • 想降低視覺雜訊。

 

  • 平行座標 vs. 散佈圖。
    • 想看單一規則的細節。
    • 不關心多重指標。
    • 只想檢視最終規則。
Python 的 Market Basket Analysis

準備資料

 

from mlxtend.frequent_patterns import association_rules, apriori

# Load the one-hot encoded data
onehot = pd.read_csv('datasets/movies_onehot.csv')
# Generate frequent itemsets
frequent_itemsets = apriori(onehot, min_support = 0.10, use_colnames = True, max_len = 2)

# Generate association rules
rules = association_rules(frequent_itemsets, metric = 'support', min_threshold = 0.00)
Python 的 Market Basket Analysis

將規則轉為座標

# Convert rules to coordinates.
rules['antecedent'] = rules['antecedents'].apply(lambda antecedent: list(antecedent)[0])
rules['consequent'] = rules['consequents'].apply(lambda consequent: list(consequent)[0])
rules['rule'] = rules.index
# Define coordinates and label
coords = rules[['antecedent','consequent','rule']]

# Print example
print(coords.head(1))
                antecedent        consequent  rule
0  Dark Knight, The (2008)  Inception (2010)     0
Python 的 Market Basket Analysis

繪製平行座標圖

 

from pandas.plotting import parallel_coordinates

# Generate parallel coordinates plot
parallel_coordinates(coords, 'rule', colormap = 'ocean')
Python 的 Market Basket Analysis

繪製平行座標圖

此圖顯示使用 MovieLens 資料的平行座標圖。

Python 的 Market Basket Analysis

優化平行座標圖

# Generate frequent itemsets
frequent_itemsets = apriori(onehot, min_support = 0.01, use_colnames = True, max_len = 2)

# Generate association rules
rules = association_rules(frequent_itemsets, metric = 'lift', min_threshold = 1.00)

# Generate coordinates and print example
coords = rules_to_coordinates(rules)

# Generate parallel coordinates plot
parallel_coordinates(coords, 'rule')
Python 的 Market Basket Analysis

優化平行座標圖

此圖顯示使用 MovieLens 資料的平行座標圖。

Python 的 Market Basket Analysis

一起來練習吧!

Python 的 Market Basket Analysis

Preparing Video For Download...