文章目录
- 1. 项目背景及目的
- 1.1 项目背景
- 1.2 项目目的
 
 
- 2. 理解数据
- 3. 数据预处理
- 3.1 字段调整
- 3.2 数据清洗
- 3.2.1 空值
- 3.2.2 重复值
- 3.2.3 异常值
 
 
 
 
- 4. 数据分析
- 4.1 人
- 4.1.1 获客情况(PV、UV、PV/UV)
- 4.1.2 留存情况(留存率、跳失率)
- 4.1.3 行为情况(时间序列、用户转化率、行为路径)
- 4.1.4 用户定位( RFM模型)
 
 
- 4.2 货
- 4.2.1 商品热度分类(商品、品类、品类商品热度)
- 4.2.2 商品转化率
 
 
 
 
- 5. 数据可视化
- 6. 结论与建议
 
1. 项目背景及目的
1.1 项目背景
现有一组用户行为数据,希望通过对用户行为的分析,探究提高销量的措施。
1.2 项目目的
- 了解获客和留存情况;
- 分析用户行为路径,找到用户活跃的时间规律;
- 利用RFM模型对用户进行分类,针对不同的用户类型采用不同的维护方式;
- 分析热门浏览商品/品类和热门购买商品/品类之间的关系,找到合理的推送机制。
2. 理解数据
userbehavior包含了2017年11月25日至2017年12月3日之间所有的用户行为数据,详细字段及期含义见下表:
| 列名称 | 说明 | 
|---|---|
| user_id | 整数类型,用户ID | 
| item_id | 整数类型,商品ID | 
| category_id | 整数类型,商品类目ID | 
| behavior_type | 行为类型,枚举类型,pv(浏览)、buy(购买)、cart(加购物车)、fav(收藏) | 
| timestamps | 时间戳 | 
3. 数据预处理
3.1 字段调整
--修改timestamp列为timestamps
alter table userbehavior change timestamp timestamps int;
--增加列datetimes
alter table userbehavior add datetimes datetime;
update userbehavior set datetimes=FROM_UNIXTIME(timestamps);
--增加列dates,times,hours
alter table userbehavior add dates date;
alter table userbehavior add times time;
alter table userbehavior add hours int;
update userbehavior set dates=date(datetimes);
update userbehavior set times=DATE_FORMAT(datetimes,'%H:%i:%s');
update userbehavior set hours=hour(datetimes);
3.2 数据清洗
3.2.1 空值
--检查空值
select * from userbehavior where user_id is null or user_id='' or user_id=' ';
select * from userbehavior where item_id is null or item_id='' or item_id=' ';
select * from userbehavior where category_id is null or category_id='' or category_id=' ';
select * from userbehavior where behavior_type is null or behavior_type='' or behavior_type=' ';
select * from userbehavior where timestamps is null or timestamps='' or timestamps=' ';
无空值。
3.2.2 重复值
用户、商品和时间 3个字段租合应该具有唯一性。
select user_id,item_id,timestamps from userbehavior group by user_id,item_id,timestamps having count(*)>1;
无重复值。
3.2.3 异常值
  时间范围应在2017年11月25日至2017年12月3日之间;
   behavior_type 应该=pv/buy/cart/fav
--检查异常值 
--时间范围应在2017年11月25日至2017年12月3日之间
select min(datetimes),max(datetimes) from userbehavior;
--有44条超出时间范围的异常值
select count(*)'超出时间范围的记录数量' from userbehavior where datetimes<'2017-11-25' or datetimes>'2017-12-03 23:59:59';
-- 删除44条时间异常的数据
delete from userbehavior where datetimes<'2017-11-25' or datetimes>'2017-12-03 23:59:59';
-- behavior_type 应该在pv/buy/cart/fav中
select * from userbehavior where  behavior_type not in ('pv','buy','cart','fav');
--behavior_type无异常

 
4. 数据分析
  分析思路如下:
 
4.1 人
4.1.1 获客情况(PV、UV、PV/UV)
页面浏览量PV
 独立访客数UV
 浏览深度PV/UV
-- 人 获客情况
-- PV、UV
select dates
,count(*) PV
,count(distinct user_id) UV
,round(count(*)/count(distinct user_id),2) 'PV/UV'
from userbehavior where behavior_type='pv' group by dates;

   可视化如下:
 
   11月25日-30日PV和UV一直比较稳定,从12月开始由显著的增长,为探究期增长原因可以提出以下假设:
