电商用户行为分析
2.3用户价值分析
根据美国数据库营销研究所Arthur Hughes的研究,客户数据库中有3个神奇的要素,这3个要素构成了数据分析最好的指标:
最近一次消费 (Recency)
消费频率 (Frequency)
消费金额 (Monetary)
根据三个指标分数的高低,能大概将客户分成下面4类(1代表高,0代表低)
重要价值客户(111)、重要保持客户(011)
重要发展客户(101)、重要挽留客户(001)。
下面我们将根据已有的数据来搭建RFM模型(由于数据中没有金额所以我们只能打出R和F的分值)
也许更应该叫RF模型?
最近一次的消费即为用户最后消费的日期和12月3日的差值,消费频率则是在这9天里用户的消费次数,
首先我们要对消费次数和时间差进行分数的定义,
先计算出最大值和最小值
select max(消费次数), min(消费次数), max(时间差), min(时间差)
from(select user_id,count(*) as 消费次数,datediff('2017-12-03', MAX(date)) as 时间差
from ubd
where behavior_type = 'buy'
group by user_id) as a
对于”最近一次消费“,时间差只有0-8,我们将其分为三个档次
分值 | 时间差 |
---|---|
1 | 0-2天 |
2 | 3-5天 |
3 | 6-8天 |
同样我们将“消费频率“也分为三个档次
分值 | 消费次数 |
---|---|
1 | 1-31次 |
2 | 32-63次 |
3 | 64-94次 |
其中上面这个sql语句中的子表a可以单独拿出来查询,这样就可以得到每个用户的情况了
select user_id,count(*) as 消费次数,datediff('2017-12-03', MAX(date)) as 时间差
from ubd
where behavior_type = 'buy'
group by user_id
以此来计算R和F的分值
select user_id,
(case when 时间差 BETWEEN 0 and 2 THEN 3
when 时间差 BETWEEN 3 and 5 THEN 2
when 时间差 BETWEEN 6 and 8 THEN 1
else null end
) as R,
(case when 消费次数 BETWEEN 1 and 31 THEN 1
when 消费次数 BETWEEN 32 and 63 THEN 2
when 消费次数 BETWEEN 64 and 94 THEN 3
else null end
) as F
from(select user_id,count(*) as 消费次数,datediff('2017-12-03', MAX(date)) as 时间差
from ubd
where behavior_type = 'buy'
group by user_id) as a
group by user_id
那么要如何定义R和F的高于低呢,其实一般都是计算出均值,高于均值的就算高,低于均值的就算低
select avg(R), avg(F)
from(select user_id,
(case when 时间差 BETWEEN 0 and 2 THEN 3
when 时间差 BETWEEN 3 and 5 THEN 2
when 时间差 BETWEEN 6 and 8 THEN 1
else null end
) as R,
(case when 消费次数 BETWEEN 1 and 31 THEN 1
when 消费次数 BETWEEN 32 and 63 THEN 2
when 消费次数 BETWEEN 64 and 94 THEN 3
else null end
) as F
from(select user_id,count(*) as 消费次数,datediff('2017-12-03', MAX(date)) as 时间差
from ubd
where behavior_type = 'buy'
group by user_id) as a
group by user_id) as b
然后再根据均值来分类
select user_id,
(case when R>=2.4148 and F>=1.0006 then '重要价值客户'
when R<2.4148 and F>=1.0006 then '重要保持客户'
when R>=2.4148 and F<1.0006 then '重要发展客户'
when R<2.4148 and F<1.0006 then '重要挽留客户'
end) as 用户等级
from(select user_id,
(case when 时间差 BETWEEN 0 and 2 THEN 3
when 时间差 BETWEEN 3 and 5 THEN 2
when 时间差 BETWEEN 6 and 8 THEN 1
else null end
) as R,
(case when 消费次数 BETWEEN 1 and 31 THEN 1
when 消费次数 BETWEEN 32 and 63 THEN 2
when 消费次数 BETWEEN 64 and 94 THEN 3
else null end
) as F
from(select user_id,count(*) as 消费次数,datediff('2017-12-03', MAX(date)) as 时间差
from ubd
where behavior_type = 'buy'
group by user_id) as a
group by user_id) as b
group by user_id
再对各类用户总数进行一个统计
select 用户等级,count(*) as 用户数
from(select user_id,
(case when R>=2.4148 and F>=1.0006 then '重要价值客户'
when R<2.4148 and F>=1.0006 then '重要保持客户'
when R>=2.4148 and F<1.0006 then '重要发展客户'
when R<2.4148 and F<1.0006 then '重要挽留客户'
end) as 用户等级
from(select user_id,
(case when 时间差 BETWEEN 0 and 2 THEN 3
when 时间差 BETWEEN 3 and 5 THEN 2
when 时间差 BETWEEN 6 and 8 THEN 1
else null end
) as R,
(case when 消费次数 BETWEEN 1 and 31 THEN 1
when 消费次数 BETWEEN 32 and 63 THEN 2
when 消费次数 BETWEEN 64 and 94 THEN 3
else null end
) as F
from(select user_id,count(*) as 消费次数,datediff('2017-12-03', MAX(date)) as 时间差
from ubd
where behavior_type = 'buy'
group by user_id) as a
group by user_id) as b
group by user_id) as c
group by 用户等级
order by 用户数 desc
用一个饼图来可视化
import numpy as np
from pymysql import Connect
from pyecharts.charts import Pie
from pyecharts import options as opts
conn = Connect(host='localhost', port=3306, user='root', password='123456', database='demo')
cursor = conn.cursor() # 获取游标
sql = '''
select 用户等级,count(*) as 用户数
from(select user_id,
(case when R>=2.4148 and F>=1.0006 then '重要价值客户'
when R<2.4148 and F>=1.0006 then '重要保持客户'
when R>=2.4148 and F<1.0006 then '重要发展客户'
when R<2.4148 and F<1.0006 then '重要挽留客户'
end) as 用户等级
from(select user_id,
(case when 时间差 BETWEEN 0 and 2 THEN 3
when 时间差 BETWEEN 3 and 5 THEN 2
when 时间差 BETWEEN 6 and 8 THEN 1
else null end
) as R,
(case when 消费次数 BETWEEN 1 and 31 THEN 1
when 消费次数 BETWEEN 32 and 63 THEN 2
when 消费次数 BETWEEN 64 and 94 THEN 3
else null end
) as F
from(select user_id,count(*) as 消费次数,datediff('2017-12-03', MAX(date)) as 时间差
from ubd
where behavior_type = 'buy'
group by user_id) as a
group by user_id) as b
group by user_id) as c
group by 用户等级
order by 用户数 desc
'''
cursor.execute(sql)
data = cursor.fetchall()
data = np.array(data)
print(data)
x = [data[i][0] for i in range(data.shape[0])]
y = [data[j][1] for j in range(data.shape[0])]
pie = (
Pie()
.add("", [list(z) for z in zip(x, y)])
.set_global_opts(title_opts=opts.TitleOpts(title="用户价值分类"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}({d})%"))
)
pie.render('6.html')
从图中我们可以分析出:
重要发展客户和重要挽留客户的数量远远超过重要价值用户和重要保持客户,这么来看平台用户粘性不高,大多数用户都是活动的时候进行一些操作然后就会离开平台一段时间。
1.其中,重要发展的客户数量超过了50%,此类用户,都是近期进行过消费,但是总的消费频率不高。对此我们需要根据分析其在平台上点击,收藏的商品,然后发放此类商品的优惠券或者是时不时举行特点活动来促进消费频率的上升。
2.而重要挽留客户则是有着大量流失可能性的用户,为了进一步发展和挽留这些客户,我们需要更精准的对其投放推荐和优惠活动,可以在其活动最活跃的时间进行投放。还可以定期让平台的客服进行电话寻访,了解客户对平台的不满和建议并且收集进行汇总,然后酌情进行分析。
3.重要价值客户是平台最重要的用户,需要每天检查用户的活跃时间和进行操作过的商品,制定合理的推荐策略,一但这一天的活跃时间发生异常就要寻找原因,可以定期提供短信,电话寻访。
4.重要发展客户是占比最少的用户,但是他们具有一定的潜在价值,很有可能是最近才被平台的某个活动吸引而来的用户,需要观察一定时间,再投其所好的进行推荐。