一、新闻推荐之数据探索与数据分析

新闻推荐之数据探索与数据分析

导入相关的包

%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")

读取数据

# path = './data/' # 自定义的路径
path='./data_raw/'
trn_click = pd.read_csv(path+'train_click_log.csv')
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_idclick_article_idclick_timestampclick_environmentclick_deviceGroupclick_osclick_countryclick_regionclick_referrer_type
0199999160417150702957019041171131
11999995408150702957147841171131
219999950823150702960147841171131
3199998157770150702953220041171255
419999896613150702967183141171255
item_df.head()
click_article_idcategory_idcreated_at_tswords_count
0001513144419000168
1111405341936000189
2211408667706000250
3311408468313000230
4411407071171000162
item_emb_df.head()
article_idemb_0emb_1emb_2emb_3emb_4emb_5emb_6emb_7emb_8...emb_240emb_241emb_242emb_243emb_244emb_245emb_246emb_247emb_248emb_249
00-0.161183-0.957233-0.1379440.0508550.8300550.901365-0.335148-0.559561-0.500603...0.3212480.3139990.6364120.1691790.540524-0.8131820.286870-0.2316860.5974160.409623
11-0.523216-0.9740580.7386080.1552340.6262940.485297-0.715657-0.897996-0.359747...-0.4878430.8231240.412688-0.3386540.3207860.588643-0.5941370.1828280.397090-0.834364
22-0.619619-0.972960-0.207360-0.1288610.044748-0.387535-0.730477-0.066126-0.754899...0.4547560.4731840.377866-0.863887-0.3833650.137721-0.810877-0.4475800.805932-0.285284
33-0.740843-0.9757490.3916980.641738-0.2686450.191745-0.825593-0.710591-0.040099...0.2715350.0360400.480029-0.7631730.0226270.565165-0.910286-0.5378380.243541-0.885329
44-0.279052-0.9723150.6853740.1130560.2383150.271913-0.5688160.341194-0.600554...0.2382860.8092680.427521-0.615932-0.5036970.614450-0.917760-0.4240610.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')
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_idclick_article_idclick_timestampclick_environmentclick_deviceGroupclick_osclick_countryclick_regionclick_referrer_typerankclick_cntscategory_idcreated_at_tswords_count
019999916041715070295701904117113111112811506942089000173
11999995408150702957147841171131101141506994257000118
219999950823150702960147841171131911991507013614000213
319999815777015070295322004117125540402811506983935000201
41999989661315070296718314117125539402091506938444000185

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 14 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            1112623 non-null int64
created_at_ts          1112623 non-null int64
words_count            1112623 non-null int64
dtypes: int32(1), int64(13)
memory usage: 123.1 MB
trn_click.describe([0.01,0.25,0.5,0.75,0.99])
user_idclick_article_idclick_timestampclick_environmentclick_deviceGroupclick_osclick_countryclick_regionclick_referrer_typerankclick_cntscategory_idcreated_at_tswords_count
count1.112623e+061.112623e+061.112623e+061.112623e+061.112623e+061.112623e+061.112623e+061.112623e+061.112623e+061.112623e+061.112623e+061.112623e+061.112623e+061.112623e+06
mean1.221198e+051.951541e+051.507588e+123.947786e+001.815981e+001.301976e+011.310776e+001.813587e+011.910063e+007.118518e+001.323704e+013.056176e+021.506598e+122.011981e+02
std5.540349e+049.292286e+043.363466e+083.276715e-011.035170e+006.967844e+001.618264e+007.105832e+001.220012e+001.016095e+011.631503e+011.155791e+028.343066e+095.223881e+01
min0.000000e+003.000000e+001.507030e+121.000000e+001.000000e+002.000000e+001.000000e+001.000000e+001.000000e+001.000000e+002.000000e+001.000000e+001.166573e+120.000000e+00
1%4.502000e+031.520900e+041.507039e+122.000000e+001.000000e+002.000000e+001.000000e+003.000000e+001.000000e+001.000000e+002.000000e+007.000000e+001.476077e+129.700000e+01
25%7.934700e+041.239090e+051.507297e+124.000000e+001.000000e+002.000000e+001.000000e+001.300000e+011.000000e+002.000000e+004.000000e+002.500000e+021.507220e+121.700000e+02
50%1.309670e+052.038900e+051.507596e+124.000000e+001.000000e+001.700000e+011.000000e+002.100000e+012.000000e+004.000000e+008.000000e+003.280000e+021.507553e+121.970000e+02
75%1.704010e+052.777120e+051.507841e+124.000000e+003.000000e+001.700000e+011.000000e+002.500000e+012.000000e+008.000000e+001.600000e+014.100000e+021.507756e+122.280000e+02
99%1.990788e+053.540860e+051.508197e+124.000000e+004.000000e+002.000000e+011.000000e+012.800000e+017.000000e+004.900000e+018.000000e+014.420000e+021.508171e+123.180000e+02
max1.999990e+053.640460e+051.510603e+124.000000e+005.000000e+002.000000e+011.100000e+012.800000e+017.000000e+002.410000e+022.410000e+024.600000e+021.510666e+126.690000e+03
trn_click.head()
user_idclick_article_idclick_timestampclick_environmentclick_deviceGroupclick_osclick_countryclick_regionclick_referrer_typerankclick_cntscategory_idcreated_at_tswords_count
019999916041715070295701904117113111112811506942089000173
11999995408150702957147841171131101141506994257000118
219999950823150702960147841171131911991507013614000213
319999815777015070295322004117125540402811506983935000201
41999989661315070296718314117125539402091506938444000185
columns=trn_click.columns.to_list()
len(columns)
14
fig=plt.figure(figsize=(80,60))
for i in range(14):
    plt.subplot(4,4,i+1)
    sns.boxplot(trn_click[columns[i]],orient='v',width=0.5)
    plt.ylabel(columns[i],fontsize=36)
