套用 Zhang 指標
在第 2 章中,我們學到 Zhang 指標是一種連續型的關聯強度量測,取值範圍為 [-1,+1]。-1 代表完全負向關聯,而 +1 代表完全正向關聯。在這個練習中,你要判斷是否能用 Zhang 指標來優化某家禮品店目前用於促銷商品的規則集合。
注意:頻繁項目集已替你計算好,變數為 frequent_itemsets。此外,zhangs_rule() 已定義好,且 association_rules() 已從 mlxtend 匯入。你會先重新計算原始的規則集合,接著套用 Zhang 指標,只保留關聯強度高且為正向的規則。
本練習屬於課程
Python 的 Market Basket Analysis
練習說明
- 產生提升度(lift)至少為 1.00 的關聯規則集合。
- 將前件支持度門檻設為 0.005。
- 計算 Zhang 指標,並將輸出指派到
rules的zhang欄位。 - 選出 Zhang 指標大於 0.98 的規則。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Generate the initial set of rules using a minimum lift of 1.00
rules = association_rules(frequent_itemsets, metric = "____", min_threshold = ____)
# Set antecedent support to 0.005
rules = rules[rules['____'] > 0.005]
# Set consequent support to 0.005
rules = rules[rules['consequent support'] > 0.005]
# Compute Zhang's rule
rules['zhang'] = ____(____)
# Set the lower bound for Zhang's rule to 0.98
rules = rules[____['zhang'] > 0.98]
print(rules[['antecedents', 'consequents']])