- 周末或节假日十流量增加;
- 有活动吸引了新老用户。
  查看日历,12月1日并非周末或节假日,排除假设1。12月有双12活动开始,吸引了新老用户,假设2成立。
   平均每个用户可以带来13个页面浏览量,浏览深度相对稳定。
4.1.2 留存情况(留存率、跳失率)
留存率
 跳失率
--人 留存情况
--留存率
create table retention_rate as 
select a.dates
,round(count(distinct case when datediff(b.dates,a.dates)=1 then a.user_id end)/count(distinct a.user_id),2) 次日留存率
,round(count(distinct case when datediff(b.dates,a.dates)=2 then a.user_id end)/count(distinct a.user_id),2) 第2日留存率
,round(count(distinct case when datediff(b.dates,a.dates)=3 then a.user_id end)/count(distinct a.user_id),2) 第3日留存率
,round(count(distinct case when datediff(b.dates,a.dates)=4 then a.user_id end)/count(distinct a.user_id),2) 第4日留存率
,round(count(distinct case when datediff(b.dates,a.dates)=5 then a.user_id end)/count(distinct a.user_id),2) 第5日留存率
,round(count(distinct case when datediff(b.dates,a.dates)=6 then a.user_id end)/count(distinct a.user_id),2) 第6日留存率
,round(count(distinct case when datediff(b.dates,a.dates)=7 then a.user_id end)/count(distinct a.user_id),2) 第7日留存率
,round(count(distinct case when datediff(b.dates,a.dates)=8 then a.user_id end)/count(distinct a.user_id),2) 第8日留存率
from userbehavior a left join userbehavior b on a.user_id=b.user_id
group by a.dates;
-- 跳失率
-- 跳失用户
select user_id
from userbehavior
group by user_id
having count(*)=1;
-- 没有跳失用户。
  可视化如下:
 
   多日留存率数据太少,重点看下次日留存率,总体留存率均较高,且没有跳失用户,说明平台粘性很优秀。12月开始留存率有显著增长,原因同样为双12活动。
4.1.3 行为情况(时间序列、用户转化率、行为路径)
时间序列分析
-- 时间序列
create table date_hour_behavior as 
select dates,hours
,count(if(behavior_type='pv',behavior_type,null)) pv_count
,count(if(behavior_type='buy',behavior_type,null)) buy_count
,count(if(behavior_type='cart',behavior_type,null)) cart_count
,count(if(behavior_type='fav',behavior_type,null)) fav_count
from userbehavior
group by dates,hours
order by dates,hours;
可视化图下:
 
- 统计周期内11月25日-12月3日(周六-周日)用户行为:从12月开始浏览量PV、加购CART和收藏FAV均有明显上升;
- 用户日内行为:晚间2-4点操作最少,22点左右各类行为达到当天峰值。
用户转化率分析
   用户的行为路径如下:
 
