Market Basket Analysis in Python
Isaiah Hull
Visiting Associate Professor of Finance, BI Norwegian Business School
$$Support(X) = \frac{Frequency(X)}{N}$$
$$Support(X \rightarrow Y) = \frac{Frequency(X \& Y)}{N}$$
$$Confidence(X \rightarrow Y) = \frac{Support(X \rightarrow Y)} {Support(X)}$$
$$Lift(X \rightarrow Y) = \frac{Support(X \rightarrow Y)}{Support(X) Support(Y)}$$
$$Leverage(X \rightarrow Y) = $$ $$Support(X \& Y) - Support(X) Support(Y)$$
# Compute support for Twilight and Harry Potter
supportTP = np.logical_and(books['Twilight'], books['Potter']).mean()
# Compute support for Twilight
supportT = books['Twilight'].mean()
# Compute support for Harry Potter
supportP = books['Potter'].mean()
# Compute and print leverage
leverage = supportTP - supportP * supportT
print(leverage)
0.018
$$Conviction(X \rightarrow Y) = $$ $$\frac{Support(X) Support(\bar{Y})} {Support(X \& \bar{{Y}})}$$
# Compute support for Twilight and Harry Potter and Twilight
supportTP = np.logical_and(books['Twilight'], books['Potter']).mean()
supportT = books['Twilight'].mean()
# Compute support for NOT Harry Potter
supportnP = 1.0 - books['Potter'].mean()
# Compute support for Twilight and NOT Harry Potter
supportTnP = supportT - supportPT
# Compute conviction
conviction = supportT*supportnP / supportTnP
print(conviction)
1.16
Market Basket Analysis in Python