開始使用免費開始

計算關聯與反關聯

圖書館又回來詢問你先前建議:用《Twilight(暮光之城)》帶動《Harry Potter(哈利波特)》的推廣。他們擔心兩者可能是反關聯,會對行銷造成負面影響,並請你確認是否不是這種情況。

你立刻想到 Zhang 指標,它能連續量化關聯與反關聯。關聯為正值,反關聯為負值。和前面練習一樣,已為你匯入 DataFrame books,以及以別名 np 匯入的 numpy。Zhang 指標的計算如下:

$$Zhang(A \rightarrow B) = $$ $$\frac{Support(A \& B) - Support(A) Support(B)}{ max[Support(AB) (1-Support(A)), Support(A)(Support(B)-Support(AB))]}$$

本練習屬於課程

Python 的 Market Basket Analysis

檢視課程

練習說明

  • 計算 {Twilight} 的支持度,以及 {Potter} 的支持度。
  • 計算 {Twilight, Potter} 的支持度。
  • 完成分母的表達式。
  • 計算 {Twilight} \(\rightarrow\) {Potter} 的 Zhang 指標。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Compute the support of Twilight and Harry Potter
supportT = books['Twilight'].____
supportP = books['Potter'].____

# Compute the support of both books
supportTP = ____.mean()

# Complete the expressions for the numerator and denominator
numerator = supportTP - supportT*supportP
denominator = ___(supportTP*(1-supportT), supportT*(supportP-supportTP))

# Compute and print Zhang's metric
zhang = ____ / ____
print(zhang)
編輯並執行程式碼