-- 各行为的用户数量
select behavior_type,count(distinct user_id) user_count
from 
(select user_id,
case when behavior_type='fav' or behavior_type='cart' then 'fav_cart' else behavior_type end behavior_type
from userbehavior)t
where user_id in (select distinct user_id from userbehavior where behavior_type='pv')
group by behavior_type; 
-- buy	669
-- fav_cart	838
-- pv	980
-- 总购买率
create table user_buy_rate as 
select count(distinct if(behavior_type='buy',user_id,null))/count(distinct if(behavior_type='pv',user_id,null)) user_buy_rate -- 购买率0.6827
from userbehavior
where user_id in (select distinct user_id from userbehavior where behavior_type='pv');
-- 总收藏加购率0.8551
create table user_fav_cart_rate as 
select count(distinct if(behavior_type='fav' or behavior_type='cart',user_id,null))/count(distinct if(behavior_type='pv',user_id,null)) user_buy_rate 
from userbehavior
where user_id in (select distinct user_id from userbehavior where behavior_type='pv');
-- 周内用户转化率
create table dates_user_rate as 
select dates
,count(distinct if(behavior_type='pv',user_id,null)) pv_count
,count(distinct if(behavior_type='buy',user_id,null)) buy_count
,count(distinct if(behavior_type='cart',user_id,null)) cart_count
,count(distinct if(behavior_type='fav',user_id,null)) fav_count
,count(distinct if(behavior_type='buy',user_id,null))/count(distinct if(behavior_type='pv',user_id,null)) user_buy_rate
,count(distinct if(behavior_type='fav' or behavior_type='cart',user_id,null))/count(distinct if(behavior_type='pv',user_id,null)) user_fav_cart_rate
from userbehavior
where user_id in (select distinct user_id from userbehavior where behavior_type='pv')
group by dates
order by dates;
-- 日内用户转化率
create table hours_user_rate as 
select hours
,count(distinct if(behavior_type='pv',user_id,null)) pv_count
,count(distinct if(behavior_type='buy',user_id,null)) buy_count
,count(distinct if(behavior_type='cart',user_id,null)) cart_count
,count(distinct if(behavior_type='fav',user_id,null)) fav_count
,count(distinct if(behavior_type='buy',user_id,null))/count(distinct if(behavior_type='pv',user_id,null)) user_buy_rate
,count(distinct if(behavior_type='fav' or behavior_type='cart',user_id,null))/count(distinct if(behavior_type='pv',user_id,null)) user_fav_cart_rate
from userbehavior
where user_id in (select distinct user_id from userbehavior where behavior_type='pv')
group by hours
order by hours;
  可视化如下:
 
- 用户并没有在浏览后退出,购买率68.27%,收藏加购率85.51%;
- 购买率基本符合用户行为的一般趋势,在22点左右达到峰值;收藏加购率在夜晚2点左右达到峰值;购买率和收藏加购率分别在11月30日、12月1日达到峰值,说明双12预售活动有显著成效。
行为路径分析
   针对已购买的用户进行行为路径分析,可以分为8类:直接购买、浏览后购买、加购后购买、浏览加购后购买、收藏后购买、浏览收藏后购买、收藏加购后购买、浏览收藏加购后购买。
   未经浏览过程的,是之前已经加购或收藏的用户。
-- 行为路径分析
create table behavior_path as 
select user_id,item_id,behavior_no
,(case when behavior_no=0001 then '直接购买了'
when behavior_no=1001 then '浏览后购买了'
when behavior_no=0011 then '加购后购买了'
when behavior_no=1011 then '浏览加购后购买了'
when behavior_no=0101 then '收藏后购买了'
when behavior_no=1101 then '浏览收藏后购买了'
when behavior_no=0111 then '收藏加购后购买了'
when behavior_no=1111  then '浏览收藏加购后购买了'
end )行为
from 
		-- 行为编码表
	(select user_id,item_id
	,concat(if(count(if(behavior_type='pv',1,null))>0,1,0)
	,if(count(if(behavior_type='cart',1,null))>0,1,0)
	,if(count(if(behavior_type='fav',1,null))>0,1,0)
	,if(count(if(behavior_type='buy',1,null))>0,1,0)) behavior_no
	from userbehavior
	group by user_id,item_id) t
where behavior_no like '%1' 

   大多数用户浏览后直接购买,占比51.27%,并未经过收藏或加购的过程;其次为之前已经加购或收藏的用户,直接进行购买,占比27.01%;还有部分用户经过浏览加购后进购物车进行结算,占比11.27%。
4.1.4 用户定位( RFM模型)
RFM是按照R(时间间隔)、F(消费频次)、M(消费金额)3个维度,将用户分为重要价值、重要发展、重要保持、重要挽留、一般价值、一般发展、一般保持、一般挽留8类。
select user_id,count(*)
from userbehavior
where behavior_type='buy'
group by user_id
order by count(*) desc;
-- 消费频次范围 1-43
由于数据有限,从R、F两个角度,按照下表规则,将用户进行划分
| 分值 | R (天) | F (次数) | 
|---|---|---|
| 1 | >7 | 1 | 
| 2 | 6-7 | 2-5 | 
| 3 | 4-5 | 6-10 | 
| 4 | 2-3 | 11-20 | 
| 5 | <=1天 | >20 | 
以R、F求得平均值为标准,按照以下规则,将用户划分为4类
| 用户类型 | R | F | 
|---|---|---|
| 价值用户 | >avg(R) | >avg(F) | 
| 保持用户 | <avg (R) | >avg(F) | 
| 发展用户 | >avg(R) | <avg(F) | 
| 挽留用户 | <avg(R) | <avg(F) | 
create view user_rf_score_view as 
select a.user_id,r_score,f_score
from 
-- 各用户的R分数
	(select user_id
	,(case when date_diff<=1 then 5
				when date_diff>=2 and date_diff<=3 then 4
				when date_diff>=4 and date_diff<=5 then 3
				when date_diff>=6 and date_diff<=7 then 2
				when date_diff>7 then 1 end) r_score
	from 
		(select user_id,datediff('2017-12-04',max(dates)) date_diff
		from userbehavior
		where behavior_type='buy'
		group by user_id) t1) a
