使用多种度量的高级筛选
之前,我们使用一家在线创意礼品店的数据来寻找可用于推广特定后件的前件。由于潜在规则集合很大,我们需要依赖 Apriori 算法和多指标筛选来缩小范围。在本练习中,您将检查完整的规则集合并找到一个有用的规则,而不是针对某个特定前件。
请注意,数据已加载、预处理并做了独热编码,可通过 onehot 获取。另外,apriori() 和 association_rules() 已从 mlxtend 导入。在本练习中,您将应用 Apriori 算法以识别频繁项集。随后,您将从这些项集中恢复关联规则集合,并应用多指标筛选。
本练习是课程的一部分
Python 中的购物篮分析
练习说明
- 将 Apriori 算法应用于独热编码后的项集,最小支持度阈值设为 0.001。
- 使用最小支持度阈值 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']])