一、数据来源
本节选用的是 Python 的第三方库 seaborn 自带的数据集,该小费数据集为餐饮行业收集的数据,其中 total_bill 为消费总金额、tip 为小费金额、sex 为顾客性别、smoker 为顾客是否吸烟、day 为消费的星期、time 为聚餐的时间段、size 为聚餐人数。
import numpy as np
from pandas import Series,DataFrame
import pandas as pd
import seaborn as sns #导入seaborn库
tips=sns.load_dataset('tips') #seaborn库自带的数据集
tips.head()
AI 代码解读
二、问题探索二、问题探索
- 小费金额与消费总金额是否存在相关性?
- 性别、是否吸烟、星期几、聚餐人数和小费金额是否有一定的关联?
- 小费金额占小费总金额的百分比是否服从正态分布?
三、数据清洗
tips.shape #数据集的维度
AI 代码解读
(244,7)
共有 244 条数据,7 列。
tips.describe() #描述统计
AI 代码解读
描述统计结果如上所示。
tips.info() #查看缺失值信息
AI 代码解读
此例无缺失值。
四、数据探索
tips.plot(kind='scatter',x='total_bill',y='tip') #绘制散点图
AI 代码解读
由图可看出,小费金额与消费总金额存在正相关性。
import numpy as np
from pandas import Series,DataFrame
import pandas as pd
import seaborn as sns #导入seaborn库
tips=sns.load_dataset('tips')#seaborn库自带的数据集
tips.head()
AI 代码解读
3.0896178343949052
female_tip = tips[tips['sex'] == 'Female']['tip'].mean() #女性平均消费金额female_tip
AI 代码解读
2.833448275862069
s = Series([male_tip,female_tip],index=['male','female'])
s
AI 代码解读
male 3.089618
female 2.833448
dtype: float64
s.plot(kind='bar') #男女平均小费柱状图
AI 代码解读
由图可看出,女性小费金额小于男性小费金额。
sun_tip = tips[tips['day'] == 'Sun']['tip'].mean()
sat_tip = tips[tips['day'] == 'Sat']['tip'].mean()
thur_tip = tips[tips['day'] == 'Thur']['tip'].mean()
fri_tip = tips[tips['day'] == 'Fri']['tip'].mean()#各个日期的平均小费值
s = Series([thur_tip,fri_tip,sat_tip,sun_tip],index=['Thur','Fri','Sat','Sun'])
s
AI 代码解读
s.plot(kind='bar') #日期平均小费柱状图
AI 代码解读
由图可看出,周六、周日的小费比周四、周五的小费高。
tips['percent_tip'] = tips['tip']/(tips['total_bill']+tips['tip'])
tips.head(10) #小费所占百分比
AI 代码解读
tips['percent_tip'].hist(bins=50)#小费百分比直方图
AI 代码解读
由图可看出,小费金额占小费总金额的百分比基本服从正态分布。