數(shù)據(jù)背景:用戶在一家CD網(wǎng)站的消費(fèi)記錄,僅包含4列存皂,不確定是具體是哪類數(shù)據(jù)
使用工具:jupyter
0、數(shù)據(jù)準(zhǔn)備
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
導(dǎo)入所需包
columns =['user_id','order_dt','order_products','order_amount']
df = pd.read_table('CDNOW_master.txt',names=columns,sep='\s+')
df.head()
數(shù)據(jù)僅包含四列
user_id:用戶ID
order_dt:購(gòu)買(mǎi)日期
order_prodects:購(gòu)買(mǎi)產(chǎn)品數(shù)
order_amount:購(gòu)買(mǎi)金額
df.describe()
數(shù)據(jù)描述 根據(jù)數(shù)據(jù)面熟結(jié)果,75%的客戶的購(gòu)買(mǎi)量在3件以內(nèi)策肝,購(gòu)買(mǎi)金額也較低妥泉,符合一個(gè)普通CD店的消費(fèi)習(xí)慣分布
df['order_dt'] = pd.to_datetime(df.order_dt,format='%Y%m%d')
df['month'] = df.order_dt.values.astype('datetime64[M]')
df.head()
將購(gòu)買(mǎi)日期列的格式更改為日期格式椭微,并添加月份(month)列
![](https://upload-images.jia
nshu.io/upload_images/14624538-62b90c62c24b829e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1、進(jìn)行用戶消費(fèi)趨勢(shì)的分析(按月)
- 每月的消費(fèi)總金額
- 每月的消費(fèi)次數(shù)
- 每月的產(chǎn)品購(gòu)買(mǎi)量
- 每月的消費(fèi)人數(shù)
grouped_month=df.groupby('month')
order_month_amount=grouped_month.order_amount.sum()
order_month_amount.head()
plt.style.use('ggplot')
order_month_amount.plot()
消費(fèi)金額前三個(gè)月最高盲链,后續(xù)波動(dòng)下降(原因蝇率?)
user_id列不唯一,是用戶消費(fèi)次數(shù)數(shù)據(jù)刽沾,或與產(chǎn)品關(guān)聯(lián)的訂單數(shù)據(jù)(僅猜測(cè))
grouped_month.user_id.nunique().plot()
grouped_month.order_products.sum().plot()
購(gòu)買(mǎi)金額本慕、用戶數(shù)、數(shù)量高度關(guān)聯(lián)侧漓,可知多數(shù)ID購(gòu)買(mǎi)的金額锅尘、數(shù)量相差不大,主營(yíng)CD價(jià)格相差不大(推測(cè))
df.pivot_table(index= 'month',
values = ['order_products','order_amount','user_id'],
aggfunc = {'order_products':'sum','order_amount':'sum','user_id':'count'})
2.用戶個(gè)體消費(fèi)分析
- 用戶消費(fèi)金額布蔗,消費(fèi)次數(shù)的秒速統(tǒng)計(jì)
- 用戶消費(fèi)金額和消費(fèi)次數(shù)的散點(diǎn)圖
- 用戶消費(fèi)金額的分布圖
- 用戶消費(fèi)次數(shù)的分布圖
- 用戶累計(jì)消費(fèi)金額占比(百分之多少的用戶占了百分之多少的消費(fèi)額)
grouped_user = df.groupby('user_id')
grouped_user.sum().describe()
用戶平均購(gòu)買(mǎi)了7張CD藤违,中位數(shù)3浪腐,部分用戶購(gòu)買(mǎi)了較多數(shù)量的CD;用戶平均消費(fèi)106元顿乒,中位數(shù)43议街,可驗(yàn)證上條結(jié)論
grouped_user.sum().query('order_amount<4000').plot.scatter(x= 'order_amount',y = 'order_products')
去除極值(>=4000)
grouped_user.sum().query('order_products<100').order_products.plot.hist(bins=40)
去除極值(>=100)
從直方圖可知,用戶消費(fèi)金額璧榄,絕大部分呈現(xiàn)集中趨勢(shì)特漩,小部分異常值干擾了判斷,使用過(guò)濾操作排除異常(利用切比雪夫定理過(guò)濾異常值骨杂,95%的數(shù)據(jù)都分布在5個(gè)標(biāo)準(zhǔn)差之內(nèi))
user_cumsum =grouped_user.sum().sort_values('order_amount').apply(lambda x:x.cumsum() / x.sum())
user_cumsum.reset_index().order_amount.plot()
按照用戶消費(fèi)金額進(jìn)行升序排序拾稳,由圖可以知道50%的用戶僅貢獻(xiàn)了15%的消費(fèi)額度,而排名前5000的用戶就貢獻(xiàn)了60%的消費(fèi)額度
3.用戶消費(fèi)行為
- 用戶第一次消費(fèi)(首購(gòu))
- 用戶最后一次消費(fèi)
- 新老客戶消費(fèi)比
- 多少用戶僅消費(fèi)一次
- 每月新客占比
- 用戶分層
- RFM模型
- 新腊脱、老访得、活躍、回流陕凹、流失
- 用戶購(gòu)買(mǎi)周期(按訂單)
- 用戶消費(fèi)周期描述
- 用戶消費(fèi)周期分布
- 用戶生命周期(按第一次和最后一次消費(fèi)) -用戶生命周期描述
- 用戶生命周期分布
grouped_user.min().order_dt.value_counts().plot()
grouped_user.max().order_dt.value_counts().plot()
斷崖式下跌悍抑,看熱鬧人可能意味著前期活動(dòng)引流效果明顯,但未能留住客戶
大部分最后一次購(gòu)買(mǎi)杜耙,集中在前三個(gè)月搜骡,說(shuō)明很多用戶購(gòu)買(mǎi)了一次后就不再進(jìn)行購(gòu)買(mǎi)
user_life = grouped_user.order_dt.agg(['min','max'])
user_life.head()
(user_life['min'] ==user_life['max']).value_counts()
過(guò)半客戶僅消費(fèi)一次
rfm = df.pivot_table(index ='user_id',
values = ['order_products','order_amount','order_dt'],
aggfunc ={'order_dt':'max','order_amount':'sum','order_products':'sum'})
rfm.head()
rfm['R'] = -(rfm.order_dt-rfm.order_dt.max())/np.timedelta64(1,'D')
rfm.rename(columns = {'order_products':'F','order_amount':'M'},inplace = True)
rfm[['R','F','M']].apply(lambda x:x-x.mean())
def rfm_func(x):
level = x.apply(lambda x:'1' if x>=1 else '0')
label = level.R +level.F +level.M
d = {
'111':'重要價(jià)值客戶',
'011':'重要保持客戶',
'101':'重要發(fā)展客戶',
'001':'重要挽留客戶',
'110':'一般價(jià)值客戶',
'010':'一般保持客戶',
'100':'一般發(fā)展客戶',
'000':'一般挽留客戶',
}
result = d[label]
return result
rfm['label'] = rfm[['R','F','M']].apply(lambda x:x-x.mean()).apply(rfm_func,axis=1)
rfm.groupby('label').sum()
rfm.loc[rfm.label == '重要價(jià)值客戶','color'] = 'g'
rfm.loc[~(rfm.label == '重要價(jià)值客戶'),'color'] = 'r'
rfm.plot.scatter('F','R',c=rfm.color)
RFM分析。將用戶以三個(gè)維度分為8種進(jìn)行區(qū)分佑女,認(rèn)為可能在一張圖中對(duì)八種用戶進(jìn)行分類记靡,需縮減坐標(biāo)軸長(zhǎng)度,劃分更多顏色等
從RFM 分層可知团驱,大部分用戶是重要保持客戶摸吠,但是這是由于極值的影響,所以 RFM 的劃分標(biāo)準(zhǔn)應(yīng)該以業(yè)務(wù)為準(zhǔn)嚎花,也可以通過(guò)切比雪夫去除極值后求均值寸痢,并且 RFM 的各個(gè)劃分標(biāo)準(zhǔn)可以都不一樣
pivoted_counts = df.pivot_table(index='user_id',
columns='month',
values='order_dt',
aggfunc='count').fillna(0)
pivoted_counts
df_purchase = pivoted_counts.applymap(lambda x :1 if x >0 else 0)
df_purchase.tail()
def active_status(data):
status = []
for i in range(18):
if data[i]==0:
if len(status) > 0:
if status[i-1] == 'unreg':
status.append('unreg')
else:
status.append('unactive')
else:
status.append('unreg')
else:
if len(status) == 0:
status.append('new')
else:
if status[i-1]=='unactive':
status.append('return')
elif status[i-1]=='unreg':
status.append('new')
else :
status.append('active')
return status
indexs=df['month'].sort_values().astype('str').unique()
purchase_status = pivoted_counts.apply(lambda x:pd.Series(active_status(x),index=indexs),axis=1)
purchase_status.head(5)
這里遇到個(gè)麻煩,可能由于pandas版本是23.4的關(guān)系紊选,導(dǎo)出的結(jié)果并非表格啼止。所以需要對(duì)產(chǎn)生的數(shù)據(jù)再設(shè)置
purchase_status_ct = purchase_status.replace('unreg',np.NaN).apply(lambda x:pd.value_counts(x))
purchase_status_ct
purchase_status_ct.fillna(0).T.plot.area()
新用戶僅前期存在。如果數(shù)據(jù)完整且正常兵罢,該網(wǎng)站長(zhǎng)期未招新(献烦?不合理)
purchase_status_ct.fillna(0).T.apply(lambda x:x/x.sum(),axis =1)
order_diff = grouped_user.apply(lambda x:x.order_dt - x.order_dt.shift())
order_diff.head(10)
order_diff.describe()
訂單周期呈指數(shù)分布
用戶的平均購(gòu)買(mǎi)周期是68天
絕大部分用戶的購(gòu)買(mǎi)周期都低于100天
(order_diff / np.timedelta64(1,'D')).hist(bins=20)
(user_life['max']-user_life['min']).describe()
、
u_1=(user_life['max']-user_life['min'])/np.timedelta64(1,'D')
u_1[u_1>0].hist(bins = 40)
4.復(fù)購(gòu)率和回購(gòu)率分析
- 復(fù)購(gòu)率
- 自然月內(nèi)卖词,購(gòu)買(mǎi)多次的用戶占比(即巩那,購(gòu)買(mǎi)了兩次以上)
pivoted_counts.head()
purchase_r = pivoted_counts.applymap(lambda x: 1 if x>1 else np.NaN if x ==0 else 0)
purchase_r.head()
(purchase_r.sum()/purchase_r.count()).plot(figsize = (10,4))
復(fù)購(gòu)率穩(wěn)定在20%所有,前一個(gè)月因?yàn)橛写罅啃掠脩粲咳耄@批用戶只購(gòu)買(mǎi)了一次拢操,所以導(dǎo)致復(fù)購(gòu)率降低