การกรองขั้นสูงด้วยหลายเมตริก
ก่อนหน้านี้ เราใช้ข้อมูลจากร้านของขวัญออนไลน์เพื่อค้นหา antecedents ที่จะช่วยโปรโมต consequent ที่ต้องการ เนื่องจากกฎที่เป็นไปได้มีจำนวนมาก จึงต้องพึ่งอัลกอริทึม Apriori และการกรองแบบหลายเมตริกเพื่อคัดกรองให้เหลือเฉพาะกฎที่น่าสนใจ ในแบบฝึกหัดนี้ เราจะตรวจสอบกฎทั้งหมดและค้นหากฎที่มีประโยชน์ โดยไม่ได้มุ่งเป้าไปที่ antecedent ใดเป็นพิเศษ
ข้อมูลได้ถูกโหลด ประมวลผล และแปลงเป็น one-hot encoding แล้ว และพร้อมใช้งานในชื่อ onehot นอกจากนี้ยังได้ import apriori() และ association_rules() จาก mlxtend แล้ว ในแบบฝึกหัดนี้ ให้ใช้อัลกอริทึม Apriori เพื่อระบุ frequent itemsets จากนั้นดึงชุดของ association rules ออกมา และนำการกรองแบบหลายเมตริกไปใช้
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Market Basket Analysis ด้วย Python
คำแนะนำการฝึกหัด
- ใช้อัลกอริทึม Apriori กับ itemsets ที่ผ่าน one-hot encoding โดยกำหนด minimum support threshold ที่ 0.001
- ดึง association rules โดยกำหนด minimum support threshold ที่ 0.001
- กำหนด
antecedent_supportที่ 0.002 และconsequent_supportที่ 0.01 - กำหนดให้
confidenceมากกว่า 0.60 และliftมากกว่า 2.50
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Apply the Apriori algorithm with a minimum support threshold of 0.001
frequent_itemsets = ____(onehot, min_support = ____, use_colnames = True)
# Recover association rules using a minium support threshold of 0.001
rules = ____(frequent_itemsets, metric = '____', min_threshold = 0.001)
# Apply a 0.002 antecedent support threshold, 0.60 confidence threshold, and 2.50 lift threshold
filtered_rules = rules[(rules['antecedent support'] > ____) &
(____['consequent support'] > 0.01) &
(rules['____'] > ____) &
(____ > 2.50)]
# Print remaining rule
print(filtered_rules[['antecedents','consequents']])