left join
-- 各用户的f分数
	(select user_id
	,(case when f>20 then 5
				when f>=11 and f<=20 then 4
				when f>=6 and f<=10 then 3
				when f>=2 and f<=5 then 2
				when f=1 then 1 end) f_score
	from 
		(select user_id,count(*) f
		from userbehavior
		where behavior_type='buy'
		group by user_id) t2) b
on a.user_id=b.user_id
-- 创建变量,并用r和f的平均值赋值
set @avg_r=null;
set @avg_f=null;
select  @avg_r :=avg(r_score), @avg_f:=avg(f_score) 
from user_rf_score_view;
-- 同平均值比较,对用户进行分类
create table user_rf as
select user_id,r_score,f_score
,case when r_score>@avg_r and f_score>@avg_f then '价值用户'
			when r_score<@avg_r and f_score>@avg_f then '保持用户'
			when r_score>@avg_r and f_score<@avg_f then '发展用户'
			when r_score<@avg_r and f_score<@avg_f then '挽回用户'
			end 用户价值
from user_rf_score_view
  数据可视化如下:
 
   价值用户占比超过40%,另外挽回用户的占比已经超过发展用户,需要重点关注。
4.2 货
4.2.1 商品热度分类(商品、品类、品类商品热度)
按照PV来进行热度排名。
-- 商品按热度分类 按照PV
-- 热门品类
create table category_count as
select a.category_id,category_pv_count,category_buy_count,rk_pv,rk_buy
from 
		(select category_id,count(*) category_pv_count,rank() over(order by count(*) desc) rk_pv
		from userbehavior
		where behavior_type='pv'
		group by category_id) a
left join 
		(select category_id,count(*) category_buy_count,rank() over(order by count(*) desc) rk_buy
		from userbehavior
		where behavior_type='buy'
		group by category_id) b
on a.category_id=b.category_id
order by 2 desc;
-- 热门商品
create table item_pv_count as 
select a.item_id,item_pv_count,item_buy_count,rk_pv,rk_buy
from 
		(select item_id,count(*) item_pv_count,rank() over(order by count(*) desc) rk_pv
		from userbehavior
		where behavior_type='pv'
		group by item_id) a
left join 
		(select item_id,count(*) item_buy_count,rank() over(order by count(*) desc) rk_buy
		from userbehavior
		where behavior_type='buy'
		group by item_id) b
on a.item_id=b.item_id
order by 2 desc;
-- 热门品类中的热门商品TOP3
create table category_item_pv_count as 
select  category_id,item_id,品类商品浏览量 
from 
	(select category_id,item_id,count(*) 品类商品浏览量
	,rank() over(partition by category_id order by count(*) desc) rk
	from userbehavior
	where behavior_type='pv'
	and category_id in (select category_id
											from (select category_id,count(*),rank() over(order by count(*) desc) rk
														from userbehavior
														where behavior_type='pv'
														group by category_id) t
											where rk<=3)
	group by category_id,item_id
	order by 1,3 desc) t
where rk<=3;

 
   数据可视化如下:
 
