一、理解赛题
1.1 、赛题背景
赛题:零基础入门推荐系统 - 新闻推荐
赛题以新闻APP中的新闻推荐为背景,要求选手根据用户历史浏览点击新闻文章的数据信息预测用户未来点击行为,即用户的最后一次点击的新闻文章,测试集对最后一次点击行为进行了剔除。
1.2、赛题目标
预测用户点击新闻文章Top5的article_id依概率从高到低排序
1.3、数据概况
数据说明:数据已经给出了几张表,已经将数据划分了训练集和测试集了。
其中数据段的说明我们需要明确知道哪些字段代表这什么意思。这样才有助于我们对后面的数据理解。
1.4 、评估指标
MRR(Mean Reciprocal Rank):首先对选手提交的表格中的每个用户计算用户得分
其中, 如果选手对该user的预测结果predict k命中该user的最后一条购买数据则s(user,k)=1; 否则s(user,k)=0 。而选手得分为所有这些score(user)的平均值。
二、数据初探
- 导入相关的包
%matplotlib inline import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns plt.rc('font', family='SimHei', size=13) import os,gc,re,warnings,sys warnings.filterwarnings("ignore")
- 读取数据
将下载的数据放在data_raw下
# path = './data/' # 自定义的路径 path='./data_raw/' trn_click = pd.read_csv(path+'train_click_log.csv') #trn_click = pd.read_csv(path+'train_click_log.csv', names=['user_id','item_id','click_time','click_environment','click_deviceGroup','click_os','click_country','click_region','click_referrer_type']) item_df = pd.read_csv(path+'articles.csv') item_df = item_df.rename(columns={'article_id': 'click_article_id'}) #重命名,方便后续match item_emb_df = pd.read_csv(path+'articles_emb.csv') #####test tst_click = pd.read_csv(path+'testA_click_log.csv')
- 数据探索
查看相关字段
trn_click.head()
user_id | click_article_id | click_timestamp | click_environment | click_deviceGroup | click_os | click_country | click_region | click_referrer_type | |
0 | 199999 | 160417 | 1507029570190 | 4 | 1 | 17 | 1 | 13 | 1 |
1 | 199999 | 5408 | 1507029571478 | 4 | 1 | 17 | 1 | 13 | 1 |
2 | 199999 | 50823 | 1507029601478 | 4 | 1 | 17 | 1 | 13 | 1 |
3 | 199998 | 157770 | 1507029532200 | 4 | 1 | 17 | 1 | 25 | 5 |
4 | 199998 | 96613 | 1507029671831 | 4 | 1 | 17 | 1 | 25 | 5 |
item_df.head()
click_article_id | category_id | created_at_ts | words_count | |
0 | 0 | 0 | 1513144419000 | 168 |
1 | 1 | 1 | 1405341936000 | 189 |
2 | 2 | 1 | 1408667706000 | 250 |
3 | 3 | 1 | 1408468313000 | 230 |
4 | 4 | 1 | 1407071171000 | 162 |
item_emb_df.head()
article_id | emb_0 | emb_1 | emb_2 | emb_3 | emb_4 | emb_5 | emb_6 | emb_7 | emb_8 | ... | emb_240 | emb_241 | emb_242 | emb_243 | emb_244 | emb_245 | emb_246 | emb_247 | emb_248 | emb_249 | |
0 | 0 | -0.161183 | -0.957233 | -0.137944 | 0.050855 | 0.830055 | 0.901365 | -0.335148 | -0.559561 | -0.500603 | ... | 0.321248 | 0.313999 | 0.636412 | 0.169179 | 0.540524 | -0.813182 | 0.286870 | -0.231686 | 0.597416 | 0.409623 |
1 | 1 | -0.523216 | -0.974058 | 0.738608 | 0.155234 | 0.626294 | 0.485297 | -0.715657 | -0.897996 | -0.359747 | ... | -0.487843 | 0.823124 | 0.412688 | -0.338654 | 0.320786 | 0.588643 | -0.594137 | 0.182828 | 0.397090 | -0.834364 |
2 | 2 | -0.619619 | -0.972960 | -0.207360 | -0.128861 | 0.044748 | -0.387535 | -0.730477 | -0.066126 | -0.754899 | ... | 0.454756 | 0.473184 | 0.377866 | -0.863887 | -0.383365 | 0.137721 | -0.810877 | -0.447580 | 0.805932 | -0.285284 |
3 | 3 | -0.740843 | -0.975749 | 0.391698 | 0.641738 | -0.268645 | 0.191745 | -0.825593 | -0.710591 | -0.040099 | ... | 0.271535 | 0.036040 | 0.480029 | -0.763173 | 0.022627 | 0.565165 | -0.910286 | -0.537838 | 0.243541 | -0.885329 |
4 | 4 | -0.279052 | -0.972315 | 0.685374 | 0.113056 | 0.238315 | 0.271913 | -0.568816 | 0.341194 | -0.600554 | ... | 0.238286 | 0.809268 | 0.427521 | -0.615932 | -0.503697 | 0.614450 | -0.917760 | -0.424061 | 0.185484 | -0.580292 |
5 rows × 251 columns
'运行 运行
- 数据预处理
计算用户点击rank和点击次数
对每个用户的点击时间戳进行排序,降序排列
trn_click['rank'] = trn_click.groupby(['user_id'])['click_timestamp'].rank(ascending=False).astype(int) tst_click['rank'] = tst_click.groupby(['user_id'])['click_timestamp'].rank(ascending=False).astype(int)
计算用户点击文章的次数,并添加新的一列count
trn_click['click_cnts'] = trn_click.groupby(['user_id'])['click_timestamp'].transform('count') tst_click['click_cnts'] = tst_click.groupby(['user_id'])['click_timestamp'].transform('count')
trn_click.groupby(['user_id'])['click_timestamp'].transform('count') • 1
0 11 1 11 2 11 3 40 4 40 .. 1112618 4 1112619 2 1112620 2 1112621 14 1112622 14 Name: click_timestamp, Length: 1112623, dtype: int64
trn_click.groupby(['user_id'])['click_timestamp'].agg('count')
user_id 0 2 1 2 2 2 3 2 4 2 .. 199995 7 199996 13 199997 2 199998 40 199999 11 Name: click_timestamp, Length: 200000, dtype: int64
trn_click = trn_click.merge(item_df, how='left', on=['click_article_id']) trn_click.head()
user_id | click_article_id | click_timestamp | click_environment | click_deviceGroup | click_os | click_country | click_region | click_referrer_type | rank | click_cnts | category_id_x | created_at_ts_x | words_count_x | category_id_y | created_at_ts_y | words_count_y | |
0 | 199999 | 160417 | 1507029570190 | 4 | 1 | 17 | 1 | 13 | 1 | 11 | 11 | 281 | 1506942089000 | 173 | 281 | 1506942089000 | 173 |
1 | 199999 | 5408 | 1507029571478 | 4 | 1 | 17 | 1 | 13 | 1 | 10 | 11 | 4 | 1506994257000 | 118 | 4 | 1506994257000 | 118 |
2 | 199999 | 50823 | 1507029601478 | 4 | 1 | 17 | 1 | 13 | 1 | 9 | 11 | 99 | 1507013614000 | 213 | 99 | 1507013614000 | 213 |
3 | 199998 | 157770 | 1507029532200 | 4 | 1 | 17 | 1 | 25 | 5 | 40 | 40 | 281 | 1506983935000 | 201 | 281 | 1506983935000 | 201 |
4 | 199998 | 96613 | 1507029671831 | 4 | 1 | 17 | 1 | 25 | 5 | 39 | 40 | 209 | 1506938444000 | 185 | 209 | 1506938444000 | 185 |
train_click_log.csv文件数据中每个字段的含义
- user_id: 用户的唯一标识
- click_article_id: 用户点击的文章唯一标识
- click_timestamp: 用户点击文章时的时间戳
- click_environment: 用户点击文章的环境
- click_deviceGroup: 用户点击文章的设备组
- click_os: 用户点击文章时的操作系统
- click_country: 用户点击文章时的所在的国家
- click_region: 用户点击文章时所在的区域
- click_referrer_type: 用户点击文章时,文章的来源
trn_click.info()
<class 'pandas.core.frame.DataFrame'> Int64Index: 1112623 entries, 0 to 1112622 Data columns (total 17 columns): user_id 1112623 non-null int64 click_article_id 1112623 non-null int64 click_timestamp 1112623 non-null int64 click_environment 1112623 non-null int64 click_deviceGroup 1112623 non-null int64 click_os 1112623 non-null int64 click_country 1112623 non-null int64 click_region 1112623 non-null int64 click_referrer_type 1112623 non-null int64 rank 1112623 non-null int32 click_cnts 1112623 non-null int64 category_id_x 1112623 non-null int64 created_at_ts_x 1112623 non-null int64 words_count_x 1112623 non-null int64 category_id_y 1112623 non-null int64 created_at_ts_y 1112623 non-null int64 words_count_y 1112623 non-null int64 dtypes: int32(1), int64(16) memory usage: 148.6 MB
查看训练点击表基本描述
trn_click.describe([0.01,0.25,0.5,0.75,0.99])
user_id | click_article_id | click_timestamp | click_environment | click_deviceGroup | click_os | click_country | click_region | click_referrer_type | rank | click_cnts | category_id_x | created_at_ts_x | words_count_x | category_id_y | created_at_ts_y | words_count_y | |
count | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 | 1.112623e+06 |
mean | 1.221198e+05 | 1.951541e+05 | 1.507588e+12 | 3.947786e+00 | 1.815981e+00 | 1.301976e+01 | 1.310776e+00 | 1.813587e+01 | 1.910063e+00 | 7.118518e+00 | 1.323704e+01 | 3.056176e+02 | 1.506598e+12 | 2.011981e+02 | 3.056176e+02 | 1.506598e+12 | 2.011981e+02 |
std | 5.540349e+04 | 9.292286e+04 | 3.363466e+08 | 3.276715e-01 | 1.035170e+00 | 6.967844e+00 | 1.618264e+00 | 7.105832e+00 | 1.220012e+00 | 1.016095e+01 | 1.631503e+01 | 1.155791e+02 | 8.343066e+09 | 5.223881e+01 | 1.155791e+02 | 8.343066e+09 | 5.223881e+01 |
min | 0.000000e+00 | 3.000000e+00 | 1.507030e+12 | 1.000000e+00 | 1.000000e+00 | 2.000000e+00 | 1.000000e+00 | 1.000000e+00 | 1.000000e+00 | 1.000000e+00 | 2.000000e+00 | 1.000000e+00 | 1.166573e+12 | 0.000000e+00 | 1.000000e+00 | 1.166573e+12 | 0.000000e+00 |
1% | 4.502000e+03 | 1.520900e+04 | 1.507039e+12 | 2.000000e+00 | 1.000000e+00 | 2.000000e+00 | 1.000000e+00 | 3.000000e+00 | 1.000000e+00 | 1.000000e+00 | 2.000000e+00 | 7.000000e+00 | 1.476077e+12 | 9.700000e+01 | 7.000000e+00 | 1.476077e+12 | 9.700000e+01 |
25% | 7.934700e+04 | 1.239090e+05 | 1.507297e+12 | 4.000000e+00 | 1.000000e+00 | 2.000000e+00 | 1.000000e+00 | 1.300000e+01 | 1.000000e+00 | 2.000000e+00 | 4.000000e+00 | 2.500000e+02 | 1.507220e+12 | 1.700000e+02 | 2.500000e+02 | 1.507220e+12 | 1.700000e+02 |
50% | 1.309670e+05 | 2.038900e+05 | 1.507596e+12 | 4.000000e+00 | 1.000000e+00 | 1.700000e+01 | 1.000000e+00 | 2.100000e+01 | 2.000000e+00 | 4.000000e+00 | 8.000000e+00 | 3.280000e+02 | 1.507553e+12 | 1.970000e+02 | 3.280000e+02 | 1.507553e+12 | 1.970000e+02 |
75% | 1.704010e+05 | 2.777120e+05 | 1.507841e+12 | 4.000000e+00 | 3.000000e+00 | 1.700000e+01 | 1.000000e+00 | 2.500000e+01 | 2.000000e+00 | 8.000000e+00 | 1.600000e+01 | 4.100000e+02 | 1.507756e+12 | 2.280000e+02 | 4.100000e+02 | 1.507756e+12 | 2.280000e+02 |
99% | 1.990788e+05 | 3.540860e+05 | 1.508197e+12 | 4.000000e+00 | 4.000000e+00 | 2.000000e+01 | 1.000000e+01 | 2.800000e+01 | 7.000000e+00 | 4.900000e+01 | 8.000000e+01 | 4.420000e+02 | 1.508171e+12 | 3.180000e+02 | 4.420000e+02 | 1.508171e+12 | 3.180000e+02 |
max | 1.999990e+05 | 3.640460e+05 | 1.510603e+12 | 4.000000e+00 | 5.000000e+00 | 2.000000e+01 | 1.100000e+01 | 2.800000e+01 | 7.000000e+00 | 2.410000e+02 | 2.410000e+02 | 4.600000e+02 | 1.510666e+12 | 6.690000e+03 | 4.600000e+02 | 1.510666e+12 | 6.690000e+03 |
#训练集中的用户数量为20w trn_click["user_id"].nunique()
200000 • 1
trn_click.groupby('user_id')['click_article_id'].count().min() # 训练集里面每个用户至少点击了两篇文章
2
trn_click.groupby('user_id')['click_article_id'].count()
user_id 0 2 1 2 2 2 3 2 4 2 .. 199995 7 199996 13 199997 2 199998 40 199999 11 Name: click_article_id, Length: 200000, dtype: int64
三 、Baseline代码实现及理解
import time, math, os from tqdm import tqdm import gc import pickle import random from datetime import datetime from operator import itemgetter import numpy as np import pandas as pd import warnings import collections from collections import defaultdict warnings.filterwarnings('ignore')
data_path = './data_raw/' save_path = './tmp_results/' • 1 • 2 '运行 运行
这是一个节约内存的函数,将有些数的字节没有必要这么大,将其字节给变小,可以在一定程度上减少内存消耗。
# 节约内存的一个标配函数 def reduce_mem(df): starttime = time.time() numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64'] start_mem = df.memory_usage().sum() / 1024**2 for col in df.columns: col_type = df[col].dtypes if col_type in numerics: c_min = df[col].min() c_max = df[col].max() if pd.isnull(c_min) or pd.isnull(c_max): continue if str(col_type)[:3] == 'int': if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max: df[col] = df[col].astype(np.int8) elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max: df[col] = df[col].astype(np.int16) elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max: df[col] = df[col].astype(np.int32) elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max: df[col] = df[col].astype(np.int64) else: if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max: df[col] = df[col].astype(np.float16) elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max: df[col] = df[col].astype(np.float32) else: df[col] = df[col].astype(np.float64) end_mem = df.memory_usage().sum() / 1024**2 print('-- Mem. usage decreased to {:5.2f} Mb ({:.1f}% reduction),time spend:{:2.2f} min'.format(end_mem, 100*(start_mem-end_mem)/start_mem, (time.time()-starttime)/60)) return df
# debug模式:从训练集中划出一部分数据来调试代码 def get_all_click_sample(data_path, sample_nums=5000): """ 训练集中采样一部分数据调试 data_path: 原数据的存储路径 sample_nums: 采样数目(这里由于机器的内存限制,可以采样用户做) """ all_click = pd.read_csv(data_path + 'train_click_log.csv') all_user_ids = all_click.user_id.unique() sample_user_ids = np.random.choice(all_user_ids, size=sample_nums, replace=False) all_click = all_click[all_click['user_id'].isin(sample_user_ids)] all_click = all_click.drop_duplicates((['user_id', 'click_article_id', 'click_timestamp'])) return all_click
读取点击数据,这里分成线上和线下,如果是为了获取线上提交结果应该讲测试集中的点击数据合并到总的数据中
# 读取点击数据,这里分成线上和线下,如果是为了获取线上提交结果应该讲测试集中的点击数据合并到总的数据中 # 如果是为了线下验证模型的有效性或者特征的有效性,可以只使用训练集 def get_all_click_df(data_path='./data_raw/', offline=True): if offline: all_click = pd.read_csv(data_path + 'train_click_log.csv') else: trn_click = pd.read_csv(data_path + 'train_click_log.csv') tst_click = pd.read_csv(data_path + 'testA_click_log.csv') all_click = trn_click.append(tst_click) all_click = all_click.drop_duplicates((['user_id', 'click_article_id', 'click_timestamp'])) return all_click '运行 运行
# 全量训练集因为已经分成两部分数据集,可以是读取合并成一大部分数据集 all_click_df = get_all_click_df(offline=False)
# 根据点击时间获取用户的点击文章序列 {user1: [(item1, time1), (item2, time2)..]...} def get_user_item_time(click_df): click_df = click_df.sort_values('click_timestamp') def make_item_time_pair(df): return list(zip(df['click_article_id'], df['click_timestamp'])) user_item_time_df = click_df.groupby('user_id')['click_article_id', 'click_timestamp'].apply(lambda x: make_item_time_pair(x))\ .reset_index().rename(columns={0: 'item_time_list'}) user_item_time_dict = dict(zip(user_item_time_df['user_id'], user_item_time_df['item_time_list'])) return user_item_time_dict '运行 运行
# 获取近期点击最多的文章 def get_item_topk_click(click_df, k): topk_click = click_df['click_article_id'].value_counts().index[:k] return topk_click '运行 运行
def itemcf_sim(df): """ 文章与文章之间的相似性矩阵计算 :param df: 数据表 :item_created_time_dict: 文章创建时间的字典 return : 文章与文章的相似性矩阵 思路: 基于物品的协同过滤(详细请参考上一期推荐系统基础的组队学习), 在多路召回部分会加上关联规则的召回策略 """ user_item_time_dict = get_user_item_time(df) # 计算物品相似度 i2i_sim = {} item_cnt = defaultdict(int) for user, item_time_list in tqdm(user_item_time_dict.items()): # 在基于商品的协同过滤优化的时候可以考虑时间因素 for i, i_click_time in item_time_list: item_cnt[i] += 1 i2i_sim.setdefault(i, {}) for j, j_click_time in item_time_list: if(i == j): continue i2i_sim[i].setdefault(j, 0) i2i_sim[i][j] += 1 / math.log(len(item_time_list) + 1) i2i_sim_ = i2i_sim.copy() for i, related_items in i2i_sim.items(): for j, wij in related_items.items(): i2i_sim_[i][j] = wij / math.sqrt(item_cnt[i] * item_cnt[j]) # 将得到的相似性矩阵保存到本地 pickle.dump(i2i_sim_, open(save_path + 'itemcf_i2i_sim.pkl', 'wb')) return i2i_sim_5 '运行 运行
i2i_sim = itemcf_sim(all_click_df)
# 基于商品的召回i2i def item_based_recommend(user_id, user_item_time_dict, i2i_sim, sim_item_topk, recall_item_num, item_topk_click): """ 基于文章协同过滤的召回 :param user_id: 用户id :param user_item_time_dict: 字典, 根据点击时间获取用户的点击文章序列{user1: [(item1, time1), (item2, time2)..]...} :param i2i_sim: 字典,文章相似性矩阵 :param sim_item_topk: 整数, 选择与当前文章最相似的前k篇文章 :param recall_item_num: 整数, 最后的召回文章数量 :param item_topk_click: 列表,点击次数最多的文章列表,用户召回补全 return: 召回的文章列表 [item1:score1, item2: score2...] 注意: 基于物品的协同过滤(详细请参考上一期推荐系统基础的组队学习), 在多路召回部分会加上关联规则的召回策略 """ # 获取用户历史交互的文章 user_hist_items = user_item_time_dict[user_id] # 注意,此时获取得到的是一个元组列表,需要将里面的user_id提取出来 user_hist_items_ = {user_id for user_id, _ in user_hist_items} item_rank = {} for loc, (i, click_time) in enumerate(user_hist_items): for j, wij in sorted(i2i_sim[i].items(), key=lambda x: x[1], reverse=True)[:sim_item_topk]: if j in user_hist_items_: continue item_rank.setdefault(j, 0) item_rank[j] += wij # 不足10个,用热门商品补全 if len(item_rank) < recall_item_num: for i, item in enumerate(item_topk_click): if item in item_rank.items(): # 填充的item应该不在原来的列表中 continue item_rank[item] = - i - 100 # 随便给个负数就行 if len(item_rank) == recall_item_num: break item_rank = sorted(item_rank.items(), key=lambda x: x[1], reverse=True)[:recall_item_num] return item_rank '运行 运行
# 定义 user_recall_items_dict = collections.defaultdict(dict)
# 获取 用户 - 文章 - 点击时间的字典 user_item_time_dict = get_user_item_time(all_click_df)
# 去取文章相似度 i2i_sim = pickle.load(open(save_path + 'itemcf_i2i_sim.pkl', 'rb'))
• 1 '运行 运行
# 相似文章的数量 sim_item_topk = 10 # 召回文章数量 recall_item_num = 10 # 用户热度补全 item_topk_click = get_item_topk_click(all_click_df, k=50)
• 1 '运行 运行
for user in tqdm(all_click_df['user_id'].unique()): user_recall_items_dict[user] = item_based_recommend(user, user_item_time_dict, i2i_sim, 、 sim_item_topk, recall_item_num, item_topk_click)
100%|████████████████████████████████████████████████████████████████████████| 250000/250000 [2:06:25<00:00, 32.96it/s]
# 将字典的形式转换成df user_item_score_list = [] for user, items in tqdm(user_recall_items_dict.items()): for item, score in items: user_item_score_list.append([user, item, score]) recall_df = pd.DataFrame(user_item_score_list, columns=['user_id', 'click_article_id', 'pred_score'])
100%|███████████████████████████████████████████████████████████████████████| 250000/250000 [00:11<00:00, 22018.74it/s]
# 生成提交文件 def submit(recall_df, topk=5, model_name=None): recall_df = recall_df.sort_values(by=['user_id', 'pred_score']) recall_df['rank'] = recall_df.groupby(['user_id'])['pred_score'].rank(ascending=False, method='first') # 判断是不是每个用户都有5篇文章及以上 tmp = recall_df.groupby('user_id').apply(lambda x: x['rank'].max()) assert tmp.min() >= topk del recall_df['pred_score'] submit = recall_df[recall_df['rank'] <= topk].set_index(['user_id', 'rank']).unstack(-1).reset_index() submit.columns = [int(col) if isinstance(col, int) else col for col in submit.columns.droplevel(0)] # 按照提交格式定义列名 submit = submit.rename(columns={'': 'user_id', 1: 'article_1', 2: 'article_2', 3: 'article_3', 4: 'article_4', 5: 'article_5'}) save_name = save_path + model_name + '_' + datetime.today().strftime('%m-%d') + '.csv' submit.to_csv(save_name, index=False, header=True) '运行 运行
• 1 '运行 运行
# 获取测试集 tst_click = pd.read_csv(data_path + 'testA_click_log.csv') tst_users = tst_click['user_id'].unique() # 从所有的召回数据中将测试集中的用户选出来 tst_recall = recall_df[recall_df['user_id'].isin(tst_users)] # 生成提交文件 submit(tst_recall, topk=5, model_name='itemcf_baseline')
最后产生文件提交即可。另外本文如有错误望您指正。感谢
baseline 最后Score:0.1026