赛题背景
赛题以购物篮分析为背景,要求选手对品牌的历史订单数据,挖掘频繁项集与关联规则。通过这道赛题,鼓励学习者利用订单数据,为企业提供销售策略,产品关联组合,为企业提升销量的同时,也为消费者提供更适合的商品推荐。
赛题数据
数据源:order.csv,product.csv,customer.csv,date.csv ,分别为订单表,产品表,客户表,日期表
订单表
订单日期 | 年份 | 订单数量 | 产品ID | 客户ID | 交易类型 | 销售区域ID | 销售大区 | 国家 | 区域 | 产品类别 | 产品型号名称 | 产品名称 | 产品成本 | 利润 | 单价 | 销售金额 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2016/1/1 | 2016 | 1 | 528 | 14432BA | 1 | 4 | 西南区 | 中国 | 大中华区 | 配件 | Rawlings Heart of THE Hide-11.5 | 棒球手套 | 500 | 1199 | 1699 | 1699 |
2016/1/2 | 2016 | 1 | 528 | 18741BA | 1 | 4 | 西南区 | 中国 | 大中华区 | 配件 | Rawlings Heart of THE Hide-11.5 | 棒球手套 | 500 | 1199 | 1699 | 1699 |
2016/1/2 | 2016 | 1 | 528 | 27988BA | 1 | 4 | 西南区 | 中国 | 大中华区 | 配件 | Rawlings Heart of THE Hide-11.5 | 棒球手套 | 500 | 1199 | 1699 | 1699 |
2016/1/5 | 2016 | 1 | 528 | 25710BA | 1 | 4 | 西南区 | 中国 | 大中华区 | 配件 | Rawlings Heart of THE Hide-11.5 | 棒球手套 | 500 | 1199 | 1699 | 1699 |
…… | …… | …… | …… | …… | …… | …… | …… | …… | …… | …… | …… | …… | …… | …… | …… | …… |
2016/7/30 | 2016 | 1 | 528 | 15444BA | 1 | 6 | 韩国 | 韩国 | 韩国 | 配件 | Rawlings Heart of THE Hide-11.5 | 棒球手套 | 500 | 1199 | 1699 | 1699 |
2016/7/30 | 2016 | 1 | 528 | 15196BA | 1 | 6 | 韩国 | 韩国 | 韩国 | 配件 | Rawlings Heart of THE Hide-11.5 | 棒球手套 | 500 | 1199 | 1699 | 1699 |
产品表
产品类别 | 产品ID | 产品型号 | 产品名称 |
---|---|---|---|
配件 | 528 | Rawlings Heart of THE Hide-11.5 | 棒球手套 |
配件 | 480 | Rawlings Gold Glove-11.5 | 棒球手套 |
配件 | 537 | Mizuno MVP-12 | 棒球手套 |
配件 | 529 | Wilson-A2000-12.5 | 棒球手套 |
…… | …… | …… | …… |
配件 | 477 | Bat Pack | 球棒与球棒袋 |
球 | 322 | BA-150 | 硬式棒球 |
客户表
客户ID |
---|
30410BA |
23789BA |
27884BA |
16522BA |
…… |
15692BA |
20550BA |
日期表
日期 | 年度 | 季度 | 月份 | 日 | 年度季度 | 年度月份 | 星期几 |
---|---|---|---|---|---|---|---|
2013/7/1 | 2013 | Q3 | 7 | 1 | 2013Q3 | 201307 | 1 |
…… | …… | …… | …… | …… | …… | …… | …… |
2016/1/31 | 2016 | Q1 | 1 | 31 | 2016Q1 | 201601 | 7 |
赛题任务
现在需要你使用关联分析(比如Apriori算法) 挖掘订单中的频繁项集及关联规则
说明:
1)频繁项集、关联规则的计算会用到支持度、置信度、提升度等指标,
2)频繁项集:即大于最小支持度的商品或商品组合
3)关联规则:在频繁项集中,满足最小置信度,或最小提升度的推荐规则
(这里最小支持度、最小置信度或最小提升度,选手可以根据数据集的特点自己设定)
代码展示
#!/usr/bin/python
# -*- coding:utf-8 -*-
"""
To Do: 使用关联分析(比如Apriori算法)挖掘订单中的频繁项集及关联规则
"""
import pandas as pd
from efficient_apriori import apriori
import time
import matplotlib.pyplot as plt
import matplotlib
from matplotlib import font_manager
font = font_manager.FontProperties(fname=r"/Users/weiyi/anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/simhei.ttf", size=14)
# font_manager.fontManager.addfont('./SimHei.ttf')
# plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置汉字字体,优先使用黑体
plt.rcParams['font.size'] = 12 # 设置字体大小
plt.rcParams['axes.unicode_minus'] = False # 设置正常显示负号
# 1.数据加载
df_product = pd.read_csv("./data/product.csv", encoding='gbk')
df_date = pd.read_csv("./data/date.csv", encoding='gbk')
df_customer = pd.read_csv('./data/customer.csv', encoding='gbk')
df_order = pd.read_csv('./data/order.csv', encoding='gbk')
# 2.数据探索
# print(df_product.info) # [产品类别 产品ID 产品型号 产品名称]
# print(df_date.info) # [日期 年度 季度 月份 日 年度季度 年度月份 星期几]
# print(df_customer.info) # [客户ID]
# print(df_order.info) # [60398 rows x 17 columns]
# ['订单日期', '年份', '订单数量', '产品ID', '客户ID', '交易类型', '销售区域ID',
# '销售大区', '国家','区域', '产品类别', '产品型号名称', '产品名称', '产品成本', '利润', '单价', '销售金额']
# 将原表中'订单日期'字段转化为pandas日期格式
df_order['订单日期']=pd.to_datetime(df_order['订单日期'])
# 统计当天同一客户的订单中购买的所有产品
df_order = df_order.groupby(['订单日期','客户ID'])['产品名称'].unique()
# 将所有交易订单追加到列表
transactions = []
for value in df_order:
transactions.append(list(value))
# 挖掘频繁项集和频繁规则
start = time.time()
itemsets, rules = apriori(transactions, min_support=0.03, min_confidence=0.1)
print("频繁项集:", itemsets)
print("关联规则:", rules)
end = time.time()
print("用时:",end-start)
# 绘制频繁项集的条形图。 横坐标为频繁项集,纵坐标为相应的频数
itemsets_products = []
itemsets_nums = []
for key in itemsets.keys():
df1 = itemsets[key]
for key in df1:
itemsets_products.append(key) # [('软式棒球',), ('硬式棒球',),...]
itemsets_nums.append(df1[key]) # [4970, 8068, ...]
# print(itemsets_products)
# print(itemsets_nums)
# 将横坐标转化为字符串形式
itemsets_products_str = [] # ['软式棒球', '硬式棒球',...]
for i in itemsets_products:
itemsets_products_str.append(','.join(list(i)))
# 频繁项、频数分布柱状图
plt.bar(itemsets_products_str, itemsets_nums)
plt.xlabel('频繁项集_产品名称', fontsize=14)
plt.ylabel('频繁项集_出现频数', fontsize=14)
plt.title('频繁项频数分布柱状图', fontsize=18)
plt.xticks(rotation=90, fontsize=10) # 设置X轴标签竖排显示
# 设置数据标签可见
for a, b in zip(itemsets_products_str, itemsets_nums):
plt.text(a, b+0.005, str(b), ha='center', va='bottom', fontsize=8)
plt.show()