商品或品类浏览量排名靠前的,购买量排名并没有靠前,浏览量和购买量不成正比,平台推荐的产品用户的浏览量是有的,但是并没有成功转化购买,说明推荐商品不是用户最感兴趣的品,或非必需品。
4.2.2 商品转化率
-- 商品转化率分析
-- 商品转化率
create table item_conversion_rate as
select item_id
,count(if(behavior_type='pv',behavior_type,null)) pv
,count(if(behavior_type='fav',behavior_type,null)) fav
,count(if(behavior_type='cart',behavior_type,null)) cart
,count(if(behavior_type='buy',behavior_type,null)) buy
,round(count(distinct if(behavior_type='buy',user_id,null))/count(distinct user_id),2) 商品转化率
from userbehavior
group by item_id
order by 商品转化率 desc;
select 商品转化率,count(商品转化率) as co
from
(SELECT item_id
,(case when 商品转化率<=0.1 then '0-10%'
when 商品转化率>0.1 and 商品转化率<=0.3 then '10%-30%'
when 商品转化率>0.3 and 商品转化率<=0.6 then '30%-60%'
when 商品转化率>0.6 and 商品转化率<=1 then '60%-100%' end)商品转化率
from item_conversion_rate
) a
group by 商品转化率
-- 品类转化率
create table category_conversion_rate as
select category_id
,count(if(behavior_type='pv',behavior_type,null)) pv
,count(if(behavior_type='fav',behavior_type,null)) fav
,count(if(behavior_type='cart',behavior_type,null)) cart
,count(if(behavior_type='buy',behavior_type,null)) buy
,round(count(distinct if(behavior_type='buy',user_id,null))/count(distinct user_id),2) 品类转化率
from userbehavior
group by category_id
order by 品类转化率 desc;
select 品类转化率,count(品类转化率) as co
from
(SELECT category_id
,(case when 品类转化率<=0.1 then '0-10%'
when 品类转化率>0.1 and 品类转化率<=0.3 then '10%-30%'
when 品类转化率>0.3 and 品类转化率<=0.6 then '30%-60%'
when 品类转化率>0.6 and 品类转化率<=1 then '60%-100%' end)品类转化率
from category_conversion_rate
) a
group by 品类转化率
  数据可视化如下:
 
   虽然用户的转化率很高,但是品类和商品的转化率不高。转化率低于10%的品类和商品占比分别高达78.52%和96.93%。进一步说明,推荐给用户的品类或商品与用户的需求不符。
5. 数据可视化
  完整的数据可视化仪表板如下:
 
6. 结论与建议
- 获客方面:浏览深度在统计周期内相对稳定,保持在12-14之间。
- 留存率方面:从次日留存率看,均达到77%以上,12月之后由于双12活动预热,留存率有明显增长,统计周期内不存在跳失用户,说明平台粘性较高。
建议: 可以通过策划活动进行获客和提高留存,但要注意一定的周期性。
-  用户行为方面: - 用户在日内20:00-23:00活跃性高一点,基本在21:00左右达到峰值;在统计周期内,双12活动带来比较明显的各类行为增长;
- 用户购买率在22:00达到峰值,收藏加购率在2:00达到峰值;
- 在购买的用户中,行为路径排在前3的为浏览后购买的用户51.27%、直接购买的用户27.01%、浏览加购后购买的用户11.71%。
 
建议: 优惠券、促销或商品推荐等活动建议在20:00-22:00之间进行,此时用户活跃度最高。
- 用户定位方面:价值用户占比最多,超过40%,但挽回用户同样需要关注,占比将近20%。
建议: 针对不同的用户采用不同的维护策略。针对价值用户重点服务,维持和留存为主;
 针对保持用户要防止其流失,通过APP、短信等进行提醒,必要时主动联系,并有针对性推荐产品;
 针对发展用户引导加入会员充值,提高消费频次,增加用户粘性,使其成为价值用户;
 针对挽回用户要重点召回,挖掘其需求,可以发放优惠券或推荐新品引导回购。
- 热门品类/商品方面:商品/品类浏览量和购买量的排名相差很大;商品/品类的转化率低于10%高达96.93%和78.52%。说明用户浏览的商品/品类和所需商品/品类,有较大差异。
建议: 优化平台的推荐机制。增加热销产品的推送。根据用户历史购买、加购或收藏的商品进行分析,针对不同的用户推荐不同的商品。











![推荐系统[三]:粗排算法常用模型汇总(集合选择和精准预估),技术发展历史(向量內积,WideDeep等模型)以及前沿技术](https://img-blog.csdnimg.cn/b935ff75e3e84d7f88de39613bb02bad.png)