plt.show()
#训练集中的用户数量为20w
trn_click["user_id"].nunique()
200000
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
trn_click['click_article_id'].value_counts().reset_index()[:10]
indexclick_article_id
023469811886
112390911438
233622111290
39621011146
418317610487
533622310186
61686239882
73311169369
83362457851
91247497264
plt.figure()
plt.figure(figsize=(15, 20))
i = 1
for col in ['click_article_id', 'click_timestamp', 'click_environment', 'click_deviceGroup', 'click_os', 'click_country', 
            'click_region', 'click_referrer_type', 'rank', 'click_cnts']:
    plot_envs = plt.subplot(5, 2, i)
    i += 1
    v = trn_click[col].value_counts().reset_index()[:10]
    fig = sns.barplot(x=v['index'], y=v[col])
    for item in fig.get_xticklabels():
        item.set_rotation(90)
    plt.title(col)
plt.tight_layout()
plt.show()
<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Jp00f6N-1606484105249)(output_26_1.png)]

从点击时间clik_timestamp来看,分布较为平均,可不做特殊处理。由于时间戳是13位的,后续将时间格式转换成10位方便计算。

从点击环境click_environment来看,仅有1922次(占0.1%)点击环境为1;仅有24617次(占2.3%)点击环境为2;剩余(占97.6%)点击环境为4。

从点击设备组click_deviceGroup来看,设备1占大部分(60.4%),设备3占36%。

