一、项目介绍
本次实战的数据是来自阿里巴巴提供的一个淘宝用户行为数据集,用于隐式反馈推荐问题的研究。
数据下载:https://tianchi.aliyun.com/dataset/dataDetail?dataId=649&userId=1数据下载地址
由于数据过于庞大,自己的电脑性能有限,所以截取了前1000万条数据来进行实验,主要还是懂得方法。
二、分析数据
本数据集包含了2017年11月25日至2017年12月3日之间,有行为的约一百万随机用户的所有行为(行为包括点击、购买、加购、喜欢)。数据集的组织形式和MovieLens-20M类似,即数据集的每一行表示一条用户行为,由用户ID、商品ID、商品类目ID、行为类型和时间戳组成,并以逗号分隔。关于数据集中每一列的详细描述如下:
用户的4种行为:
三、数据清理
3.1 用pandas处理数据,导入数据的时候加上列名
import pandas as pd
data = pd.read_csv('UserBehavior_1千万条.csv', encoding='utf-8')
data.columns = ['user_id', 'item_id', 'category_id', 'behavior', 'time'] #加入列名
'''
用户id 商品id 商品类id 行为类型 时间戳
行为类型:pv :点击
buy: 够买
cart: 加入购物车
fav:收藏商品
'''
3.2 把时间戳变成时间,并且分成date和time两列
# 把时间戳变成时间,并且分成date和time两列
data["time"] = pd.to_datetime(data["time"], unit='s') # 把时间戳变为时间
data["time"] = data["time"].astype(str)
newdata = data["time"].str.split(" ", 2, True)
newdata.columns = ["date", "time"]
data = data.drop("time", axis=1).join(newdata)
3.3 重复值处理
# 重复值处理
data.drop_duplicates(inplace=True)
# print(data.duplicated().sum()) # 查看重复值
3.4 异常值处理
只保留11月25日到12月4日的数据
# 异常值处理
data = data.drop(data[(data["date"] < '2017-11-25') | (data["date"] > '2017-12-04')].index)
# print("删除之后:", data['date'].value_counts())
data = data.sort_values(by="date", ascending=True) # 按照date升序排序
至此,数据预处理完成,把处理好的数据保存方便后序导入数据库
data.to_csv("已经预处理的.csv", index=False)
四、数据分析
这里可视化用pyecharts框架来画图
4.1基于AARRR模型分析
4.1.1用户获取(Acquisition)
采用日新增用户数DNU,考察每日新增用户和日均访问数量。
每日新增用户:
由下图可见,在12月1日新增用户数非常多(当然这只是去了1000万条数据的,本次实验主要学习的是方法)。
from pymysql import Connect
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.commons.utils import JsCode
conn = Connect(host='localhost', port=3306, user='root', password='123456', database='dianshang')
# 获取游标
cursor = conn.cursor()
sql = """
select date,count(behavior) as 日均访问量
from user
where behavior = 'pv'
group by date
order by date;
"""
# 执行sql语句
cursor.execute(sql)
# 读取数据
data = cursor.fetchall()
x = []
y_data = []
x_data = []
for i in range(10):
x.append(data[i][0])
y_data.append(data[i][1])
x_data.append(x[i][5:])
# 计算每日新增数据
add = []
for i in range(9):
sum = 0
sum = y_data[i+1] - y_data[i]
if sum < 0:
sum = 0
add.append(sum)
# 可视化
background_color_js = (
"new echarts.graphic.LinearGradient(0, 0, 0, 1, "
"[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
)
area_color_js = (
"new echarts.graphic.LinearGradient(0, 0, 0, 1, "
"[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)"
)
c = (
Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js)))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="注册总量",
y_axis=add,
is_smooth=True,
is_symbol_show=True,
symbol="circle",
symbol_size=6,
linestyle_opts=opts.LineStyleOpts(color="#fff"),
label_opts=opts.LabelOpts(is_show=True, position="top", color="white"),
itemstyle_opts=opts.ItemStyleOpts(
color="red", border_color="#fff", border_width=3
),
tooltip_opts=opts.TooltipOpts(is_show=False),
areastyle_opts=opts.AreaStyleOpts(color=JsCode(area_color_js), opacity=1),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="每日新增",
pos_bottom="5%",
pos_left="center",
title_textstyle_opts=opts.TextStyleOpts(color="#fff", font_size=16),
),
xaxis_opts=opts.AxisOpts(
type_="category",
boundary_gap=False,
axislabel_opts=opts.LabelOpts(margin=30, color="#ffffff63"),
axisline_opts=opts.AxisLineOpts(is_show=False),
axistick_opts=opts.AxisTickOpts(
is_show=True,
length=25,
linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
),
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
),
),
yaxis_opts=opts.AxisOpts(
type_="value",
position="right",
axislabel_opts=opts.LabelOpts(margin=20, color="#ffffff63"),
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(width=2, color="#fff")
),
axistick_opts=opts.AxisTickOpts(
is_show=True,
length=15,
linestyle_opts=opts.LineStyleOpts(color="#ffffff1f"),
),
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(color="#ffffff1f")
),
),
legend_opts=opts.LegendOpts(is_show=False),
)
.render("每日新增.html")
)
日均访问数量:
from pymysql import Connect
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.commons.utils import JsCode
# 连接数据库
conn = Connect(host='localhost', port=3306, user='root', password='123456', database='dianshang')
# 获取游标
cursor = conn.cursor()
sql = """
select date,count(behavior) as 日均访问量
from user
where behavior = 'pv'
group by date
order by date;
"""
# 执行sql语句
cursor.execute(sql)
# 读取数据
data = cursor.fetchall()
print(data)
x = []
y_data = []
x_data = []
for i in range(10):
x.append(data[i][0])
y_data.append(data[i][1])
x_data.append(x[i][5:])
# print(x_data)
# 可视化
background_color_js = (
"new echarts.graphic.LinearGradient(0, 0, 0, 1, "
"[{offset: 0, color: '#c86589'}, {offset: 1, color: '#06a7ff'}], false)"
)
area_color_js = (
"new echarts.graphic.LinearGradient(0, 0, 0, 1, "
"[{offset: 0, color: '#eb64fb'}, {offset: 1, color: '#3fbbff0d'}], false)"
)
c = (
Line(init_opts=opts.InitOpts(bg_color=JsCode(background_color_js)))
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="注册总量",
y_axis=y_data,
is_smooth=True,
is_symbol_show=True,
symbol="circle",
symbol_size=6,
linestyle_opts=opts.LineStyleOpts(color="#fff"),
label_opts=opts.LabelOpts(is_show=True, position