一、数据介绍
来源:https://tianchi.aliyun.com/dataset/dataDetail?dataId=46
数据集包括了淘宝APP的10000个用户在2014年11月18日至2014年12月18日之间的行为记录。
变量 | 描述 |
---|---|
user_id | 用户ID |
item_id | 商品ID |
behavior_type | 用户行为类型(1=点击、2=收藏、3=加购物车、4=支付) |
user_geohash | 地理位置(加密) |
item_category | 商品种类ID |
time | 用户行为发生的时间 |
二、数据预处理
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns
import time
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] #MAC显示中文
df=pd.read_csv('tianchi_mobile_recommend_train_user.csv')
df.shape #(12256906, 6)
df.head()
>>>
user_id item_id behavior_type user_geohash item_category \
0 98047837 232431562 1 NaN 4245
1 97726136 383583590 1 NaN 5894
2 98607707 64749712 1 NaN 2883
3 98662432 320593836 1 96nn52n 6562
4 98145908 290208520 1 NaN 13926
time
0 2014-12-06 02
1 2014-12-09 20
2 2014-12-18 11
3 2014-12-06 10
4 2014-12-16 21
1. 缺失值
df.isnull().sum()
>>>
user_id 0
item_id 0
behavior_type 0
user_geohash 8334824
item_category 0
time 0
date 0
hour 0
date_str 0
dtype: int64
只有地理位置这一列有大量缺失。由于地理位置信息被加密过,无法使用,可将此列直接删除。
2. 时间序列处理
将time分为日期和小时
df['date']=df['time'].str.split().str[0]
df['date']=pd.to_datetime(df['date'])
df['hour']=df['time'].str.split().str[1]
3. 数据类型转换
int/float -> object
df['user_id']=df['user_id'].astype('object')
df['item_id']=df['item_id'].astype('object')
df['behavior_type']=df['behavior_type'].astype('object')
df['item_category']=df['item_category'].astype('object')
4. 异常值
df1=df.drop(['user_geohash', 'time'], axis=1)
df1.describe(include='all')
>>>
user_id item_id behavior_type item_category \
count 12256906.0 12256906.0 12256906.0 12256906.0
unique 10000.0 2876947.0 4.0 8916.0
top 36233277.0 112921337.0 1.0 1863.0
freq 31030.0 1445.0 11550581.0 393247.0
first NaN NaN NaN NaN
last NaN NaN NaN NaN
date hour
count 12256906 12256906
unique 31 24
top 2014-12-12 00:00:00 21
freq 691712 1090178
first 2014-11-18 00:00:00 NaN
last 2014-12-18 00:00:00 NaN
未发现异常值。
三、用户行为分析
1. PV、UV
PV(页面访问量):即Page View,即页面浏览量或点击量,用户每次刷新即被计算一次。
UV(独立访客数):即Unique Visitor,访问您网站的一台电脑客户端为一个访客。
1.1 每日访问量/访客数
daily_pv=df1.groupby('date')['user_id'].count().reset_index()
daily_uv=df1.groupby('date')['user_id'].nunique().reset_index()
# 画图
tick=list(range(0, 30, 4))
fig=plt.figure(figsize=(8,5))
ax1=fig.add_subplo