关联规则分析

#关联规则
from apyori import apriori

store_data = pd.read_csv(r'D:/Download/apriori算法示例及数据/apriori算法示例及数据/store_data.csv', header=None).fillna('')
store_data.head()

#读取csv文件,将每一行按逗号分割存成一个List,然后整体存储成List,类似这种形式List[[],[],[]]
# records = []
# with open(r'D:/Download/apriori算法示例及数据/apriori算法示例及数据/store_data.csv', "r", newline='') as f:
#     import csv
#     reader = csv.reader(f)
#     for line in reader:
#         records.append(line)
 records1 = store_data.values.tolist()

#使用Apriori关联规则分析,设置最小支持度,置信度等
association_rules = apriori(records1, min_support=0.0045, min_confidence=0.2, min_lift=3, min_length=2)
#将结果转换成List
association_results = list(association_rules)
#打印首行,可以看看结果是怎样的
print(association_results[0])
# RelationRecord(items=frozenset({'light cream', 'chicken'}), support=0.004532728969470737, ordered_statistics[OrderedStatistic(items_base=frozenset({'light cream'}), items_add=frozenset({'chicken'}), confidence=0.29059829059829057, lift=4.84395061728395)])
#使用for循环,查看符合条件的关联规则,并打印出来
for item in association_results:

    # first index of the inner list
    # Contains base item and add item
    pair = item[0]
    items = [x for x in pair]
    print("Rule: " + items[0] + " -> " + items[1])

    #second index of the inner list
    print("Support: " + str(item[1]))

    #third index of the list located at 0th
    #of the third index of the inner list

    print("Confidence: " + str(item[2][0][2]))
    print("Lift: " + str(item[2][0][3]))
    print("=====================================")

 

ps: [ x for x in pair] 列表生成器,可以提取可迭代对象association_results[0]等号后面的内容

参考文档 :https://www.cnblogs.com/listenfwind/p/10280392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值