1 前言
?? 這兩年開始畢業(yè)設(shè)計(jì)和畢業(yè)答辯的要求和難度不斷提升,傳統(tǒng)的畢設(shè)題目缺少創(chuàng)新和亮點(diǎn),往往達(dá)不到畢業(yè)答辯的要求,這兩年不斷有學(xué)弟學(xué)妹告訴學(xué)長(zhǎng)自己做的項(xiàng)目系統(tǒng)達(dá)不到老師的要求。
為了大家能夠順利以及最少的精力通過(guò)畢設(shè),學(xué)長(zhǎng)分享優(yōu)質(zhì)畢業(yè)設(shè)計(jì)項(xiàng)目,今天要分享的是
?? 基于大數(shù)據(jù)的京東消費(fèi)行為分析與可視化
??學(xué)長(zhǎng)這里給一個(gè)題目綜合評(píng)分(每項(xiàng)滿分5分)
- 難度系數(shù):3分
- 工作量:3分
- 創(chuàng)新點(diǎn):3分
?? 選題指導(dǎo), 項(xiàng)目分享:
https://gitee.com/dancheng-senior/project-sharing-1/blob/master/%E6%AF%95%E8%AE%BE%E6%8C%87%E5%AF%BC/README.md
2 數(shù)據(jù)處理
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
import seaborn as sns
from pylab import *
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
import textwrap
# 中文設(shè)置
plt.rcParams['font.sans-serif']=['Microsoft YaHei']
plt.rcParams['axes.unicode_minus']=False
plt.rc('font',family = 'Microsoft YaHei',size = '15')
warnings.filterwarnings("ignore")
%matplotlib inline
# 讀取數(shù)據(jù)
data = pd.read_excel('D:/data/京東消費(fèi)者分析數(shù)據(jù).xlsx')
data.head(10)
# 查看數(shù)據(jù)量
print('數(shù)據(jù)條數(shù):{} \n數(shù)據(jù)字段:{}'.format(data.shape[0],data.shape[1]))
#字段中文命名
data.columns = ["客戶id","產(chǎn)品id","行為時(shí)間","行為id","行為類別","年齡分段","性別","客戶注冊(cè)日期",\
"會(huì)員級(jí)別","會(huì)員城市級(jí)別","產(chǎn)品品牌","店鋪id","產(chǎn)品類別","產(chǎn)品上市日期","商家id",\
"粉絲數(shù)","會(huì)員數(shù)","開店時(shí)間","店鋪主營(yíng)","店鋪打分"]
data.head(10)
data.info()
# 缺失率
data.isnull().mean()
缺失字段只有“年齡分段”,“會(huì)員城市級(jí)別”,“開店時(shí)間”。
其中“年齡分段”,“會(huì)員城市級(jí)別”數(shù)據(jù)量占比很少,可以去除
“開店時(shí)間”缺失達(dá)到1/3,暫時(shí)保留數(shù)據(jù),需要相關(guān)分析時(shí)再去除。
# 刪除缺失值
data.dropna(subset=['年齡分段','會(huì)員城市級(jí)別'],inplace=True)
print(data.shape)
data.isnull().mean()
# 刪除重復(fù)值
data.drop_duplicates(inplace=True)
print(data.shape)
# 數(shù)據(jù)描述
print('行為類別包含:{}'.format(data['行為類別'].unique()))
print('性別包含:{}'.format(data['性別'].unique()))
print('產(chǎn)品類別包含:{}'.format(data["產(chǎn)品類別"].unique()))
print('年齡分段包含:{}'.format(data['年齡分段'].unique()))
data[['年齡分段','粉絲數(shù)','會(huì)員數(shù)','店鋪打分']].describe()[1:]
# 異常值判斷
print(len(data[data['店鋪打分'] < 0]))
print(len(data[data['店鋪打分'] < 0]) / data.shape[0])
店鋪打分出現(xiàn)負(fù)值,考慮到可能是差評(píng),但是常規(guī)評(píng)分為(0-10),認(rèn)為0分已經(jīng)是差評(píng)了,具體要看實(shí)際業(yè)務(wù)評(píng)定,這里考慮到數(shù)量極少,可以視為0分處理
#替換異常值
data['店鋪打分'] = data['店鋪打分'].apply(lambda x: 0 if x<0 else x)
data[['年齡分段','粉絲數(shù)','會(huì)員數(shù)','店鋪打分']].describe()[1:]
# 轉(zhuǎn)化行為時(shí)間,提取年份、月份、日、周數(shù)據(jù)
pd.to_datetime(data['行為時(shí)間'],format="%Y-%m-%d")
data['年份'] = data['行為時(shí)間'].dt.year
data['月份'] = data['行為時(shí)間'].dt.month
data['日'] = data['行為時(shí)間'].dt.day
data['周'] = data['行為時(shí)間'].dt.weekday
data['日期'] = data['行為時(shí)間'].dt.date
data['小時(shí)'] = data['行為時(shí)間'].dt.hour
# 重建data索引
data = data.reset_index(drop = True)
data.head()
3 數(shù)據(jù)分析
每日UV(訪客數(shù))與每日PV(訪客量)
# 計(jì)算每日的UV
uv_data = data.drop_duplicates(subset = ['日期','客戶id'])
uv_data = uv_data[['日期','客戶id']].groupby(['日期']).count().reset_index()
uv_data.rename(columns = {'客戶id':'日UV'},inplace = True)
uv_data.head()
# 繪制平滑好看的折線圖函數(shù)
def echarts_line(x,y,title = '主標(biāo)題',subtitle = '副標(biāo)題',label = '圖例'):
"""
x: 函數(shù)傳入x軸標(biāo)簽數(shù)據(jù)
y:函數(shù)傳入y軸數(shù)據(jù)
title:主標(biāo)題
subtitle:副標(biāo)題
label:圖例
"""
line = Line(
init_opts=opts.InitOpts(
bg_color='#080b30', # 設(shè)置背景顏色
theme='dark', # 設(shè)置主題
width='1200px', # 設(shè)置圖的寬度
height='600px', # 設(shè)置圖的高度
)
)
line.add_xaxis(x)
line.add_yaxis(
label,
y,
is_symbol_show=False, # 是否顯示數(shù)據(jù)標(biāo)簽點(diǎn)
is_smooth=True, # 設(shè)置曲線平滑
label_opts=opts.LabelOpts(
is_show=False, # 是否顯示數(shù)據(jù)
),
itemstyle_opts=opts.ItemStyleOpts(color='#00ca95'), # 設(shè)置系列顏色
# 線條粗細(xì)陰影設(shè)置
linestyle_opts={
"normal": {
"color": "#00ca95", #線條顏色
"shadowColor": 'rgba(0, 0, 0, .3)', #陰影顏色和不透明度
"shadowBlur": 2, #陰影虛化大小
"shadowOffsetY": 5, #陰影y偏移量
"shadowOffsetX": 5, #陰影x偏移量
"width": 6 # 線條粗細(xì)
},
},
# 陰影設(shè)置
areastyle_opts={
"normal": {
"color": JsCode("""new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgba(0,202,149,0.5)'
},
{
offset: 1,
color: 'rgba(0,202,149,0)'
}
], false)"""), #設(shè)置底色色塊漸變
"shadowColor": 'rgba(0,202,149, 0.9)', #設(shè)置底色陰影
"shadowBlur": 20 #設(shè)置底色陰影大小
}
},
)
line.set_global_opts(
# 標(biāo)題設(shè)置
title_opts=opts.TitleOpts(
title=title, # 主標(biāo)題
subtitle=subtitle, # 副標(biāo)題
pos_left='center', # 標(biāo)題展示位置
title_textstyle_opts=dict(color='#fff') # 設(shè)置標(biāo)題字體顏色
),
# 圖例設(shè)置
legend_opts=opts.LegendOpts(
is_show=True, # 是否顯示圖例
pos_left='right', # 圖例顯示位置
pos_top='3%', #圖例距離頂部的距離
orient='horizontal' # 圖例水平布局
),
tooltip_opts=opts.TooltipOpts(
is_show=True, # 是否使用提示框
trigger='axis', # 觸發(fā)類型
is_show_content = True,
trigger_on='mousemove|click', # 觸發(fā)條件,點(diǎn)擊或者懸停均可出發(fā)
axis_pointer_type='cross', # 指示器類型,鼠標(biāo)移動(dòng)到圖表區(qū)可以查看效果
# formatter = '{a}<br>:{c}人' # 文本內(nèi)容
),
datazoom_opts=opts.DataZoomOpts(
range_start=0, # 開始范圍
range_end=50, # 結(jié)束范圍
# orient='vertical', # 設(shè)置為垂直布局
type_='slider', # slider形式
is_zoom_lock=False, # 鎖定區(qū)域大小
# pos_left='1%' # 設(shè)置位置
),
yaxis_opts=opts.AxisOpts(
is_show=True,
splitline_opts=opts.SplitLineOpts(is_show=False), # 分割線
axistick_opts=opts.AxisTickOpts(is_show=False), # 刻度不顯示
), # 關(guān)閉Y軸顯示
xaxis_opts=opts.AxisOpts(
boundary_gap=False, # 兩邊不顯示間隔
axistick_opts=opts.AxisTickOpts(is_show=True), # 刻度不顯示
splitline_opts=opts.SplitLineOpts(is_show=False), # 分割線不顯示
axisline_opts=opts.AxisLineOpts(is_show=True), # 軸不顯示
axislabel_opts=opts.LabelOpts( # 坐標(biāo)軸標(biāo)簽配置
font_size=10, # 字體大小
font_weight='light' # 字重
)
),
)
return line.render_notebook()
# 日UV變化圖
echarts_line(uv_data['日期'].tolist(),uv_data['日UV'].tolist(),title = '日UV變化圖',subtitle = '每日UV變化圖',label = 'UV')
因?yàn)樵路葸^(guò)少,且4月數(shù)據(jù)不完整,只分析每日UV(訪客數(shù))。
1.在2月上旬屬于平臺(tái)高峰期,且在2月15日出現(xiàn)了最低谷,大概率是京東平臺(tái)在2月上旬為2月14日情人節(jié)準(zhǔn)備了預(yù)熱活動(dòng),加上客戶為情人節(jié)準(zhǔn)備禮物的原因。
2.3月27日,3月28兩日的UV出現(xiàn)了異常,出現(xiàn)斷崖式低峰,而3月29日又立刻恢復(fù)了正常,需要重點(diǎn)查明UV低下的原因,暫時(shí)分析的原因可能是數(shù)據(jù)出錯(cuò)或者兩日內(nèi)平臺(tái)出現(xiàn)技術(shù)上的問(wèn)題,使得用戶無(wú)法登錄。
3.其他日期的數(shù)據(jù)變化相對(duì)較為平緩,屬于正常趨勢(shì)。
# 日PV變化
day_pv_data = data[['日期','客戶id']].groupby('日期').count().reset_index()
day_pv_data.rename(columns = {'客戶id':'日PV'},inplace = True)
day_pv_data.head()
# 日pv變化圖
echarts_line(day_pv_data['日期'].tolist(),day_pv_data['日PV'].tolist(),title = '日PV變化圖',subtitle = '每日PV變化圖',label = 'PV')
日PV(訪客量)的變化趨勢(shì)跟日UV(訪客數(shù))的訪客變化趨勢(shì)大致一致,分析一致,不多累述。
人群圖像
# 客戶性別比例
user_gender = data[['客戶id','性別']].drop_duplicates(subset = ['客戶id'])
gender_rate = user_gender.groupby('性別').count().reset_index()
gender_rate.rename(columns={'客戶id':'人數(shù)'},inplace=True)
gender_rate.drop(gender_rate[gender_rate['性別']=='U'].index, inplace=True)
gender_rate['比例'] = gender_rate["人數(shù)"] / gender_rate["人數(shù)"].sum()
gender_rate = gender_rate.reset_index(drop=True)
gender_rate
# 購(gòu)買數(shù)量性別比例
buy_gender = data[['產(chǎn)品id','性別']]
gender_buy_rate = buy_gender.groupby('性別').count().reset_index()
gender_buy_rate.rename(columns={'產(chǎn)品id':'購(gòu)買數(shù)量'},inplace=True)
gender_buy_rate.drop(gender_buy_rate[gender_buy_rate['性別']=='U'].index, inplace=True)
gender_buy_rate['比例'] = gender_buy_rate["購(gòu)買數(shù)量"] / gender_buy_rate["購(gòu)買數(shù)量"].sum()
gender_buy_rate = gender_buy_rate.reset_index(drop=True)
gender_buy_rate
# 年齡分布
age_data = data[['客戶id','年齡分段']].drop_duplicates(subset = ['客戶id'])
age_rate = age_data.groupby('年齡分段').count().reset_index()
age_rate.drop(age_rate[age_rate['年齡分段']==3].index,inplace=True)
age_rate.rename(columns={'客戶id':'人數(shù)'},inplace=True)
age_rate['比例'] = age_rate['人數(shù)'] / age_rate['人數(shù)'].sum()
age_rate
c = (
Pie()
.add(
"",
[list(z) for z in zip(age_rate['年齡分段'].tolist(),age_rate['比例'].round(4).tolist())],
center=["35%", "50%"],
)
.set_global_opts(
title_opts=opts.TitleOpts(title="年齡分布比例"),
legend_opts=opts.LegendOpts(pos_left="15%"),
)
.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c}"))
)
c.render_notebook()
性別分布上,該平臺(tái)無(wú)論客戶人數(shù)還是購(gòu)買產(chǎn)品數(shù)量上的男女比例都是男性占大多數(shù),約比女性多一倍,產(chǎn)品和營(yíng)銷活動(dòng)可以多以男性為主,但是考慮到部分產(chǎn)品品牌類別原因,如珠寶,化妝品等產(chǎn)品為主的門店仍然應(yīng)該以女性為主導(dǎo)。
年齡分布上,該數(shù)據(jù)集年齡分段為3的數(shù)據(jù)因?yàn)楫惓1粍h除,在剩下的五個(gè)年齡分段中,以5,6年齡分段的人數(shù)占大多數(shù),約80%,重點(diǎn)關(guān)注這兩個(gè)年齡段人群進(jìn)行廣告,活動(dòng)引流,具體代表的年齡需要查看實(shí)際業(yè)務(wù)年齡分段的區(qū)間。
2.3.轉(zhuǎn)化率
# 計(jì)算各環(huán)節(jié)的人數(shù)
view_data = data[data['行為類別'] == 'PageView'].drop_duplicates(subset = ['日期','客戶id'])[['日期','客戶id']].groupby('日期').count().reset_index()
follow_data = data[data['行為類別'] == 'Follow'].drop_duplicates(subset = ['日期','客戶id'])[['日期','客戶id']].groupby('日期').count().reset_index()
cart_data = data[data['行為類別'] == 'SavedCart'].drop_duplicates(subset = ['日期','客戶id'])[['日期','客戶id']].groupby('日期').count().reset_index()
order_data = data[data['行為類別'] == 'Order'].drop_duplicates(subset = ['日期','客戶id'])[['日期','客戶id']].groupby('日期').count().reset_index()
user_data = data.drop_duplicates(subset = ['日期','客戶id'])[['日期','客戶id']].groupby('日期').count().reset_index()
view_data.rename(columns = {'客戶id':'瀏覽'},inplace = True)
follow_data.rename(columns = {'客戶id':'收藏'},inplace = True)
cart_data.rename(columns ={'客戶id':'加入購(gòu)物車'},inplace = True)
order_data.rename(columns = {'客戶id':'購(gòu)買'},inplace = True)
user_data.rename(columns = {'客戶id':'用戶數(shù)量'},inplace = True)
transform_data = pd.merge(pd.merge(pd.merge(pd.merge(view_data,follow_data,how = 'left',on = '日期'),cart_data,how = 'right',on = '日期'),order_data,how = 'left',on = '日期'),user_data,how = 'left',on = '日期')
transform_data
transform_data['瀏覽-加購(gòu)轉(zhuǎn)化率'] = transform_data['加入購(gòu)物車'] / transform_data['瀏覽']
transform_data['瀏覽-加購(gòu)轉(zhuǎn)化率'] = transform_data['瀏覽-加購(gòu)轉(zhuǎn)化率'].round(4)
transform_data['加購(gòu)-購(gòu)買轉(zhuǎn)化率'] = transform_data['購(gòu)買'] / transform_data['加入購(gòu)物車']
transform_data['加購(gòu)-購(gòu)買轉(zhuǎn)化率'] = transform_data['加購(gòu)-購(gòu)買轉(zhuǎn)化率'].round(4)
transform_data['加購(gòu)率'] = transform_data['加入購(gòu)物車'] / transform_data['用戶數(shù)量']
transform_data['加購(gòu)率'] = transform_data['加購(gòu)率'].round(4)
transform_data['購(gòu)買率'] = transform_data['購(gòu)買'] / transform_data['用戶數(shù)量']
transform_data['購(gòu)買率'] = transform_data['購(gòu)買率'].round(4)
transform_data
由于數(shù)據(jù)行為類別字段中的加入購(gòu)物車只有4月份的數(shù)據(jù),所以這次的轉(zhuǎn)化率是以一周的的數(shù)據(jù)計(jì)算周轉(zhuǎn)化率,
cate = ['瀏覽', '加入購(gòu)物車', '購(gòu)買']
trans_data = [int(transform_data['瀏覽'].sum()), int(transform_data['加入購(gòu)物車'].sum()), int(transform_data['購(gòu)買'].sum())]
funnel = Funnel(
init_opts=opts.InitOpts(
bg_color='#E8C5CB50', # 設(shè)置背景顏色
theme='essos', # 設(shè)置主題
width='1000px', # 設(shè)置圖的寬度
height='600px' # 設(shè)置圖的高度
)
)
funnel.add(
" ",
[list(z) for z in zip(cate, trans_data)],
# is_label_show=True, # 確認(rèn)顯示標(biāo)簽
)
funnel.set_series_opts( # 自定義圖表樣式
label_opts=opts.LabelOpts(
is_show=True,
formatter="{a}\n : {c}",
position = "inside",
font_weight = 'bolder',
font_style = 'oblique',
font_size=15,
), # 是否顯示數(shù)據(jù)標(biāo)簽
itemstyle_opts={
"normal": {
# 調(diào)整柱子顏色漸變
'shadowBlur': 8, # 光影大小
"barBorderRadius": [100, 100, 100, 100], # 調(diào)整柱子圓角弧度
"shadowColor": "#E9B7D3", # 調(diào)整陰影顏色
'shadowOffsetY': 6,
'shadowOffsetX': 6, # 偏移量
}
}
)
funnel.set_global_opts(
# 標(biāo)題設(shè)置
title_opts=opts.TitleOpts(
title='整體各環(huán)節(jié)漏斗圖', # 主標(biāo)題
subtitle='瀏覽-加購(gòu)-購(gòu)買各環(huán)節(jié)人數(shù)', # 副標(biāo)題
pos_left='center', # 標(biāo)題展示位置
title_textstyle_opts=dict(color='#5A3147'), # 設(shè)置標(biāo)題字體顏色
subtitle_textstyle_opts=dict(color='#5A3147')
),
legend_opts=opts.LegendOpts(
is_show=True, # 是否顯示圖例
pos_left='right', # 圖例顯示位置
pos_top='3%', #圖例距離頂部的距離
orient='vertical', # 圖例水平布局
textstyle_opts=opts.TextStyleOpts(
color='#5A3147', # 顏色
font_size='13', # 字體大小
font_weight='bolder', # 加粗
),
),
)
funnel.render_notebook()
# 整體各轉(zhuǎn)化率計(jì)算
view_cart_rate = round((transform_data['加入購(gòu)物車'].sum() / transform_data['瀏覽'].sum())*100,2)
cart_order = round((transform_data['購(gòu)買'].sum() / transform_data['加入購(gòu)物車'].sum())*100,2)
cart_rate = round((transform_data['加入購(gòu)物車'].sum() / transform_data['用戶數(shù)量'].sum())*100,2)
order_rate = round((transform_data['購(gòu)買'].sum() / transform_data['用戶數(shù)量'].sum())*100,2)
view_cart_rate
# 整體轉(zhuǎn)換率的查看
def chart_gauge(num,title = '主標(biāo)題',label = '圖例'):
gauge = Gauge(
init_opts=opts.InitOpts(
bg_color='#E8C5CB50', # 設(shè)置背景顏色
theme='essos', # 設(shè)置主題
width='500px', # 設(shè)置圖的寬度
height='500px' # 設(shè)置圖的高度
)
)
gauge.add(
label,
[(title,num)],
min_ = 0, # 最小的數(shù)據(jù)值
max_ = 40, # 最大的數(shù)據(jù)值
radius = "75%", # 儀表盤半徑
title_label_opts=opts.LabelOpts(
font_size=20,
color="#ECBBB5",
font_family="Microsoft YaHei", # 設(shè)置字體、顏色、大小
font_weight = "bolder",
),
axisline_opts=opts.AxisLineOpts(
linestyle_opts=opts.LineStyleOpts(
color=[(0.3, "#FFDFE3"), (0.7, "#F3BFCC"), (1, "#FB92A3")], width=30 # 設(shè)置區(qū)間顏色、儀表寬度
)
),
detail_label_opts = opts.GaugeDetailOpts( # 配置數(shù)字顯示位置以及字體、顏色等
is_show=True,
offset_center = [0,'40%'], # 數(shù)字相對(duì)位置,可以是絕對(duì)數(shù)值,也可以是百分比
color = '#5A3147', # 文字顏色
formatter="{}%".format(num), # 文字格式化
font_style = "oblique", # 文字風(fēng)格
font_weight = "bold", #字重
font_size = 35,
),
)
gauge.set_series_opts( # 自定義圖表樣式
itemstyle_opts={
"normal": {
# 調(diào)整指針顏色漸變
'shadowBlur': 8, # 光影大小
"shadowColor": "#E9B7D3", # 調(diào)整陰影顏色
'shadowOffsetY': 6,
'shadowOffsetX': 6, # 偏移量
}
}
)
gauge.set_global_opts(
legend_opts=opts.LegendOpts(
is_show=True, # 是否顯示圖例
pos_left='right', # 圖例顯示位置
# pos_top='3%', #圖例距離頂部的距離
orient='vertical', # 圖例水平布局
textstyle_opts=opts.TextStyleOpts(
color='#5A3147', # 顏色
font_size='13', # 字體大小
font_weight='bolder', # 加粗
),
),
)
return gauge.render_notebook()
chart_gauge(view_cart_rate,title = '瀏覽-加購(gòu)總體轉(zhuǎn)化率',label = '轉(zhuǎn)化率')
chart_gauge(cart_order,title = '加購(gòu)-購(gòu)買總體轉(zhuǎn)化率',label = '轉(zhuǎn)化率')
chart_gauge(cart_rate,title = '總體加購(gòu)率',label = '加購(gòu)率')
chart_gauge(order_rate,title = '總體購(gòu)買率',label = '購(gòu)買率')
1.繪制各環(huán)節(jié)的銷售漏斗圖可以發(fā)現(xiàn),用戶從瀏覽-加入購(gòu)物車的轉(zhuǎn)換率較低,大部分用戶在瀏覽了商品以后,就直接劃走了,并不會(huì)加入購(gòu)物車,瀏覽到加購(gòu)的總體轉(zhuǎn)換率只有15.04%,這個(gè)數(shù)據(jù)并不是很客觀,原因有可能是商品推薦的并不是用戶喜歡的想購(gòu)買的,或者是用戶在瀏覽了產(chǎn)品后,并不會(huì)被商品詳情頁(yè)吸引,因此需要進(jìn)一步查看從瀏覽-加入購(gòu)物車過(guò)程中,到底在哪一部分使得用戶放棄了加入購(gòu)物車。
2.繪制各環(huán)節(jié)轉(zhuǎn)化率的儀表盤,可以發(fā)現(xiàn),加購(gòu)到購(gòu)買的轉(zhuǎn)化率還可以,有41.5%,說(shuō)明加入購(gòu)物車的客戶還是有比較強(qiáng)的購(gòu)買欲,應(yīng)該盡量做多活動(dòng),新品競(jìng)品吸引客戶,引導(dǎo)客戶加入購(gòu)物車,提高購(gòu)買量。
3.總體購(gòu)買率同樣也不高,僅有6.24%,瀏覽-購(gòu)買的轉(zhuǎn)換率也不高,因此要優(yōu)化對(duì)用戶的推薦,盡量推薦其想要的產(chǎn)品,產(chǎn)品有降價(jià)、新品、活動(dòng)等及時(shí)推送給用戶,要注重維護(hù)平臺(tái)老用戶。加強(qiáng)會(huì)員管理等。
4 產(chǎn)品數(shù)據(jù)分析
銷量
new_data = data[data['行為類別'] == 'Order']
product_counts = new_data[['日期','產(chǎn)品id']].groupby("日期").count().reset_index()
product_counts.rename(columns={'產(chǎn)品id':'銷售量'},inplace=True)
product_counts.head()
# 繪制平滑好看的折線圖函數(shù)
def echarts_line2(x,y,title = '主標(biāo)題',subtitle = '副標(biāo)題',label = '圖例'):
"""
x: 函數(shù)傳入x軸標(biāo)簽數(shù)據(jù)
y:函數(shù)傳入y軸數(shù)據(jù)
title:主標(biāo)題
subtitle:副標(biāo)題
label:圖例
"""
line = Line(
init_opts=opts.InitOpts(
bg_color='#E8C5CB50', # 設(shè)置背景顏色
theme='essos', # 設(shè)置主題
width='1200px', # 設(shè)置圖的寬度
height='600px', # 設(shè)置圖的高度
)
)
line.add_xaxis(x)
line.add_yaxis(
label,
y,
is_symbol_show=False, # 是否顯示數(shù)據(jù)標(biāo)簽點(diǎn)
is_smooth=True, # 設(shè)置曲線平滑
label_opts=opts.LabelOpts(
is_show=False, # 是否顯示數(shù)據(jù)
),
itemstyle_opts=opts.ItemStyleOpts(color='#00ca95'), # 設(shè)置系列顏色
# 線條粗細(xì)陰影設(shè)置
linestyle_opts={
"normal": {
"color": "#E47085", #線條顏色
"shadowColor": '#D99AAD60', #陰影顏色和不透明度
"shadowBlur": 8, #陰影虛化大小
"shadowOffsetY": 20, #陰影y偏移量
"shadowOffsetX": 20, #陰影x偏移量
"width": 7 # 線條粗細(xì)
},
},
)
line.set_global_opts(
# 標(biāo)題設(shè)置
title_opts=opts.TitleOpts(
title=title, # 主標(biāo)題
subtitle=subtitle, # 副標(biāo)題
pos_left='center', # 標(biāo)題展示位置
title_textstyle_opts=dict(color='#5A3147'), # 設(shè)置標(biāo)題字體顏色
subtitle_textstyle_opts=dict(color='#5A3147')
),
# 圖例設(shè)置
legend_opts=opts.LegendOpts(
is_show=True, # 是否顯示圖例
pos_left='right', # 圖例顯示位置
pos_top='3%', #圖例距離頂部的距離
orient='horizontal', # 圖例水平布局
textstyle_opts=opts.TextStyleOpts(
color='#5A3147', # 顏色
font_size='13', # 字體大小
font_weight='bolder', # 加粗
),
),
tooltip_opts=opts.TooltipOpts(
is_show=True, # 是否使用提示框
trigger='axis', # 觸發(fā)類型
is_show_content = True,
trigger_on='mousemove|click', # 觸發(fā)條件,點(diǎn)擊或者懸停均可出發(fā)
axis_pointer_type='cross', # 指示器類型,鼠標(biāo)移動(dòng)到圖表區(qū)可以查看效果
# formatter = '{a}<br>:{c}人' # 文本內(nèi)容
),
datazoom_opts=opts.DataZoomOpts(
range_start=0, # 開始范圍
range_end=50, # 結(jié)束范圍
# orient='vertical', # 設(shè)置為垂直布局
type_='slider', # slider形式
is_zoom_lock=False, # 鎖定區(qū)域大小
# pos_left='1%' # 設(shè)置位置
),
yaxis_opts=opts.AxisOpts(
is_show=True,
splitline_opts=opts.SplitLineOpts(is_show=False), # 分割線
axistick_opts=opts.AxisTickOpts(is_show=False), # 刻度不顯示
axislabel_opts=opts.LabelOpts( # 坐標(biāo)軸標(biāo)簽配置
font_size=13, # 字體大小
font_weight='bolder' # 字重
),
), # 關(guān)閉Y軸顯示
xaxis_opts=opts.AxisOpts(
boundary_gap=False, # 兩邊不顯示間隔
axistick_opts=opts.AxisTickOpts(is_show=True), # 刻度不顯示
splitline_opts=opts.SplitLineOpts(is_show=False), # 分割線不顯示
axisline_opts=opts.AxisLineOpts(is_show=True), # 軸不顯示
axislabel_opts=opts.LabelOpts( # 坐標(biāo)軸標(biāo)簽配置
font_size=13, # 字體大小
font_weight='bolder' # 字重
),
),
)
return line.render_notebook()
echarts_line2(product_counts['日期'].tolist(),product_counts['銷售量'],title = '日銷售量變化圖',subtitle = '每日銷售量變化',label = '銷售量')
因?yàn)閿?shù)據(jù)上沒有提供商品單價(jià),所以只能分析產(chǎn)品銷售數(shù)量
產(chǎn)品銷售數(shù)量和UV,PV,有很大關(guān)聯(lián),趨勢(shì)與UV,PV曲線基本一致,都是情人節(jié)前出現(xiàn)高峰,產(chǎn)品銷售量高,之后慢慢恢復(fù)正常趨勢(shì)。
product_id_counts = new_data[['月份','產(chǎn)品類別','客戶id']].groupby(['月份','產(chǎn)品類別']).count().sort_values(by=['月份','客戶id'],ascending=[True,False]).reset_index()
product_id_counts.rename(columns={'客戶id':'月銷售量'},inplace=True)
product_id_counts.head()
month2_data = product_id_counts[product_id_counts['月份'] == 2].iloc[:10].sort_values(by = '月銷售量',ascending = True)
month3_data = product_id_counts[product_id_counts['月份'] == 3].iloc[:10].sort_values(by = '月銷售量',ascending = True)
month4_data = product_id_counts[product_id_counts['月份'] == 4].iloc[:10].sort_values(by = '月銷售量',ascending = True)
# 繪制動(dòng)態(tài)榜單
month_lis = ['2018年2月','2018年3月','2018年4月']
month_data_lis = [month2_data,month3_data,month4_data]
# 新建一個(gè)timeline對(duì)象
tl = Timeline(
init_opts=opts.InitOpts(
bg_color='#E8C5CB50', # 設(shè)置背景顏色
theme='essos', # 設(shè)置主題
width='1200px', # 設(shè)置圖的寬度
height='700px' # 設(shè)置圖的高度
)
)
tl.add_schema(
is_auto_play = True, # 是否自動(dòng)播放
play_interval = 1500, # 播放速度
is_loop_play = True, # 是否循環(huán)播放
)
for i,data1 in zip(month_lis,month_data_lis):
day = i
bar = Bar(
init_opts=opts.InitOpts(
bg_color='#E8C5CB50', # 設(shè)置背景顏色
theme='essos', # 設(shè)置主題
width='1200px', # 設(shè)置圖的寬度
height='700px' # 設(shè)置圖的高度
)
)
bar.add_xaxis(data1['產(chǎn)品類別'].tolist())
bar.add_yaxis(
'月銷售量',
data1['月銷售量'].round(2).tolist(),
category_gap="40%"
)
bar.reversal_axis()
bar.set_series_opts( # 自定義圖表樣式
label_opts=opts.LabelOpts(is_show=True,position = "right"), # 是否顯示數(shù)據(jù)標(biāo)簽
itemstyle_opts={
"normal": {
"color": JsCode(
"""new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
offset: 0,color: '#EFA0AB'}
,{offset: 1,color: '#E47085'}], false)
"""
), # 調(diào)整柱子顏色漸變
'shadowBlur': 8, # 光影大小
"barBorderRadius": [100, 100, 100, 100], # 調(diào)整柱子圓角弧度
"shadowColor": "#E9B7D3", # 調(diào)整陰影顏色
'shadowOffsetY': 6,
'shadowOffsetX': 6, # 偏移量
}
}
)
bar.set_global_opts(
# 標(biāo)題設(shè)置
title_opts=opts.TitleOpts(
title='每月不同產(chǎn)品月銷售量top榜單', # 主標(biāo)題
subtitle='各品類月銷售量動(dòng)態(tài)榜單', # 副標(biāo)題
pos_left='center', # 標(biāo)題展示位置
title_textstyle_opts=dict(color='#5A3147'), # 設(shè)置標(biāo)題字體顏色
subtitle_textstyle_opts=dict(color='#5A3147')
),
legend_opts=opts.LegendOpts(
is_show=True, # 是否顯示圖例
pos_left='right', # 圖例顯示位置
pos_top='3%', #圖例距離頂部的距離
orient='vertical', # 圖例水平布局
textstyle_opts=opts.TextStyleOpts(
color='#5A3147', # 顏色
font_size='13', # 字體大小
font_weight='bolder', # 加粗
),
),
tooltip_opts=opts.TooltipOpts(
is_show=True, # 是否使用提示框
trigger='axis', # 觸發(fā)類型
is_show_content = True,
trigger_on='mousemove|click', # 觸發(fā)條件,點(diǎn)擊或者懸停均可出發(fā)
axis_pointer_type='cross', # 指示器類型,鼠標(biāo)移動(dòng)到圖表區(qū)可以查看效果
# formatter = '{a}<br>:{c}人' # 文本內(nèi)容
),
yaxis_opts=opts.AxisOpts(
is_show=True,
splitline_opts=opts.SplitLineOpts(is_show=False), # 分割線
axistick_opts=opts.AxisTickOpts(is_show=False), # 刻度不顯示
axislabel_opts=opts.LabelOpts( # 坐標(biāo)軸標(biāo)簽配置
font_size=13, # 字體大小
font_weight='bolder' # 字重
),
), # 關(guān)閉Y軸顯示
xaxis_opts=opts.AxisOpts(
boundary_gap=True, # 兩邊不顯示間隔
axistick_opts=opts.AxisTickOpts(is_show=True), # 刻度不顯示
splitline_opts=opts.SplitLineOpts(is_show=False), # 分割線不顯示
axisline_opts=opts.AxisLineOpts(is_show=True), # 軸不顯示
axislabel_opts=opts.LabelOpts( # 坐標(biāo)軸標(biāo)簽配置
font_size=13, # 字體大小
font_weight='bolder' # 字重
),
),
)
tl.add(bar, day)
tl.render_notebook()
從時(shí)間序列維度進(jìn)行了銷售額的分析,當(dāng)然也要從產(chǎn)品的維度進(jìn)行分析了。計(jì)算出了每個(gè)月銷售額top10的產(chǎn)品類別榜單,查看各個(gè)產(chǎn)品的每月銷售額的變化,哪個(gè)月哪個(gè)產(chǎn)品是爆款
5 建立回歸模型
分析哪些因素能夠更有效的提高客戶量
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from matplotlib.pylab import date2num
# 1.準(zhǔn)備數(shù)據(jù)
jd = data[data['行為類別'] == 'Order']
jd = jd[['客戶id','開店時(shí)間','店鋪打分','粉絲數(shù)','會(huì)員數(shù)','店鋪id']]
jd.dropna(inplace=True)
# 將開店時(shí)間轉(zhuǎn)化為數(shù)值
jd['開店時(shí)間'] = date2num(jd['開店時(shí)間'])
# 每個(gè)店鋪客戶人數(shù)
U_data = jd[['客戶id','開店時(shí)間','店鋪打分','粉絲數(shù)','會(huì)員數(shù)','店鋪id']].groupby(['店鋪id','開店時(shí)間','店鋪打分','粉絲數(shù)','會(huì)員數(shù)']).count().reset_index()
U_data.rename(columns={'客戶id':'客戶數(shù)'},inplace=True)
# 建立門店的特征值與特征向量
U_data.data = U_data[['開店時(shí)間','店鋪打分','粉絲數(shù)','會(huì)員數(shù)']]
U_data.target = U_data['客戶數(shù)']
# 2.數(shù)據(jù)集劃分
x_train,x_test,y_train,y_test = train_test_split(U_data.data,U_data.target,test_size=0.2,random_state=2)
# 3.特征工程—標(biāo)準(zhǔn)化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
# 4.機(jī)器學(xué)習(xí)-線性回歸(正規(guī)方程)
estimator = LinearRegression()
estimator.fit(x_train,y_train)
# 5.模型評(píng)估
U_predict = estimator.predict(x_test)
print("預(yù)測(cè)值為:\n",U_predict[:10])
print("模型中的系數(shù)為:\n",estimator.coef_)
print("模型中的偏置為:\n",estimator.intercept_)
# 5.2 評(píng)價(jià)
# 均方誤差
error = mean_squared_error(y_test, U_predict)
print("誤差為:\n", error)
模型分析:回歸模型可以判斷部分字段對(duì)提高客戶量的有效率,同時(shí)能夠根據(jù)當(dāng)前數(shù)據(jù),門店配置預(yù)測(cè)出相同時(shí)間段的客戶量,為相關(guān)活動(dòng)規(guī)劃提供數(shù)據(jù)支持。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-443284.html
本次模型的4個(gè)系數(shù)對(duì)應(yīng)的字段分別是[‘開店時(shí)間’,‘店鋪打分’,‘粉絲數(shù)’,‘會(huì)員數(shù)’],各系數(shù)大小代表對(duì)客戶量的影響大小,其中多次隨機(jī)開店時(shí)間系數(shù)始終為負(fù)數(shù),表明開店的時(shí)間越大(時(shí)間大,門店新),客戶量越少,符合正常情況,而會(huì)員數(shù)的系數(shù)始終是最大的,而且占比很大,說(shuō)明對(duì)客戶量的影響很大,所以我們更應(yīng)該在吸引新會(huì)員,維護(hù)老會(huì)員老顧客方面下功夫,多活動(dòng)多促銷,增加用戶黏度。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-443284.html
6 最后
到了這里,關(guān)于【畢業(yè)設(shè)計(jì)】基于大數(shù)據(jù)的京東消費(fèi)行為分析與可視化 - python 機(jī)器學(xué)習(xí)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!