三方面分析:口味、人均消费、性价比
数据加载
import os
os.chdir('E:\\Python\\数据挖掘项目\\城市餐饮店选址')
df1 = pd.read_excel('上海餐饮数据.xlsx',sheetname=0,header=0)
# sheetname=0,选取第一张表,即sheet1
# header = 0,以第一行为列名,可不写,默认header=0
数据清洗
去除缺失值,根据业务判断需要去除的值
在这去除 <= 0 的数据
# 去除缺失值
data1 = df1[['类别','口味','环境','服务','人均消费']]
data1.dropna(inplace = True)
# 去除<=0的数据
data1 = data1[(data1['口味']>0)&(data1['人均消费']>0)]
# 增加性价比列
data1['性价比'] = (data1['口味'] + data1['环境'] + data1['服务']) / data1['人均消费']
# 用箱线图查看异常值
def f1():
fig,axes = plt.subplots(1,3,figsize = (10,4))
data1.boxplot(column = ['口味'],ax = axes[0])
data1.boxplot(column = ['人均消费'],ax = axes[1])
data1.boxplot(column = ['性价比'],ax = axes[2])
# 清除异常值 以3IQR为界
def f2(data,col):
q1 = data[col].quantile(q = 0.25)
q3 = data[col].quantile(q = 0.75)
iqr = q3 - q1 # 四分位距
mi = q1 - 3*iqr
ma = q3 + 3*iqr
return data[(data[col]>mi) & (data[col]<ma)][['类别',col]]
[[col1,col2]] -- DataFrame的索引
# 分别处理每一份数据
kw = f2(data1,"口味")
rj = f2(data1,"人均消费")
xjb = f2(data1,"性价比")
# 将3个指标标准化处理并排序
def f3(data,col):
col_name = col + '_norm'
data_gp = data.groupby('类别').mean()
data_gp[col_name] = (data_gp[col] - data_gp[col].min())/(data_gp[col].max()-data_gp[col].min())
data_gp.sort_values(by = col_name,inplace = True,ascending = False)
return data_gp
kw_score = f3(kw,'口味')
rj_score = f3(rj,'人均消费')
xjb_scoew = f3(xjb,'性价比')
# 将三个标准化数值合并
# l/r_index = True ,以第一个/第二个index为键
data_final = pd.merge(kw_score,rj_score,left_index = True,right_index = True)
data_final = pd.merge(data_final,xjb_scoew,left_index = True,right_index = True)
数据集中的数据为离散型数据,此处用箱线图查看异常值不太合理
图表制作
利用bokeh可视化制作图表
相关库的导入
from bokeh.plotting import figure,show,output_file
from bokeh.models import ColumnDataSource
from bokeh.layouts import gridplot # 多图表设置
from bokeh.models import HoverTool
输出文件
output_file('city2.html')
各类图表的制作
# size为散点大小 *5 放大以便观察
data_final['size'] = data_final['口味'] * 5
# 将列名改为英文
data_final.index.name = 'type'
data_final.columns = ['kw','kw_norm','price','price_norm','xjb','xjb_norm','size']
# 创建数据
source = ColumnDataSource(data_final)
hover = HoverTool(tooltips=[('餐饮类型','@type'),
('人均消费','@price_norm'),
('性价比得分','@xjb_norm'),
('口味得分','@kw_norm')])
# 餐饮类型的得分情况
result = figure(plot_width = 800,plot_height = 250,
title="餐饮类型得分情况" ,
x_axis_label = '人均消费', y_axis_label = '性价比得分',
tools=[hover,'box_select,reset,xwheel_zoom,pan,crosshair'])
result.circle(x = 'price',y = 'xjb_norm',source = source,
line_color = 'black',line_dash = [6,4],fill_alpha = 0.6,
size = 'size')
# 口味得分情况
data_type = data_final.index.tolist() # 提取横坐标
kw = figure(plot_width=800, plot_height=250, title='口味得分',x_range=data_type,
tools=[hover,'box_select,reset,xwheel_zoom,pan,crosshair'])
kw.vbar(x='type', top='kw_norm', source=source,width=0.9, alpha = 0.8,color = 'red')
# 人均消费得分
price = figure(plot_width=800, plot_height=250, title='人均消费得分',x_range=kw.x_range,
tools=[hover,'box_select,reset,xwheel_zoom,pan,crosshair'])
price.vbar(x='type', top='price_norm', source=source,width=0.9, alpha = 0.8,color = 'green')
图表展示
p = gridplot([[result],[kw],[price]])
show(p)
网友评论