tst_click = tst_click.merge(item_df, how='left', on=['click_article_id'])
tst_click.head()
user_idclick_article_idclick_timestampclick_environmentclick_deviceGroupclick_osclick_countryclick_regionclick_referrer_typerankclick_cntscategory_idcreated_at_tswords_count
024999916097415069591428204117113219192811506912747000259
124999916041715069591728204117113218192811506942089000173
2249998160974150695905606641121132552811506912747000259
3249998202557150695908606641121132453271506938401000219
4249997183665150695908861341171155773011500895686000256
tst_click.describe([0.01,0.25,0.5,0.75,0.99]).T
countmeanstdmin1%25%50%75%99%max
user_id518010.02.273424e+051.461391e+042.000000e+052.004640e+052.149260e+052.291090e+052.401820e+052.495860e+052.499990e+05
click_article_id518010.01.938038e+058.827939e+041.370000e+021.612900e+041.285510e+051.991970e+052.721430e+053.536720e+053.640430e+05
click_timestamp518010.01.507387e+123.706127e+081.506959e+121.506961e+121.507026e+121.507308e+121.507666e+121.508184e+121.508832e+12
click_environment518010.03.947300e+003.239161e-011.000000e+002.000000e+004.000000e+004.000000e+004.000000e+004.000000e+004.000000e+00
click_deviceGroup518010.01.738285e+001.020858e+001.000000e+001.000000e+001.000000e+001.000000e+003.000000e+004.000000e+005.000000e+00
click_os518010.01.362847e+016.625564e+002.000000e+002.000000e+001.200000e+011.700000e+011.700000e+012.000000e+012.000000e+01
click_country518010.01.348209e+001.703524e+001.000000e+001.000000e+001.000000e+001.000000e+001.000000e+001.000000e+011.100000e+01
click_region518010.01.825025e+017.060798e+001.000000e+004.000000e+001.300000e+012.100000e+012.500000e+012.800000e+012.800000e+01
click_referrer_type518010.01.819614e+001.082657e+001.000000e+001.000000e+001.000000e+002.000000e+002.000000e+007.000000e+007.000000e+00
rank518010.01.552179e+013.395770e+011.000000e+001.000000e+004.000000e+008.000000e+001.800000e+011.000000e+029.380000e+02
click_cnts518010.03.004359e+015.686802e+011.000000e+001.000000e+001.000000e+011.900000e+013.500000e+011.630000e+029.380000e+02
category_id518010.03.053250e+021.104115e+021.000000e+007.000000e+002.520000e+023.230000e+023.990000e+024.420000e+024.600000e+02
created_at_ts518010.01.506883e+125.816668e+091.265812e+121.501765e+121.506970e+121.507249e+121.507630e+121.508156e+121.509949e+12
words_count518010.02.109663e+028.304006e+010.000000e+001.140000e+021.760000e+021.990000e+022.320000e+028.580000e+023.082000e+03
#测试集中的用户数量为5w
tst_click.user_id.nunique()
50000
tst_click.groupby('user_id')['click_article_id'].count().min()
# 注意测试集里面有只点击过一次文章的用户
1

新闻文章信息数据表¶

#新闻文章数据集浏览
item_df.head().append(item_df.tail())
click_article_idcategory_idcreated_at_tswords_count
0001513144419000168
1111405341936000189
2211408667706000250
3311408468313000230
4411407071171000162
3640423640424601434034118000144
3640433640434601434148472000463
3640443640444601457974279000177
3640453640454601515964737000126
3640463640464601505811330000479
item_df['words_count'].value_counts()
176     3485
182     3480
179     3463
178     3458
174     3456
        ... 
845        1
710        1
965        1
847        1
1535       1
Name: words_count, Length: 866, dtype: int64
print(item_df['category_id'].nunique())     # 461个文章主题
item_df['category_id'].hist()
461





<matplotlib.axes._subplots.AxesSubplot at 0x21fb84d4248>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-15pQ2HQh-1606484105253)(output_35_2.png)]

item_df.shape       # 364047篇文章
(364047, 4)

新闻文章embedding向量表示¶

item_emb_df.head()
article_idemb_0emb_1emb_2emb_3emb_4emb_5emb_6emb_7emb_8...emb_240emb_241emb_242emb_243emb_244emb_245emb_246emb_247emb_248emb_249
00-0.161183-0.957233-0.1379440.0508550.8300550.901365-0.335148-0.559561-0.500603...0.3212480.3139990.6364120.1691790.540524-0.8131820.286870-0.2316860.5974160.409623
11-0.523216-0.9740580.7386080.1552340.6262940.485297-0.715657-0.897996-0.359747...-0.4878430.8231240.412688-0.3386540.3207860.588643-0.5941370.1828280.397090-0.834364
22-0.619619-0.972960-0.207360-0.1288610.044748-0.387535-0.730477-0.066126-0.754899...0.4547560.4731840.377866-0.863887-0.3833650.137721-0.810877-0.4475800.805932-0.285284
33-0.740843-0.9757490.3916980.641738-0.2686450.191745-0.825593-0.710591-0.040099...0.2715350.0360400.480029-0.7631730.0226270.565165-0.910286-0.5378380.243541-0.885329
44-0.279052-0.9723150.6853740.1130560.2383150.271913-0.5688160.341194-0.600554...0.2382860.8092680.427521-0.615932-0.5036970.614450-0.917760-0.4240610.185484-0.580292

5 rows × 251 columns

item_emb_df.shape
(364047, 251)

数据分析
用户重复点击

