赞
踩
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False # 步骤二(解决坐标轴负数的负号显示问题)
user = pd.read_csv('E:/学习/fresh_comp_offline/tianchi_fresh_comp_train_user.csv')
user.info()
user.head()
# '''
# user_id用户标识,
# item_id 商品标识,
# behavior_type,用户对商品的行为类型 包括浏览、收藏、加购物车、购买,对应取值分别是1、2、3、4。
# user_geohash 用户位置的空间标识,可以为空 由经纬度通过保密的算法生成
# item_category 商品分类标识 字段脱敏
# time 行为时间 精确到小时级别
# '
# 查看统计信息
user.describe()
# 查看缺失值
user.isnull().sum()
3. 处理数据
user.duplicated().sum()
# 删除重复值
user.drop_duplicates(keep='last',inplace=True)
user.info() # 查看是否删除了重复值
# 将时间转换为datetime格式
user['time'] = pd.to_datetime(user['time'])
user.head()
# 提取出日期、月、年
user['dates'] = user.time.dt.date
user['month'] = user.dates.values.astype('datetime64[M]')
user['hours'] = user.time.dt.hour
user.head()
# 转换数据类型
user['behavior_type'] = user['behavior_type'].apply(str)
user['user_id'] = user['user_id'].apply(str)
user['item_id'] = user['item_id'].apply(str)
user.info()
4. 数据可视化
4.1 统计每日PV和UV数据
# 统计每日PV数据
pv_day = user[user.behavior_type=='1'].groupby('dates')['behavior_type'].count()
pv_day
# 统计每日UV数据
uv_day = user[user.behavior_type=='1'].drop_duplicates(['user_id','dates']).groupby('dates')['user_id'].count()
uv_day
4.2 分析每天的PV和uv的趋势
pv_day.plot(figsize=(12,5),legend=True,title='pv与uv趋势图')
uv_day.plot(legend=True)
两个数据的数据量相差较大,因此需要使用两个不同的坐标来表示
fig,ax1 = plt.subplots()
ax1.plot(pv_day,label = 'pv',color='red')
ax2 = ax1.twinx()
ax2.plot(uv_day,label = 'uv',color='green')
fig.legend()
plt.show()
4.3 pv、uv差异分析(by day)
pv_uv = pd.concat([pv_day,uv_day],join='outer',axis = 1)
pv_uv.columns = ['pv_day','uv_day']
pv_uv
pv_uv[['pv_day','uv_day']].plot(secondary_y=['pv_day','uv_day'])
new_day = pv_uv.diff() # 计算日期之间的差异
new_day.columns = ['new_pv','new_uv']
new_day
new_day.new_uv.plot()
new_day.new_pv.plot()
5. 不同时期用户行为分析
# 统计不同购买行为的每日总计量
shopping_cart= user[user.behavior_type == '3'].groupby('dates')['behavior_type'].count()
collect=user[user.behavior_type=='2'].groupby('dates')['behavior_type'].count()
buy=user[user.behavior_type=='4'].groupby('dates')['behavior_type'].count()
plt.figure(1,figsize=(10,4))
plt.plot(shopping_cart,'go-',label='加购人数')
plt.plot(collect,'bo-',label='收藏人数')
plt.plot(buy,'ro-',label='购买人数')
plt.legend()
plt.show()
可以看到数据在12月11日附近有较大的波动,考虑是双十二活动的影响,整理分析用户的不同时期行为可能会导致分析结果与实际差异较大,因此拆分开来做不同的对比分析
active = user[user['dates'].isin(["2014/12/11","2014/12/12","2014/12/13"])]
active
选取日常数据子集
daily = user[~user['dates'].isin(["2014/12/11","2014/12/12","2014/12/13"])]
daily
活动期间不同时段的用户行为分析
uv_h = active[active.behavior_type=='1'].groupby('hours')['user_id'].count()
collect_h = active[active.behavior_type=='2'].groupby('hours')['behavior_type'].count()
cart_h = active[active.behavior_type=='3'].groupby('hours')['behavior_type'].count()
buy_h = active[active.behavior_type=='4'].groupby('hours')['behavior_type'].count()
uv_h.plot(kind='bar',title="浏览人数")
plt.figure(figsize = (10,5))
plt.plot(cart_h,'go-',label='加购人数')
plt.plot(collect_h,'bo-',label='收藏人数')
plt.plot(buy_h,'ro-',label='购买人数')
plt.title("日均各时段活动用户行为")
plt.legend()
plt.show()
这是三天活动的日均数据,可以发现活动期间是商家在起主导作用大促集中在零点,因此用户的购买高峰也出现在0点, 点击浏览的高峰集中在晚上的21点到22点之间,因此商家可以在20点前改好促销页面吸引顾客参加0点的活动。
日常期间不同时段的用户行为分析
uv_d = daily[daily.behavior_type=='1'].groupby('hours')['user_id'].count()
collect_d = daily[daily.behavior_type=='2'].groupby('hours')['behavior_type'].count()
cart_d = daily[daily.behavior_type=='3'].groupby('hours')['behavior_type'].count()
buy_d = daily[daily.behavior_type=='4'].groupby('hours')['behavior_type'].count()
uv_d.plot(kind='bar',title="浏览人数")
plt.figure(figsize = (10,5))
plt.plot(cart_d,'go-',label='加购人数')
plt.plot(collect_d,'bo-',label='收藏人数')
plt.plot(buy_d,'ro-',label='购买人数')
plt.legend()
plt.title("日均各时段活动用户行为")
plt.show()
与大促不同的是日常期间购买人数从上午10点到晚上19点变化都不会太大,高峰出现在晚上21点,pv、加购、收藏的高峰出现在晚上21点到22点之间,说明大家都喜欢在晚上这个时间段浏览商品,日常时可以集中在这个时段进行促销活动,浏览高峰也是集中在晚上21点到22点之间。
hour_buy_user_sum = active[active.behavior_type=='4'].drop_duplicates(['user_id','dates','hours']).groupby('hours')['user_id'].count()
hour_buy_user_sum # 活动时每小时的购买人数
hour_active_user_sum = active.drop_duplicates(['user_id','dates','hours']).groupby('hours')['user_id'].count()
hour_active_user_sum # 活动参加人数
hour_buy_rate = hour_buy_user_sum/hour_active_user_sum
attr_o = list(hour_buy_user_sum.index)
vo_2 =np.around(hour_buy_rate.values,decimals=2)
vo_2
日常购买率
hour_buy_daily_num = daily[daily.behavior_type == '4'].drop_duplicates(['user_id','dates', 'hours']).groupby('hours')['user_id'].count()
hour_active_daily_num = daily.drop_duplicates(['user_id','dates', 'hours']).groupby('hours')['user_id'].count()
daily_buy_rate = hour_buy_daily_num / hour_active_daily_num
vi_2 =np.around(daily_buy_rate.values,decimals=2)
plt.figure()
plt.plot(hour_buy_rate,marker='o',label="活动购买率")
plt.plot(daily_buy_rate,marker='o',label="日常购买率")
plt.legend()
plt.show()
日常时的购买率最高的出现在上午10点到下午15点间,还有晚上的21点,和活动期间的购买率不同,但是明显晚上21点已经在分析中出现比较多的峰值,因此可以考虑这个时段做做吸引用户购买的措施。
# 活动转化
a_pv = active[active.behavior_type == '1']['user_id'].count()
a_cart=active[active.behavior_type=="3"]["user_id"].count()
a_collect=active[active.behavior_type=="2"]["user_id"].count()
a_buy=active[active.behavior_type=="4"]["user_id"].count()
a_attr=["点击","加入购物车","收藏","购买"]
values=[np.around((a_pv/a_pv*100),2),
np.around((a_cart/a_pv*100),2),
np.around((a_collect/a_pv*100),2),
np.around((a_buy/a_pv*100),2),
]
参考链接:
电商用户行为可视化分析
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。