一.背景
在上篇文章,基于Mysql的淘寶用戶分析中,由于數(shù)據(jù)量太大,進(jìn)行了部分?jǐn)?shù)據(jù)抽取。但實(shí)際工作中,對這種行為數(shù)據(jù)應(yīng)該全部分析的,因此用Python進(jìn)行全部數(shù)據(jù)的分析。
二.數(shù)據(jù)預(yù)處理
讀取并觀察數(shù)據(jù);

刪除無價值字段,對進(jìn)行時間處理;
del user_n['user_geohash']
user_n['daily'] = user_n.time.map(lambda x: x.split(' ')[1])
user_n['date'] = user_n.time.map(lambda x: x.split('-',1)[1].split(' ')[0])
import datetime as dt
user_n['time'] = pd.to_datetime(user_n['time'])
user_n['weekday'] = user_n['time'].dt.weekday
user_n['datetime'] = user_n['time'].dt.date
處理后的數(shù)據(jù);

原數(shù)據(jù)中,將用戶行為用特定行為名稱描述,behavior ={1:'pv',2:'collect',3:'cart',4:'buy'},在此進(jìn)行相關(guān)字段替換;
user_n['behavior_type'] = user_n['behavior_type'].map({1:'pv',2:'collect',3:'cart',4:'buy'})
三.時間維度分析
2.1 分時段流量
分時段統(tǒng)計(jì)各點(diǎn)擊、收藏、加購、購買人數(shù);
pv_num = user_n[user_n.behavior_type =='pv'].groupby('daily')['user_id'].count()
collect_num = user_n[user_n.behavior_type =='collect'].groupby('daily')['user_id'].count()
cart_num = user_n[user_n.behavior_type =='cart'].groupby('daily')['user_id'].count()
buy_num = user_n[user_n.behavior_type =='buy'].groupby('daily')['user_id'].count()
用圖表展示其流量變化;
L0 = Line()
L0.add_xaxis(list(pv_num.index))
L0.add_yaxis('瀏覽人數(shù)',list(pv_num))
L0.add_yaxis('收藏人數(shù)',list(collect_num))
L0.add_yaxis('加購人數(shù)',list(cart_num))
L0.add_yaxis('購買人數(shù)',list(buy_num))
L0.set_global_opts(title_opts=opts.TitleOpts(title="分時段流量分布"),
tooltip_opts=opts.TooltipOpts(trigger="axis",axis_pointer_type='cross'))
L0.set_series_opts(label_opts=opts.LabelOpts(is_show = False))
L0.render_notebook()
圖片如下:

可以看出,點(diǎn)擊、收藏、加入購物車的數(shù)量都在晚上存在明顯的提升,但是相對而言在購買上晚上的提升沒有其他用戶行為顯著。
2.2 分日期流量
同理,分日期流量如下:

2.3 分周流量

從圖中可以看出在工作日當(dāng)中pv量高于周末時候,這與原本預(yù)想的不太一樣。
從時間維度我們可以得出以下幾點(diǎn):
1.用戶對商品的瀏覽,收藏,加入購物一般在晚上7點(diǎn)到11點(diǎn)最多,但是在這期間購買量并沒有比白天有顯著的提升;
2.雙十二活動促銷效果明顯,短時間內(nèi)提升了用戶的瀏覽、收藏、加入購物車行為猛增,在雙十二當(dāng)天購買量成倍數(shù)增長。
3.非工作日的購買量低于工作日。 對工作日白天晚上進(jìn)行觀察。
四.行為路徑轉(zhuǎn)化率
p0 = user_n.groupby(['behavior_type']).size()
[list(z) for z in zip(p0.index.tolist(),p0.tolist())]
轉(zhuǎn)化率數(shù)據(jù)可視化;
from pyecharts import options as opts
from pyecharts.charts import Funnel
F1 = Funnel(init_opts=opts.InitOpts(width="780px",height="350px"))
F1.add("行為轉(zhuǎn)化",[list(z) for z in zip(p0.index.tolist(),p0.tolist())])
F1.set_global_opts(title_opts=opts.TitleOpts(title="Funnel-行為轉(zhuǎn)化"))
F1.render_notebook()
行為轉(zhuǎn)化如下:

總體來說,從上面的圖片并不能明確判斷用戶的轉(zhuǎn)化路徑是哪一種,pv-cart-collect-buy,還是pv-collect-buy.因此需要細(xì)化去分析。
user_trans = user_n.groupby(['date','behavior_type']).size().unstack()
cart_to_buy = user_trans.apply(lambda x: x[0] / x[1], axis=1)
pv_to_collect = user_trans.apply(lambda x: x[2] / x[3], axis=1)
pv_to_cart = user_trans.apply(lambda x: x[1] / x[3], axis=1)
cart_to_collect = user_trans.apply(lambda x: x[2] / x[1], axis=1)
collect_to_buy = user_trans.apply(lambda x: x[0] / x[2], axis=1)
逐日對比兩種路徑的轉(zhuǎn)化。

從路徑轉(zhuǎn)化的曲線來看,cart-to-collect這一路徑與其他轉(zhuǎn)化路徑相比異常。
五.復(fù)購率
查看最受歡迎的商品類目。

計(jì)算復(fù)購率。
re_purchase = user_n.groupby(['item_id','user_id']).size()
for i in range(1,10):
repurchase_rate = len(re_purchase[re_purchase>i])/len(re_purchase)
print('重復(fù)',i,'次購買率:',repurchase_rate)
商品復(fù)購情況如下:

71%以上的商品都存在重復(fù)購買的現(xiàn)象,而用一用戶能重復(fù)購買三次以上的商品不多。
同一用戶的最高重復(fù)購買次數(shù)為128,要么這種商品是暢銷消耗品,要么是銷售環(huán)節(jié)的漏洞或風(fēng)險。
六.留存率
留存率定義:某日的留存率為用戶在當(dāng)日后N天的仍然在線的用戶。在此計(jì)算3日留存率,7日留存率。
3日留存率所需要查看的日期;
date = pd.Series(df.datetime.unique()).sort_values()[:-3]
遍歷date里面的每個日期i,提取后三天仍然活躍的用戶。user_n1 與new_user 里面共同的用戶為留存用戶。
user = []
for i in date:
new_user = set(df[df.datetime == i]['user_id'].unique()) - set(user)
user.extend(new_user)
user_n1 = df[df.datetime == i+timedelta(3)]['user_id'].unique()
最終留存率可視化。