#####merge
user_click_merge = trn_click.append(tst_click)
#用户重复点击
user_click_count = user_click_merge.groupby(['user_id', 'click_article_id'])['click_timestamp'].agg({'count'}).reset_index()
user_click_count[:10]
user_idclick_article_idcount
00307601
101575071
21637461
312891971
42361621
521684011
63361621
73506441
84398941
94425671
user_click_count[user_click_count['count']>7]
user_idclick_article_idcount
311242862957425410
311243862957626810
39376110323720594810
39376310323723568910
5769021348506946313
user_click_count['count'].unique()
array([ 1,  2,  4,  3,  6,  5, 10,  7, 13], dtype=int64)
#用户点击新闻次数
user_click_count.loc[:,'count'].value_counts() 
1     1605541
2       11621
3         422
4          77
5          26
6          12
10          4
7           3
13          1
Name: count, dtype: int64

可以看出:有1605541(约占99.2%)的用户未重复阅读过文章,仅有极少数用户重复点击过某篇文章。 这个也可以单独制作成特征

用户点击环境变化分析¶

def plot_envs(df, cols, r, c):
    plt.figure()
    plt.figure(figsize=(10, 5))
    i = 1
    for col in cols:
        plt.subplot(r, c, i)
        i += 1
        v = df[col].value_counts().reset_index()
        fig = sns.barplot(x=v['index'], y=v[col])
        for item in fig.get_xticklabels():
            item.set_rotation(90)
        plt.title(col)
    plt.tight_layout()
    plt.show()
# 分析用户点击环境变化是否明显,这里随机采样10个用户分析这些用户的点击环境分布
sample_user_ids = np.random.choice(tst_click['user_id'].unique(), size=10, replace=False)
sample_users = user_click_merge[user_click_merge['user_id'].isin(sample_user_ids)]
cols = ['click_environment','click_deviceGroup', 'click_os', 'click_country', 'click_region','click_referrer_type']
for _, user_df in sample_users.groupby('user_id'):
    plot_envs(user_df, cols, 2, 3)
<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nvprotMs-1606484105265)(output_49_1.png)]

<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PO0XmDFF-1606484105268)(output_49_3.png)]

<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cVQdi0Vs-1606484105272)(output_49_5.png)]

<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9v1EmPq4-1606484105275)(output_49_7.png)]

<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3BENR01E-1606484105280)(output_49_9.png)]

<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JgKh48ur-1606484105283)(output_49_11.png)]

<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tiQDd0Ma-1606484105288)(output_49_13.png)]

<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cD68n4li-1606484105291)(output_49_15.png)]

<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UCDeSMbW-1606484105297)(output_49_17.png)]

<Figure size 432x288 with 0 Axes>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-l8DPKPKY-1606484105300)(output_49_19.png)]

user_click_item_count = sorted(user_click_merge.groupby('user_id')['click_article_id'].count(), reverse=True)
plt.plot(user_click_item_count)
[<matplotlib.lines.Line2D at 0x21fc0b4e3c8>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UYlR8SJs-1606484105303)(output_50_1.png)]

#点击次数在前50的用户
plt.plot(user_click_item_count[:50])
[<matplotlib.lines.Line2D at 0x21fc0b15308>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MAO8oUsk-1606484105305)(output_51_1.png)]

点击次数排前50的用户的点击次数都在100次以上。思路:我们可以定义点击次数大于等于100次的用户为活跃用户,这是一种简单的处理思路, 判断用户活跃度,更加全面的是再结合上点击时间,后面我们会基于点击次数和点击时间两个方面来判断用户活跃度。

#点击次数排名在[25000:50000]之间
plt.plot(user_click_item_count[25000:50000])
[<matplotlib.lines.Line2D at 0x21fc0ae2f48>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UJSbhRqz-1606484105308)(output_53_1.png)]

可以看出点击次数小于等于两次的用户非常的多,这些用户可以认为是非活跃用户

新闻点击次数分析

item_click_count = sorted(user_click_merge.groupby('click_article_id')['user_id'].count(), reverse=True)
plt.plot(item_click_count)
[<matplotlib.lines.Line2D at 0x21f0f68a9c8>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-osXaOJYF-1606484105311)(output_57_1.png)]

plt.plot(item_click_count[:100])
[<matplotlib.lines.Line2D at 0x21f0f716948>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-svkcMFxf-1606484105315)(output_58_1.png)]

plt.plot(item_click_count[:20])
[<matplotlib.lines.Line2D at 0x21f0f620d08>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MOW9H7IF-1606484105322)(output_59_1.png)]

点击次数最多的前20篇新闻,点击次数大于2500。思路:可以定义这些新闻为热门新闻, 这个也是简单的处理方式,后面我们也是根据点击次数和时间进行文章热度的一个划分。

