天池学习赛——基于Apriori算法的商品频繁项集与关联规则的挖掘

赛题背景

赛题以购物篮分析为背景,要求选手对品牌的历史订单数据,挖掘频繁项集与关联规则。通过这道赛题,鼓励学习者利用订单数据,为企业提供销售策略,产品关联组合,为企业提升销量的同时,也为消费者提供更适合的商品推荐。

赛题数据

数据源:order.csv,product.csv,customer.csv,date.csv ,分别为订单表,产品表,客户表,日期表

赛题任务

现在需要你使用关联分析(比如Apriori算法) 挖掘订单中的频繁项集及关联规则
说明:
1)频繁项集、关联规则的计算会用到支持度、置信度、提升度等指标,
2)频繁项集:即大于最小支持度的商品或商品组合
3)关联规则:在频繁项集中,满足最小置信度,或最小提升度的推荐规则

解答

from efficient_apriori import apriori
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df_order = pd.read_csv("../download/order.csv", encoding='gbk')
df_customer = pd.read_csv("../download/customer.csv", encoding='gbk')
df_date = pd.read_csv("../download/date.csv", encoding='gbk')
df_product = pd.read_csv("../download/product.csv", encoding='gbk')
df_order['订单日期']=pd.to_datetime(df_order['订单日期'])
print(df_order.head())

在这里插入图片描述

df_order = df_order.groupby(['客户ID'])['产品名称'].unique()
print(df_order.head())
# 将所有交易订单追加到列表
transactions = []
for value in df_order:
    transactions.append(list(value))
import time
# 挖掘频繁项集和频繁规则
start = time.time()
itemsets, rules = apriori(transactions, min_support=0.03,  min_confidence=0.3)
print("频繁项集:", itemsets)
print("关联规则:", rules)
end = time.time()
print("用时:",end-start)

在这里插入图片描述

import operator
#分别根据置信度、支持度、提升度对规则进行排序
confidence = dict()
support = dict()
lift = dict()
for rule in rules:
    confidence[rule] = rule.confidence
    support[rule] = rule.support    
    lift[(rule)] = rule.lift    
    #rule.lhs,rule.rhs可用作提取规则的前件、后件
rules_sortbycon = sorted(confidence.items(),key=operator.itemgetter(1),reverse = True)
rules_sortbysup = sorted(support.items(),key=operator.itemgetter(1),reverse = True)
rules_sortbylift = sorted(lift.items(),key=operator.itemgetter(1),reverse = True)

根据支持度排序
在这里插入图片描述
根据置信度排序
在这里插入图片描述
根据提升度排序
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hyk今天写算法了吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值