生活随笔
收集整理的這篇文章主要介紹了
【算法竞赛学习】数据分析达人赛2:产品关联分析
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
賽題背景
賽題以購(gòu)物籃分析為背景,要求選手對(duì)品牌的歷史訂單數(shù)據(jù),挖掘頻繁項(xiàng)集與關(guān)聯(lián)規(guī)則。通過(guò)這道賽題,鼓勵(lì)學(xué)習(xí)者利用訂單數(shù)據(jù),為企業(yè)提供銷(xiāo)售策略,產(chǎn)品關(guān)聯(lián)組合,為企業(yè)提升銷(xiāo)量的同時(shí),也為消費(fèi)者提供更適合的商品推薦。
賽題數(shù)據(jù)
數(shù)據(jù)源:order.csv,product.csv,customer.csv,date.csv ,分別為訂單表,產(chǎn)品表,客戶(hù)表,日期表,使用天池實(shí)驗(yàn)室打比賽即可直接在notebook中掛載數(shù)據(jù)源https://tianchi.aliyun.com/competition/entrance/531891/information
賽題任務(wù)
現(xiàn)在需要你使用關(guān)聯(lián)分析(比如Apriori算法) 挖掘訂單中的頻繁項(xiàng)集及關(guān)聯(lián)規(guī)則
說(shuō)明:
1)頻繁項(xiàng)集、關(guān)聯(lián)規(guī)則的計(jì)算會(huì)用到支持度、置信度、提升度等指標(biāo),
2)頻繁項(xiàng)集:即大于最小支持度的商品或商品組合
3)關(guān)聯(lián)規(guī)則:在頻繁項(xiàng)集中,滿足最小置信度,或最小提升度的推薦規(guī)則
(這里最小支持度、最小置信度或最小提升度,選手可以根據(jù)數(shù)據(jù)集的特點(diǎn)自己設(shè)定)
import pandas
as pd
import time
import matplotlib
.pyplot
as plt
from matplotlib
import font_manager
import matplotlib
matplotlib
.rcParams
['font.family'] = 'Microsoft YaHei'df_product
= pd
.read_csv
("./product.csv", encoding
='gbk')
df_date
= pd
.read_csv
("./date.csv", encoding
='gbk')
df_customer
= pd
.read_csv
('./customer.csv', encoding
='gbk')
df_order
= pd
.read_csv
('./order.csv', encoding
='gbk')df_order
['訂單日期'] = pd
.to_datetime
(df_order
['訂單日期'])
df_order
df_order
= df_order
.groupby
(['客戶(hù)ID', '訂單日期'])['產(chǎn)品名稱(chēng)'].unique
()
df_order
transactions
= []
for value
in df_order
:transactions
.append
(list(value
))
from efficient_apriori
import aprioristart
= time
.time
()
Itemsets
, rules
= apriori
(transactions
, min_support
=0.03, min_confidence
=0.05)
print('頻繁項(xiàng)集:', Itemsets
)
print('關(guān)聯(lián)規(guī)則:', rules
)
end
= time
.time
()
print("用時(shí):", end
- start
)
Itemsets_pro
= []
Itemsets_num
= []
for key
in Itemsets
.keys
():df1
= Itemsets
[key
]for key
in df1
:Itemsets_pro
.append
(key
)Itemsets_num
.append
(df1
[key
])
Itemsets_pro_str
= []
for i
in Itemsets_pro
:Itemsets_pro_str
.append
(','.join
(list(i
)))
plt
.figure
(figsize
=(12,9),dpi
=100)
plt
.bar
(Itemsets_pro_str
, Itemsets_num
)
plt
.xlabel
('頻繁項(xiàng)集_產(chǎn)品名稱(chēng)', fontsize
=10)
plt
.ylabel
('頻繁項(xiàng)集_出現(xiàn)頻數(shù)', fontsize
=10)
plt
.title
('頻繁項(xiàng)集頻數(shù)分布柱狀圖', fontsize
=16)
plt
.xticks
(rotation
=90, fontsize
=10)
for a
, b
in zip(Itemsets_pro_str
, Itemsets_num
): plt
.text
(a
, b
+ 0.005, str(b
), ha
='center', va
='bottom', fontsize
=7)
plt
.show
()
總結(jié)
以上是生活随笔為你收集整理的【算法竞赛学习】数据分析达人赛2:产品关联分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。