这是是最简单的一个思路,具体的规则大家可以自己整理~~~~
1.1课题介绍
本课题以阿里巴巴移动电商平台的真实用户-商品行为数据为基础,同时提供移动时代特有的位置信息。您需要通过大数据和算法构建面向移动电子商务的商品推荐模型,挖掘数据背后丰富的内涵,为移动用户在合适的时间、合适的地点精准推荐合适的内容。
在真实的业务场景下,我们往往需要对所有商品的一个子集构建个性化推荐模型。在完成这件任务的过程中,我们不仅需要利用用户在这个商品子集上的行为数据,往往还需要利用更丰富的用户行为数据。
如下是一个真实的业务场景:
给出一定量用户在时间段11月18日~12月18日内的移动端行为数据(D),
需要预测12月19日用户对商品子集(P)的购买数据。具体的数据说明下面将会详细介绍。
1.1数据说明
提供的数据包含两个部分。第一部分是用户在商品全集上的移动端行为数据(D),表名为tianchi_mobile_recommend_train_user,包含如下字段:
每一行代表了用户 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,包含如下字段:
其中PredictionSet为算法预测的购买数据集合,ReferenceSet为真实的答案购买数据集合。我们以F1值作为最终的唯一评测标准。
2 解答
2.1题目解读
已知用户在时间段11月18日~12月18日内的移动端行为数据(D),预测12月19日用户对商品子集(P)的购买数据。数据(D),表名为tianchi_mobile_recommend_train_user,包含如下字段:
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%。
这是是最简单的一个思路,具体的规则大家可以自己整理~~~~
查看更多关于天池新人实战赛之[离线赛](完整)的详细内容...