使用平行座標圖視覺化規則
你在前一題的視覺化展示,讓創辦人相信「支持度-信賴度邊界」值得進一步探索。她現在建議你擷取該邊界的一部分並將其視覺化。由於落在邊界上的規則在多數常見指標下都相當強,她主張你只要視覺化「規則是否存在」,而不是根據某個指標去看規則的強度。
你意識到平行座標圖非常適合這種情況。資料已匯入為 onehot。此外,apriori()、association_rules() 與 parallel_coordinates() 已經匯入,且 pandas 可用作 pd。函式 rules_to_coordinates() 也已定義並可使用。
本練習屬於課程
Python 的 Market Basket Analysis
練習說明
- 以最小支持度 0.05 完成 Apriori 演算法的敘述。
- 以最小信賴度門檻 0.50 計算關聯規則。這個門檻足夠高,只會擷取接近支持度-信賴度邊界上側的點。
- 將規則轉換為座標。
- 使用
parallel_coordinates()繪製這些座標。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Compute the frequent itemsets
frequent_itemsets = ____(onehot, min_support = ____,
use_colnames = True, max_len = 2)
# Compute rules from the frequent itemsets with the confidence metric
rules = association_rules(frequent_itemsets, metric = '____',
min_threshold = 0.50)
# Convert rules into coordinates suitable for use in a parallel coordinates plot
coords = rules_to_coordinates(____)
# Generate parallel coordinates plot
parallel_coordinates(____, 'rule')
plt.legend([])
plt.show()