plt.plot(item_click_count[3500:])
[<matplotlib.lines.Line2D at 0x21f9be4a988>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hFEHGFC9-1606484105334)(output_61_1.png)]

可以发现很多新闻只被点击过一两次。思路:可以定义这些新闻是冷门新闻

新闻共现频次:两篇新闻连续出现的次数¶

将以用户分组的文章id向上移动一位表示为下一条推荐的文章,shift函数(-1)

tmp = user_click_merge.sort_values('click_timestamp')
tmp['next_item'] = tmp.groupby(['user_id'])['click_article_id'].transform(lambda x:x.shift(-1))
union_item = tmp.groupby(['click_article_id','next_item'])['click_timestamp'].agg({'count'}).reset_index().sort_values('count', ascending=False)
union_item[['count']].describe()
count
count433597.000000
mean3.184139
std18.851753
min1.000000
25%1.000000
50%1.000000
75%2.000000
max2202.000000
#由统计数据可以看出,平均共现次数2.88,最高为1687。
#说明用户看的新闻,相关性是比较强的。
#画个图直观地看一看
x = union_item['click_article_id']
y = union_item['count']
plt.scatter(x, y)
<matplotlib.collections.PathCollection at 0x21f0fedad88>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AwWOhXGL-1606484105340)(output_66_1.png)]

plt.plot(union_item['count'].values[40000:])
[<matplotlib.lines.Line2D at 0x21f0ff1eb08>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A9ItfUCn-1606484105345)(output_67_1.png)]

新闻文章信息¶

#不同类型的新闻出现的次数
plt.plot(user_click_merge['category_id'].value_counts().values)
[<matplotlib.lines.Line2D at 0x21f0dda5808>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nOjT0XLF-1606484105350)(output_69_1.png)]

#出现次数比较少的新闻类型, 有些新闻类型,基本上就出现过几次
plt.plot(user_click_merge['category_id'].value_counts().values[150:])
[<matplotlib.lines.Line2D at 0x21f58bf0e88>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BlwAanPI-1606484105357)(output_70_1.png)]

#新闻字数的描述性统计
user_click_merge['words_count'].describe()
count    1.630633e+06
mean     2.043012e+02
std      6.382198e+01
min      0.000000e+00
25%      1.720000e+02
50%      1.970000e+02
75%      2.290000e+02
max      6.690000e+03
Name: words_count, dtype: float64
plt.plot(user_click_merge['words_count'].values)
[<matplotlib.lines.Line2D at 0x21f5ad068c8>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9yu5LORu-1606484105359)(output_72_1.png)]

用户点击的新闻类型的偏好

plt.plot(sorted(user_click_merge.groupby('user_id')['category_id'].nunique(), reverse=True))
[<matplotlib.lines.Line2D at 0x21f5ad08708>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-grjcYAuS-1606484105366)(output_74_1.png)]

用户查看文章的长度的分布¶

通过统计不同用户点击新闻的平均字数,这个可以反映用户是对长文更感兴趣还是对短文更感兴趣。

plt.plot(sorted(user_click_merge.groupby('user_id')['words_count'].mean(), reverse=True))
[<matplotlib.lines.Line2D at 0x21fb99c5b88>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IMmY7NOu-1606484105369)(output_77_1.png)]

从上图中可以发现有一小部分人看的文章平均词数非常高,也有一小部分人看的平均文章次数非常低。

大多数人偏好于阅读字数在200-400字之间的新闻

#挑出大多数人的区间仔细看看
plt.plot(sorted(user_click_merge.groupby('user_id')['words_count'].mean(), reverse=True)[1000:45000])
[<matplotlib.lines.Line2D at 0x21f9f9b3388>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Fxln4RfP-1606484105373)(output_79_1.png)]

