这是是最简单的一个思路,具体的规则大家可以自己整理~~~~
1.1课题介绍
本课题以阿里巴巴移动电商平台的真实用户-商品行为数据为基础,同时提供移动时代特有的位置信息。您需要通过大数据和算法构建面向移动电子商务的商品推荐模型,挖掘数据背后丰富的内涵,为移动用户在合适的时间、合适的地点精准推荐合适的内容。
在真实的业务场景下,我们往往需要对所有商品的一个子集构建个性化推荐模型。在完成这件任务的过程中,我们不仅需要利用用户在这个商品子集上的行为数据,往往还需要利用更丰富的用户行为数据。
如下是一个真实的业务场景:
给出一定量用户在时间段11月18日~12月18日内的移动端行为数据(D),
需要预测12月19日用户对商品子集(P)的购买数据。具体的数据说明下面将会详细介绍。
1.1数据说明
提供的数据包含两个部分。第一部分是用户在商品全集上的移动端行为数据(D),表名为tianchi_mobile_recommend_train_user,包含如下字段:
字段 | 字段说明 | 提取说明 |
user_id | 用户标识 | 抽样&字段脱敏 |
item_id | 商品标识 | 字段脱敏 |
behavior_type | 用户对商品的行为类型 | 包括浏览、收藏、加购物车、购买,对应取值分别是1、2、3、4。 |
user_geohash | 用户位置的空间标识,可以为空 | 由经纬度通过保密的算法生成 |
item_category | 商品分类标识 | 字段脱敏 |
Time | 行为时间 | 精确到小时级别 |
D集合的记录显示如下:
每一行代表了用户 user_id 对属于分类 item_category 的物品 item_id 在 time 这个时间于地点 user_geohash 发生了交互,交互类型是 behavior_type 。behavior_type 包括浏览、收藏、加购物车、购买,对应取值分别是1、2、3、4。
给出的一共包含31天的交互数据,最后要预测第32天有哪些user会购买哪些item。
第二个部分是商品子集(P),表名为tianchi_mobile_recommend_train_item,包含如下字段:
字段 | 字段说明 | 提取说明 |
item_id | 商品标识 | 抽样&字段脱敏 |
item_ geohash | 商品位置的空间标识,可以为空 | 由经纬度通过保密的算法生成 |
item_category | 商品分类标识 | 字段脱敏 |
这部分的数据,是 tianchi_mobile_recommend_train_user表中出现的物品item_id的一个子集。官方的第32天的会发生购买的 user, item 中的item都是这个数据表中的item,所以我们预测的 user, item 需要根据这个文件来过滤掉一些结果。
训练数据包含了抽样出来的一定量用户在一个月时间(11.18~12.18)之内的移动端行为数据(D),评分数据是这些用户在这个一个月之后的一天(12.19)对商品子集(P)的购买数据。您需要使用训练数据建立推荐模型,并输出用户在接下来一天对商品子集购买行为的预测结果。
1.1 输出要求
完成用户对商品子集P的购买预测之后,需要将结果放入指定格式的数据表(非分区表)中,要求结果表名为:tianchi_mobile_recommendation_predict,包含user_id和item_id两列(均为string类型),要求去除重复。
表名:tianchi_mobile_recommendation_predict
结果表样例:
字段 | 字段类型 | 示例 |
User_id | String | 100001 |
Item_id | String | 12900 |
1.1 评估指标
比赛采用经典的精确度(precision)、召回率(recall)和F1值作为评估指标。具体计算公式如下:
其中PredictionSet为算法预测的购买数据集合,ReferenceSet为真实的答案购买数据集合。我们以F1值作为最终的唯一评测标准。
2 解答
2.1题目解读
已知用户在时间段11月18日~12月18日内的移动端行为数据(D),预测12月19日用户对商品子集(P)的购买数据。数据(D),表名为tianchi_mobile_recommend_train_user,包含如下字段:
字段 | 字段说明 | 提取说明 |
user_id | 用户标识 | 抽样&字段脱敏 |
item_id | 商品标识 | 字段脱敏 |
behavior_type | 用户对商品的行为类型 | 包括浏览、收藏、加购物车、购买,对应取值分别是1、2、3、4。 |
user_geohash | 用户位置的空间标识,可以为空 | 由经纬度通过保密的算法生成 |
item_category | 商品分类标识 | 字段脱敏 |
Time | 行为时间 | 精确到小时级别 |
商品子集(P),表名为tianchi_mobile_recommend_train_item,包含如下字段:
字段 | 字段说明 | 提取说明 |
item_id | 商品标识 | 抽样&字段脱敏 |
item_ geohash | 商品位置的空间标识,可以为空 | 由经纬度通过保密的算法生成 |
item_category | 商品分类标识 | 字段脱敏 |
其中,预测的用户为数据(D)中用户,预测的备选商品数据(P)为数据(D)中商品的子集。预测用户在12月19日有可能会购买多个不同备选商品。
输出表名为:tianchi_mobile_recommendation_predict,结果表样例如下:
字段 | 字段类型 | 示例 |
User_id | String | 100001 |
Item_id | String | 12900 |
2.2 解题思路
显然,该问题可以转为一个二分类问题:某个用户对某个商品是否会购买?
l 分类方法:二分类
最终只有两种结果:1:购买;0:不购买
l 样本选取
样本选取—>有交互(即:用户对商品有行为,包括浏览、收藏、加购、购买)的P子集。
分析在哪一天交互的样本可能会在19号购买?16号,17号,18号?
可以基于18号的购买数据,分析16、17号的行为数据在18号购买的概率。
l 特征
分析什么样的用户在什么时候对什么样的商品有过什么样的操作之后,可能会在19号购买?比如:是否曾经买过?商品销量?加购物车?
l 模型
分类、回归。如LR、RF、GBDT等。
3 具体操作(使用的是平台赛的数据)
给出一定量用户在11.18~12.18之内的移动端行为数据(D),需要预测12.19对商品子集(P)的购买数据。行为分为四种:1(浏览),2(收藏),3(加购物车),4(购买)。
首先,由于赛题数据来源于其他project(项目空间),为方便起见及后续在算法平台(PAI)中使用,可以考虑将项目表拷贝到本队伍所在项目空间。
SQL如下:
Create tabletianchi_mobile_recommend_train_user as
select *from tianchi_data. tianchi_mobile_recommend_train_user;
Createtable tianchi_mobile_recommend_train_item as
select *from tianchi_data. tianchi_mobile_recommend_train_item;
下面我们就可以在IDE利用ODPS SQL进行相关的数据统计:
l 统计D集合的数量:
selectcount (*) from tianchi_mobile_recommend_train_user;
---包含了58亿用户的行为记录。
l 统计针对P子集的用户行为数据 :
Createtablet_mj_p_useras
select
t.*,b.item_geohash fromtianchi_mobile_recommend_train_user as t
jointianchi_mobile_recommend_train_itemas bonb.item_id=t.item_idandb.item_category=t.item_category;
selectcount(1) from t_mj_p_user;
---10.7亿
l 统计所给数据中每天各种行为类型的记录数,了解每天会有多少浏览、收藏、加购、购买,观察是否有波动:
select
substr(time,1, 10) as day
,behavior_type
,count(1) cnt
from
tianchi_mobile_recommend_train_user
group by
substr(time,1, 10)
,behavior_type;
从用户的购买行为动机和流程来看,人们通常在购买时会经历浏览→收藏/加到购物车→下单付款这一系列动作,有的可能会搜索后就直接购买,有的可能会操作得多一点,反复挑选和比较。如果说用户都已经把宝贝加到购物车了,那么购买的概率会不会更高?最近加到购物车的是不是应该更有可能被购买?基于这个设想,下面我们示范下如何在数据平台上进行这个分析,并完成结果提交。这是一个通过简单规则来判断购买从而提交结果的示例,较为简单粗暴,但可以让我们先熟悉下从拿到课题到提交结果的整个流程,以及初尝结果验证设想。
1) 由于所给数据tianchi_mobile_recommend_train_user为用户在所有商品类目上的行为,而要预测的是在商品子集P上的购买,故先锁定在P上有过交互的用户行为:
Createtablet_mj_p_useras
select
t.*,b.item_geohash fromtianchi_mobile_recommend_train_usert
join
tianchi_mobile_recommend_train_itembonb.item_id=t.item_idandb.item_category=t.item_category;
selectcount(1) from t_mj_p_user;
------1074139328 (用户对P子集商品的交互记录数)
selectcount(1) from t_mj_p_user where behavior_type=4;
-----9309353(其中对P子集商品有购买的记录数)
selectcount(1) from
(select
user_id
,item_id
,count(1) cnt
from
t_mj_p_user
group by
user_id
,item_id
)t;
-----202430141 [user,item]对
2) 分析前一天加购、收藏、浏览对后一天购买的影响。取12/18为后一天,拿12/17的数据来验证该设想。
createtable t_mj_u_i_1day as
select
user_id
,item_id
,case when substr(time,1,10)='2014-12-17' and behavior_type=1 then 1 else 0 end as is_1day_view
,case when substr(time,1,10)='2014-12-17' and behavior_type=2 then 1 else 0 end as is_1day_fav
,case when substr(time,1,10)='2014-12-17' and behavior_type=3 then 1 else 0 end as is_1day_cart
,case when substr(time,1, 10)='2014-12-17'and behavior_type=4 then 1 else 0 end as is_1day_buy
,case when substr(time,1,10)='2014-12-18' and behavior_type=4 then 1 else 0 end as is_buy
from
t_mj_p_user;
计算评估指标:
select
precision
,recall
,2*precision*recall/(precision+recall) asf1
from
(select
hit_cnt/p_cnt as precision
,hit_cnt/r_cnt as recall
from
(select
sum(if(a.user_id is not null anda.item_id is not null and b.user_id is not null and b.item_id is not null, 1,0)) as hit_cnt --命中
,if(sum(a.r_cnt) is null, 0,sum(a.r_cnt)) as r_cnt --实际
,if(sum(b.p_cnt) is null, 0,sum(b.p_cnt)) as p_cnt --预测
from
(
select user_id,item_id, count(1) asr_cnt
from
t_mj_u_i_1day
where
is_buy=1
and user_id is not null
and item_id is not null
group by user_id, item_id
) a
full outer join
(
select user_id,item_id, count(1) asp_cnt
from
t_mj_u_i_1day
where
is_1day_cart=1
and user_id is not null
and item_id is not null
group by user_id, item_id
) b
on a.user_id = b.user_id anda.item_id = b.item_id
)t
)tb;
返回如下结果,可以看到F1=2.99%,召回率高于准确率,均有较大提升空间
3) 提交结果:
以比赛要求的12/19为预测日期,将用户在12/18对商品子集P加购最多的商品作为预测结果tianchi_mobile_recommendation_predict,生成该表即完成了提交,注:大赛进行的时候,天池的评测系统会自动去扫描各project中指定命名的表(本例中即:tianchi_mobile_recommendation_predict)并评测。
droptable if exists tianchi_mobile_recommendation_predict;
createtable tianchi_mobile_recommendation_predict
as
select
user_id
,item_id
from
(
select
user_id
,item_id
,row_number() over(partition byuser_id order by num desc) as rank
from
(
select
a.user_id
,a.item_id
,a.num
from
(
select
user_id
,item_id
,count(1) as num
from
tianchi_mobile_recommend_train_user
where
substr(time, 1,10)='2014-12-18'
and behavior_type=3
group by
user_id
,item_id
) a
join
(
select distinct
item_id
from
tianchi_mobile_recommend_train_item
) b
on a.item_id = b.item_id
where
b.item_id is not null
) c
) d
where rank<=1;
counttianchi_mobile_recommendation_predict;
-----166068(与上述数据分析时所统计的每日购买数据量相仿)
基于这个提交的结果,可以拿到这样一个分数:准确率=1%, 召回率=0.9%,F1=0.96%。
这是是最简单的一个思路,具体的规则大家可以自己整理~~~~
欢迎分享
转载请标明
(机器学习算法与Python学习)