import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
df = pd.read_csv(r'C:\Users\zcy\Desktop\淘宝用户分析\tianchi_mobile_recommend_train_user.csv')
df.head()
user_id | item_id | behavior_type | user_geohash | item_category | time | |
---|---|---|---|---|---|---|
0 | 98047837 | 232431562 | 1 | NaN | 4245 | 2014-12-06 02 |
1 | 97726136 | 383583590 | 1 | NaN | 5894 | 2014-12-09 20 |
2 | 98607707 | 64749712 | 1 | NaN | 2883 | 2014-12-18 11 |
3 | 98662432 | 320593836 | 1 | 96nn52n | 6562 | 2014-12-06 10 |
4 | 98145908 | 290208520 | 1 | NaN | 13926 | 2014-12-16 21 |
#对时间特征处理,添加每日时间段,星期特征
df.time = pd.to_datetime(df['time'])
df['daily'] = df['time'].dt.hour
df['weekday'] = df['time'].dt.weekday
df['date'] = df['time'].dt.date
df.head()
user_id | item_id | behavior_type | user_geohash | item_category | time | daily | weekday | date | |
---|---|---|---|---|---|---|---|---|---|
0 | 98047837 | 232431562 | 1 | NaN | 4245 | 2014-12-06 02:00:00 | 2 | 5 | 2014-12-06 |
1 | 97726136 | 383583590 | 1 | NaN | 5894 | 2014-12-09 20:00:00 | 20 | 1 | 2014-12-09 |
2 | 98607707 | 64749712 | 1 | NaN | 2883 | 2014-12-18 11:00:00 | 11 | 3 | 2014-12-18 |
3 | 98662432 | 320593836 | 1 | 96nn52n | 6562 | 2014-12-06 10:00:00 | 10 | 5 | 2014-12-06 |
4 | 98145908 | 290208520 | 1 | NaN | 13926 | 2014-12-16 21:00:00 | 21 | 1 | 2014-12-16 |
df.drop(columns='user_geohash',axis=1,inplace=True)
behavior = {1:'pv',2:'collect',3:'cart',4:'buy'}
df['behavior_type'] = df['behavior_type'].replace(behavior)
df.head()
user_id | item_id | behavior_type | item_category | time | daily | weekday | date | |
---|---|---|---|---|---|---|---|---|
0 | 98047837 | 232431562 | pv | 4245 | 2014-12-06 02:00:00 | 2 | 5 | 2014-12-06 |
1 | 97726136 | 383583590 | pv | 5894 | 2014-12-09 20:00:00 | 20 | 1 | 2014-12-09 |
2 | 98607707 | 64749712 | pv | 2883 | 2014-12-18 11:00:00 | 11 | 3 | 2014-12-18 |
3 | 98662432 | 320593836 | pv | 6562 | 2014-12-06 10:00:00 | 10 | 5 | 2014-12-06 |
4 | 98145908 | 290208520 | pv | 13926 | 2014-12-16 21:00:00 | 21 | 1 | 2014-12-16 |
数据分析
1.时间维度上的整体分析
2.用户行为路径分析(用户消费行为)
3.留存率与活跃度分析
4.用户分类分析
1.时间维度上的整体分析
b = df.groupby(['daily','behavior_type'])['user_id'].agg('count').reset_index().rename(columns={'user_id':'num'})
b
daily | behavior_type | num | |
---|---|---|---|
0 | 0 | buy | 4845 |
1 | 0 | cart | 14156 |
2 | 0 | collect | 11062 |
3 | 0 | pv | 487341 |
4 | 1 | buy | 1703 |
... | ... | ... | ... |
91 | 22 | pv | 1027269 |
92 | 23 | buy | 6359 |
93 | 23 | cart | 27434 |
94 | 23 | collect | 17705 |
95 | 23 | pv | 797754 |
96 rows × 3 columns
table_time = df.groupby(['daily','behavior_type'])['user_id'].agg('count').unstack()
table_time
behavior_type | buy | cart | collect | pv |
---|---|---|---|---|
daily | ||||
0 | 4845 | 14156 | 11062 | 487341 |
1 | 1703 | 6712 | 6276 | 252991 |
2 | 806 | 3834 | 3311 | 139139 |
3 | 504 | 2480 | 2282 | 93250 |
4 | 397 | 2248 | 2010 | 75832 |
5 | 476 | 2213 | 2062 | 83545 |
6 | 1023 | 3768 | 3651 | 150356 |
7 | 1938 | 7044 | 5885 | 272470 |
8 | 3586 | 9970 | 7849 | 374701 |
9 | 5707 | 12956 | 10507 | 456781 |
10 | 7317 | 16203 | 11185 | 515960 |
11 | 7086 | 15257 | 10918 | 493679 |
12 | 6956 | 15025 | 9940 | 500036 |
13 | 7717 | 17419 | 11694 | 561513 |
14 | 7207 | 17067 | 11695 | 558246 |
15 | 7312 | 17289 | 12010 | 562238 |
16 | 6930 | 16304 | 11127 | 541846 |
17 | 5298 | 14515 | 9754 | 476369 |
18 | 5140 | 14823 | 10342 | 517078 |
19 | 6352 | 18853 | 13952 | 696035 |
20 | 7872 | 25021 | 16599 | 885669 |
21 | 8829 | 30469 | 20397 | 1030483 |
22 | 8845 | 32504 | 20343 | 1027269 |
23 | 6359 | 27434 | 17705 | 797754 |
table_time.columns
Index(['buy', 'cart', 'collect', 'pv'], dtype='object', name='behavior_type')
table_time.plot.bar(figsize=(10,5))
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['savefig.dpi'] = 300 # 图片像素
plt.rcParams['figure.dpi'] = 300 # 分辨率
plt.rcParams['figure.figsize'] = (15.0, 8.0) # 尺寸
from pyecharts import options as opts
from pyecharts.charts import Bar
c = (
Bar()
.add_xaxis(list(table_time.index)
)
.add_yaxis("buy", list(table_time.buy))
.add_yaxis("cart", list(table_time.cart))
.add_yaxis("collect", list(table_time.collect))
.add_yaxis("pv", list(table_time.pv))
.set_global_opts(
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
title_opts=opts.TitleOpts(title="24小时的用户行为"),
)
.render("bar_daily.html")
)
table_time.plot.bar(y=['buy', 'cart', 'collect'],figsize=(10,5))
随着一天从6点开始,人们逐渐开始逛淘宝,而且白天用户行为相对稳定,到了晚上5-6点用户行为激增并且一直持续到晚上十一点之前,表明在晚上是用户发生用户行为的高峰期。但是点击、收藏、加入购物车的数量都在晚上存在明显的提升,相对而言在购买上晚上的提升没有其他用户行为显著。
推测用户应该是在晚上有充足的时间进行浏览,并不会立刻下单购买,直到白天经过一晚上思索产生了购买行为,又或者是企业方面的购买需求在白天工作期间我完成
table_date = df.groupby(['date','behavior_type'])['user_id'].agg('count').unstack()
table_date
behavior_type | buy | cart | collect | pv |
---|---|---|---|---|
date | ||||
2014-11-18 | 3730 | 10212 | 6904 | 345855 |
2014-11-19 | 3686 | 10115 | 7152 | 337870 |
2014-11-20 | 3462 | 10008 | 7167 | 332792 |
2014-11-21 | 3021 | 8679 | 6832 | 314572 |
2014-11-22 | 3570 | 9970 | 7252 | 340563 |
2014-11-23 | 3347 | 10432 | 7702 | 361221 |
2014-11-24 | 3426 | 10391 | 7333 | 357192 |
2014-11-25 | 3464 | 9891 | 7492 | 349392 |
2014-11-26 | 3573 | 9378 | 7324 | 340621 |
2014-11-27 | 3670 | 9975 | 7699 | 350040 |
2014-11-28 | 3218 | 9123 | 6484 | 321813 |
2014-11-29 | 3211 | 10145 | 7214 | 344127 |
2014-11-30 | 3616 | 10779 | 7786 | 379439 |
2014-12-01 | 3909 | 11288 | 7319 | 372095 |
2014-12-02 | 3621 | 11521 | 8022 | 382052 |
2014-12-03 | 3885 | 11732 | 8492 | 387497 |
2014-12-04 | 3691 | 11395 | 8559 | 376307 |
2014-12-05 | 3319 | 10109 | 7474 | 340976 |
2014-12-06 | 3272 | 10889 | 8323 | 367126 |
2014-12-07 | 3256 | 11267 | 8632 | 376596 |
2014-12-08 | 3419 | 10915 | 8236 | 364097 |
2014-12-09 | 3449 | 11763 | 8552 | 374261 |
2014-12-10 | 3216 | 12280 | 8753 | 397661 |
2014-12-11 | 3226 | 15643 | 9310 | 460329 |
2014-12-12 | 15251 | 24508 | 10446 | 641507 |
2014-12-13 | 3478 | 10486 | 7859 | 385337 |
2014-12-14 | 3483 | 10213 | 8128 | 380717 |
2014-12-15 | 3764 | 10210 | 7758 | 376624 |
2014-12-16 | 3771 | 10166 | 7749 | 373399 |
2014-12-17 | 3615 | 10256 | 7163 | 363757 |
2014-12-18 | 3586 | 9825 | 7440 | 354746 |
table_date.plot(rot=45,figsize=(10,5))
table_date.plot(y=['buy', 'cart', 'collect'],figsize=(10,5),rot=45)
d = df[df.date.astype('str')=='2014-12-12'].groupby(['daily','behavior_type'])['user_id'].agg('count').unstack()
d.plot.bar(figsize=(10,5),rot=0)
d.plot.bar(y=['buy', 'cart', 'collect'],figsize=(10,5),rot=0)
plt.xlabel('双十二')
可以明显的看出,在双十二前三天,用户点击以及加入购物车的行为已经开始升高,说明对双十二的各种宣传已经开始,但在当时的背景下,人们对网购的热情远没有现在这样狂热,而且当时促销手段也没有像现在这样各种预售、游戏、抢优惠券等模式多样化,因此购买行为全部在双十二当天完成。
在当时的背景下,应该着重关注活动期间的数据变化,针对整个活动,从初期预热的点击到加购物车转化率,到双十二当天的购买率等,为运营提供建议
table_week = df.groupby(['weekday','behavior_type'])['user_id'].agg('count').unstack()
table_week.index = table_week.index +1
table_week
behavior_type | buy | cart | collect | pv |
---|---|---|---|---|
weekday | ||||
1 | 14518 | 42804 | 30646 | 1470008 |
2 | 18035 | 53553 | 38719 | 1824959 |
3 | 17975 | 53761 | 38884 | 1827406 |
4 | 17635 | 56846 | 40175 | 1874214 |
5 | 24809 | 52419 | 31236 | 1618868 |
6 | 13531 | 41490 | 30648 | 1437153 |
7 | 13702 | 42691 | 32248 | 1497973 |
table_week.plot.bar(figsize=(10,5),rot=0)
#plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
#plt.rcParams['savefig.dpi'] = 300 # 图片像素
#plt.rcParams['figure.dpi'] = 300 # 分辨率
#plt.rcParams['figure.figsize'] = (15.0, 8.0) # 尺寸
table_week.plot.bar(y=['buy', 'cart', 'collect'],figsize=(10,5),rot=0)
从图中得到用户行为在工作日比休息日要高,推测可能是因为当时背景下,人们的购物行为还是以逛街去实体店为主,而在工作日则多以收藏加购物车为主,周末则去实体店进行购买。为了扭转这种局面,应该加大推广力度,打消人们对网上购物的不信任感。而周五的购买行为升高和双十二为周五有关,并不具备代表性。
从时间维度,我们能得到以下几点:
1.人们在晚上7点到11点空闲时间多为浏览收藏加购物的行为,应加强推送力度以及精确度
2.双十二活动明显改变用户行为,用户在活动开始前浏览收藏加入购物车行为猛增,而活动当天购买行为提升
3.工作日用户行为比休息日要高,这可能和当时背景有关
2.用户行为路径分析(用户消费行为)
table_time = df.groupby(['daily','behavior_type'])['user_id'].agg('count').unstack()
table_time
behavior_type | buy | cart | collect | pv |
---|---|---|---|---|
daily | ||||
0 | 4845 | 14156 | 11062 | 487341 |
1 | 1703 | 6712 | 6276 | 252991 |
2 | 806 | 3834 | 3311 | 139139 |
3 | 504 | 2480 | 2282 | 93250 |
4 | 397 | 2248 | 2010 | 75832 |
5 | 476 | 2213 | 2062 | 83545 |
6 | 1023 | 3768 | 3651 | 150356 |
7 | 1938 | 7044 | 5885 | 272470 |
8 | 3586 | 9970 | 7849 | 374701 |
9 | 5707 | 12956 | 10507 | 456781 |
10 | 7317 | 16203 | 11185 | 515960 |
11 | 7086 | 15257 | 10918 | 493679 |
12 | 6956 | 15025 | 9940 | 500036 |
13 | 7717 | 17419 | 11694 | 561513 |
14 | 7207 | 17067 | 11695 | 558246 |
15 | 7312 | 17289 | 12010 | 562238 |
16 | 6930 | 16304 | 11127 | 541846 |
17 | 5298 | 14515 | 9754 | 476369 |
18 | 5140 | 14823 | 10342 | 517078 |
19 | 6352 | 18853 | 13952 | 696035 |
20 | 7872 | 25021 | 16599 | 885669 |
21 | 8829 | 30469 | 20397 | 1030483 |
22 | 8845 | 32504 | 20343 | 1027269 |
23 | 6359 | 27434 | 17705 | 797754 |
trans = []
for i in range(len(table_time.index)):
pv2collect = table_time.collect[i]/table_time.pv[i]
pv2cart = table_time.cart[i]/table_time.pv[i]
pv2tobuy = (table_time.collect[i]+table_time.cart[i])/table_time.pv[i]
pv2buy = table_time.buy[i]/table_time.pv[i]
tobuy2buy = table_time.buy[i]/(table_time.collect[i]+table_time.cart[i])
collect2buy = table_time.buy[i]/table_time.collect[i]
cart2buy = table_time.buy[i]/table_time.cart[i]
tran = [pv2collect,pv2cart,pv2tobuy,pv2buy,tobuy2buy,collect2buy,cart2buy]
trans.append(tran)
data = pd.DataFrame(data=trans,index=table_time.index,columns=['pv2collect','pv2cart','pv2tobuy','pv2buy','tobuy2buy','collect2buy','cart2buy'])
data
pv2collect | pv2cart | pv2tobuy | pv2buy | tobuy2buy | collect2buy | cart2buy | |
---|---|---|---|---|---|---|---|
daily | |||||||
0 | 0.022699 | 0.029047 | 0.051746 | 0.009942 | 0.192125 | 0.437986 | 0.342258 |
1 | 0.024807 | 0.026531 | 0.051338 | 0.006731 | 0.131121 | 0.271351 | 0.253725 |
2 | 0.023796 | 0.027555 | 0.051352 | 0.005793 | 0.112806 | 0.243431 | 0.210224 |
3 | 0.024472 | 0.026595 | 0.051067 | 0.005405 | 0.105838 | 0.220859 | 0.203226 |
4 | 0.026506 | 0.029644 | 0.056150 | 0.005235 | 0.093236 | 0.197512 | 0.176601 |
5 | 0.024681 | 0.026489 | 0.051170 | 0.005698 | 0.111345 | 0.230844 | 0.215093 |
6 | 0.024282 | 0.025061 | 0.049343 | 0.006804 | 0.137889 | 0.280197 | 0.271497 |
7 | 0.021599 | 0.025852 | 0.047451 | 0.007113 | 0.149896 | 0.329312 | 0.275128 |
8 | 0.020947 | 0.026608 | 0.047555 | 0.009570 | 0.201246 | 0.456873 | 0.359679 |
9 | 0.023002 | 0.028364 | 0.051366 | 0.012494 | 0.243234 | 0.543162 | 0.440491 |
10 | 0.021678 | 0.031404 | 0.053082 | 0.014181 | 0.267161 | 0.654180 | 0.451583 |
11 | 0.022116 | 0.030905 | 0.053020 | 0.014353 | 0.270716 | 0.649020 | 0.464443 |
12 | 0.019879 | 0.030048 | 0.049926 | 0.013911 | 0.278630 | 0.699799 | 0.462962 |
13 | 0.020826 | 0.031022 | 0.051847 | 0.013743 | 0.265071 | 0.659911 | 0.443022 |
14 | 0.020950 | 0.030573 | 0.051522 | 0.012910 | 0.250574 | 0.616246 | 0.422277 |
15 | 0.021361 | 0.030750 | 0.052111 | 0.013005 | 0.249565 | 0.608826 | 0.422928 |
16 | 0.020535 | 0.030090 | 0.050625 | 0.012790 | 0.252634 | 0.622809 | 0.425049 |
17 | 0.020476 | 0.030470 | 0.050946 | 0.011122 | 0.218303 | 0.543162 | 0.365002 |
18 | 0.020001 | 0.028667 | 0.048668 | 0.009940 | 0.204252 | 0.497003 | 0.346758 |
19 | 0.020045 | 0.027086 | 0.047131 | 0.009126 | 0.193629 | 0.455275 | 0.336923 |
20 | 0.018742 | 0.028251 | 0.046993 | 0.008888 | 0.189140 | 0.474245 | 0.314616 |
21 | 0.019794 | 0.029568 | 0.049361 | 0.008568 | 0.173574 | 0.432858 | 0.289770 |
22 | 0.019803 | 0.031641 | 0.051444 | 0.008610 | 0.167370 | 0.434793 | 0.272120 |
23 | 0.022194 | 0.034389 | 0.056583 | 0.007971 | 0.140876 | 0.359164 | 0.231793 |
data.plot()
data.plot(y=['pv2tobuy','tobuy2buy'],xticks=data.index.values)
加收藏和加购物车没有明显的逻辑关系,将加收藏和加购物车当做一类行为,即有购买倾向行为。
从图中可以看出点击行为到购买倾向行为和一天中时间没有太大关系,而购买倾向行为到购买行为,从早上7点开始逐渐增多,直到12点到达顶峰,然后开始下降直到晚上,在白天工作期间一方面可能是企业需求进行购买,应该关注如何快速准确满足企业需求;另一方面可能是晚上经过思考到了白天产生购买行为,应该加大收藏或加入购物车商品的营销,如给与优惠券、提醒优惠时间期限、提醒降价等等
data.plot(y=['pv2collect','pv2cart','pv2tobuy','pv2buy'],xticks=data.index.values)
从图中可以看出,购买行为如之前分析大都发生在白天,而从浏览行为到购买倾向行为的转化,都在晚上19点之后有明显的提升,这样一方面需要加大商品推送力度,另一方面需要加大对用户购买行为的挖掘、进行个性化推荐,从而增强用户粘性和活跃度
data.plot(y=['tobuy2buy','collect2buy','cart2buy'],xticks=data.index.values)
trans1 = []
for i in range(len(table_date.index)):
pv2collect = table_date.collect[i]/table_date.pv[i]
pv2cart = table_date.cart[i]/table_date.pv[i]
pv2tobuy = (table_date.collect[i]+table_date.cart[i])/table_date.pv[i]
pv2buy = table_date.buy[i]/table_date.pv[i]
tobuy2buy = table_date.buy[i]/(table_date.collect[i]+table_date.cart[i])
collect2buy = table_date.buy[i]/table_date.collect[i]
cart2buy = table_date.buy[i]/table_date.cart[i]
tran = [pv2collect,pv2cart,pv2tobuy,pv2buy,tobuy2buy,collect2buy,cart2buy]
trans1.append(tran)
data = pd.DataFrame(data=trans1,index=table_date.index,columns=['pv2collect','pv2cart','pv2tobuy','pv2buy','tobuy2buy','collect2buy','cart2buy'])
data.plot()
data.plot(y=['pv2collect','pv2cart','pv2tobuy','pv2buy'])
图中从浏览到收藏的转化,在双12有明显的下降,而且之后大家的收藏、加购物车行为比之前也要低,这时候人们消费欲望经过双12释放会进入一段疲软期,可以适当减少运营活动次数,从而降低成本