평행 좌표 플롯

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...