零基础入门推荐系统 - 新闻推荐 (1)
本文是跟随Datawhale组队学习推荐系统笔记。以天池零基础入门推荐系统 - 新闻推荐比赛为例,学习推荐系统相关内容。
原文地址:https://tianchi.aliyun.com/notebook-ai/detail?spm=5176.12586969.1002.15.4105d75fc3WKWF&postId=144449
赛题理解
赛题以预测用户未来点击新闻文章为任务,该数据来自某新闻APP平台的用户交互数据,包括30万用户,近300万次点击,共36万多篇不同的新闻文章,同时每篇新闻文章有对应的embedding向量表示。为了保证比赛的公平性,将会从中抽取20万用户的点击日志数据作为训练集,5万用户的点击日志数据作为测试集A,5万用户的点击日志数据作为测试集B。
此次比赛目标:根据用户历史浏览点击新闻的数据信息预测用户最后一次点击的新闻文章。可以预测用户最后一次点击某文章的概率,概率最大的就是最可能点击的新闻文章。
这样就把问题转变为点击率预测的问题(用户,文章)–>点击的概率。
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
from collections import defaultdict
import collections
warnings.filterwarnings('ignore')
data_path='d:/data/'
save_path='d:/data/temp_results/'
df节省内存函数
# 节约内存的一个标配函数
def reduce_mem(df):
starttime = time.time()
numeric = ['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