可以发现大`多数人都是看250字以下的文章

 #更加详细的参数
user_click_merge.groupby('user_id')['words_count'].mean().reset_index().describe()
user_idwords_count
count250000.000000250000.000000
mean124999.500000205.830189
std72168.92798647.174030
min0.0000008.000000
25%62499.750000187.500000
50%124999.500000202.000000
75%187499.250000217.750000
max249999.0000003434.500000

用户点击新闻的时间分析¶

#为了更好的可视化,这里把时间进行归一化操作
from sklearn.preprocessing import MinMaxScaler
mm = MinMaxScaler()
user_click_merge['click_timestamp'] = mm.fit_transform(user_click_merge[['click_timestamp']])
user_click_merge['created_at_ts'] = mm.fit_transform(user_click_merge[['created_at_ts']])

user_click_merge = user_click_merge.sort_values('click_timestamp')

user_click_merge.head()
user_idclick_article_idclick_timestampclick_environmentclick_deviceGroupclick_osclick_countryclick_regionclick_referrer_typerankclick_cntscategory_idcreated_at_tswords_count
182499901623000.00000043201252552810.989186193
22499981609740.00000241121132552810.989092259
302499851609740.0000034117182882810.989092259
502499791623000.00000441171252222810.989186193
252499881609740.0000044117121217172810.989092259
def mean_diff_time_func(df, col):
    df = pd.DataFrame(df, columns={col})
    df['time_shift1'] = df[col].shift(1).fillna(0)
    df['diff_time'] = abs(df[col] - df['time_shift1'])
    return df['diff_time'].mean()
# 点击时间差的平均值
mean_diff_click_time = user_click_merge.groupby('user_id')['click_timestamp', 'created_at_ts'].apply(lambda x: mean_diff_time_func(x, 'click_timestamp'))
plt.plot(sorted(mean_diff_click_time.values, reverse=True))

从上图可以发现不同用户点击文章的时间差是有差异的

# 前后点击文章的创建时间差的平均值
mean_diff_created_time = user_click_merge.groupby('user_id')['click_timestamp', 'created_at_ts'].apply(lambda x: mean_diff_time_func(x, 'created_at_ts'))
 plt.plot(sorted(mean_diff_created_time.values, reverse=True))

从图中可以发现用户先后点击文章,文章的创建时间也是有差异的

# 用户前后点击文章的相似性分布
item_idx_2_rawid_dict = dict(zip(item_emb_df['article_id'], item_emb_df.index))
del item_emb_df['article_id']
item_emb_np = np.ascontiguousarray(item_emb_df.values, dtype=np.float32)
# 随机选择5个用户,查看这些用户前后查看文章的相似性
sub_user_ids = np.random.choice(user_click_merge.user_id.unique(), size=15, replace=False)
sub_user_info = user_click_merge[user_click_merge['user_id'].isin(sub_user_ids)]

sub_user_info.head()
def get_item_sim_list(df):
    sim_list = []
    item_list = df['click_article_id'].values
    for i in range(0, len(item_list)-1):
        emb1 = item_emb_np[item_idx_2_rawid_dict[item_list[i]]]
        emb2 = item_emb_np[item_idx_2_rawid_dict[item_list[i+1]]]
        sim_list.append(np.dot(emb1,emb2)/(np.linalg.norm(emb1)*(np.linalg.norm(emb2))))
    sim_list.append(0)
    return sim_list
for _, user_df in sub_user_info.groupby('user_id'):
    item_sim_list = get_item_sim_list(user_df)
    plt.plot(item_sim_list)

从图中可以看出有些用户前后看的商品的相似度波动比较大,有些波动比较小,也是有一定的区分度的

  1. 训练集和测试集的用户id没有重复,也就是测试集里面的用户模型是没有见过的
  2. 训练集中用户最少的点击文章数是2, 而测试集里面用户最少的点击文章数是1
  3. 用户对于文章存在重复点击的情况, 但这个都存在于训练集里面
  4. 同一用户的点击环境存在不唯一的情况,后面做这部分特征的时候可以采用统计特征
  5. 用户点击文章的次数有很大的区分度,后面可以根据这个制作衡量用户活跃度的特征
  6. 文章被用户点击的次数也有很大的区分度,后面可以根据这个制作衡量文章热度的特征
  7. 用户看的新闻,相关性是比较强的,所以往往我们判断用户是否对某篇文章感兴趣的时候, 在很大程度上会和他历史点击过的文章有关
  8. 用户点击的文章字数有比较大的区别, 这个可以反映用户对于文章字数的区别
  9. 用户点击过的文章主题也有很大的区别, 这个可以反映用户的主题偏好 10.不同用户点击文章的时间差也会有所区别, 这个可以反映用户对于文章时效性的偏好

所以根据上面的一些分析,可以更好的帮助我们后面做好特征工程, 充分挖掘数据的隐含信息。

最后本文如有错误,望联系本人会立马改正。本人是使用jupyter编写代码部分上传,可以直接下载。 另外本文可能太长给 您带来不便,望理解。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值