平行座標プロット

Python で学ぶマーケットバスケット分析

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

平行座標プロットとは?

このプロットはMovieLensデータセットを用いた平行座標プロットの例です。

Python で学ぶマーケットバスケット分析

平行座標プロットの使いどき

 

  • 平行座標 vs. ヒートマップ
    • 強度情報は不要
    • ルールの有無だけ知りたい
    • 視覚的な煩雑さを減らしたい

 

  • 平行座標 vs. 散布図
    • 個々のルール情報が欲しい
    • 複数指標には関心がない
    • 最終ルールのみ確認したい
Python で学ぶマーケットバスケット分析

データの準備

 

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 で学ぶマーケットバスケット分析

ルールを座標に変換する

# 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 で学ぶマーケットバスケット分析

平行座標プロットの作成

 

from pandas.plotting import parallel_coordinates

# Generate parallel coordinates plot
parallel_coordinates(coords, 'rule', colormap = 'ocean')
Python で学ぶマーケットバスケット分析

平行座標プロットの作成

図はMovieLensデータを用いた平行座標プロットの例です。

Python で学ぶマーケットバスケット分析

平行座標プロットの調整

# 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 で学ぶマーケットバスケット分析

平行座標プロットの調整

図はMovieLensデータを用いた平行座標プロットの例です。

Python で学ぶマーケットバスケット分析

練習しましょう!

Python で学ぶマーケットバスケット分析

Preparing Video For Download...