数据分析_小费数据集描述性分析

项目介绍

众所周知,在西方国家的服务行业中,顾客会给服务员一定金额的小费。本次项目研究对象是餐饮行业收集到的小费数据。

数据获取

本次项目的数据来源是python第三方库seaborn中自带的数据。数据集中含有7个字段,包括有消费总金额(totall_bill)(不含小费),小费金额(tip),顾客性别(sex),消费的星期(day),消费的时间段(time),用餐人数(size),顾客是否抽烟(smoker)

# 设置cell多行输出

from IPython.core.interactiveshell import InteractiveShell 
InteractiveShell.ast_node_interactivity = 'all' #默认为'last'
# 导入相关库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
# 导入数据集
tips = sns.load_dataset('tips')
tips.head()
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4

定义问题

本次研究将围绕小费数据集进行。

研究小费金额与消费总金额是否存在相关性?

小费金额与消费的日期,时间段,用餐人数以及顾客是否吸烟是否存在一定的关联?

数据清洗与整理

tips.info()  # 查看数据结构

# 数据结构 (244,7),也就是一共244条数据,包含7个字段的信息
# 从结构数据返回,观察不存在缺失数据,且各列的数据类型也符合实际情况
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
total_bill    244 non-null float64
tip           244 non-null float64
sex           244 non-null category
smoker        244 non-null category
day           244 non-null category
time          244 non-null category
size          244 non-null int64
dtypes: category(4), float64(2), int64(1)
memory usage: 7.2 KB
tips.isna().sum() # 进一步判断是否存在缺失数据

# 不存在缺失数据
total_bill    0
tip           0
sex           0
smoker        0
day           0
time          0
size          0
dtype: int64

数据探索

1.消费总金额与小费金额的关系

# 小费金额基本情况描述
tips.describe()['tip']
count    244.000000
mean       2.998279
std        1.383638
min        1.000000
25%        2.000000
50%        2.900000
75%        3.562500
max       10.000000
Name: tip, dtype: float64
tips['tip'].hist(bins=20,figsize=(8,6))
plt.xlabel('tip')
plt.ylabel('freq')
plt.title('Basic information of tip amount',pad=12)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-h3vyIKcR-1586673025947)(output_11_4.png)]

# 绘制散点图查看小费金额和消费总金额的关系
sns.set(style='darkgrid',color_codes=True)
sns.lmplot(x="total_bill", y="tip",data=tips,)
plt.title('Relationship between total consumption amount and tip amountt',pad=12)

# 计算两个连续变量(消费总金额和小费金额)的pearson相关系数
corr = np.corrcoef(x=tips.total_bill,y=tips.tip)[0,1]
print('消费总金额与小费金额的相关系数pearson为:%.2f'%corr)
消费总金额与小费金额的相关系数pearson为:0.68

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AHSiorl3-1586673025947)(output_12_3.png)]

# 计算小费金额占聚合所有费用的百分比(聚合所有费用含消费总金额+小费金额)
tips['percent'] = tips['tip']/(tips['tip']+tips['total_bill'])
tips.head()
total_bill tip sex smoker day time size percent
0 16.99 1.01 Female No Sun Dinner 2 0.056111
1 10.34 1.66 Male No Sun Dinner 3 0.138333
2 21.01 3.50 Male No Sun Dinner 3 0.142799
3 23.68 3.31 Male No Sun Dinner 2 0.122638
4 24.59 3.61 Female No Sun Dinner 4 0.128014
# 描述小费百分比的分布情况
tips['percent'].hist(bins=20,figsize=(8,6))
plt.xlabel('percent',labelpad=12)
plt.ylabel('freq',labelpad=12)
plt.title('Tip percentage distribution',pad=12)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Du4NWgeI-1586673025948)(output_14_4.png)]

1.从数据集所涵盖的数据中,顾客的小费金额最高达到10美金,最少为1美金,大部分小费金额集中在均值3美金附近。

2.从散点图描述两个变量之间的关系可知,小费金额与消费总金额存在正相关的关系,表示消费的金额越多,小费给得越多。

3.其次从直方图可看出,小费金额占聚合所有花费金额的百分比分布基本符合正态分布,大多集中在均值附近,但也有几个异常的点。

2.顾客的性别与小费金额的关系

# 男顾客的小费金额分布情况
male = tips[tips['sex']=='Male']
male['tip'].hist(bins=20,figsize=(8,6))
plt.xlabel('tip')
plt.ylabel('freq')
plt.xlim([1,10])
plt.title('Tip amount distribution of male customers',pad=12)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jgeJzHb8-1586673025949)(output_17_5.png)]

# 女顾客小费金额分布情况
female = tips[tips['sex']=='Female']
female['tip'].hist(bins=20,figsize=(8,6))
plt.xlabel('tip')
plt.ylabel('freq')
plt.title('Tip amount distribution of female customers',pad=12)

在这里插入图片描述

gender = tips.groupby('sex').mean()['tip']

gender.plot(kind='bar',figsize=(8,6),rot=0)
plt.title('Relationship between gender and tip amount',pad=12)
plt.ylabel('tip_for_mean',labelpad=12)

在这里插入图片描述

1.从男顾客和女顾客的小费金额分布情况知,男顾客的小费金额最高达到10美金,最少为1美金;而女顾客的小费金额最高为6.5美金,最少同样为1美金。

2.从柱状图反映出,男顾客的小费金额高于女顾客的小费金额。

3.顾客的消费时间段与小费金额的关系

# 查看消费时间段time字段的分类 → 分为2个类别
tips['time'].cat.categories
Index(['Lunch', 'Dinner'], dtype='object')
# 按类别汇总计算平均小费金额
time = tips.groupby('time').mean()['tip']
time
time
Lunch     2.728088
Dinner    3.102670
Name: tip, dtype: float64
time.plot(kind='bar',figsize=(8,6),rot=0)
plt.ylabel('tip_for_mean')
plt.title('Relationship between consumption time and tip amount',pad=12)

在这里插入图片描述

很显然,从柱状图的分布看出,顾客晚餐时段的小费金额比午餐时段高

4.顾客聚餐的日期与小费的关系

tips['day'].cat.categories # 查看day字段的分类  

# 返回4个分类,周四,周五,周六,周日
Index(['Thur', 'Fri', 'Sat', 'Sun'], dtype='object')
# 按星期几统计平均小费金额
day = tips.groupby('day').mean()['tip']
day
day
Thur    2.771452
Fri     2.734737
Sat     2.993103
Sun     3.255132
Name: tip, dtype: float64
day.plot(kind='bar',figsize=(8,6),rot=0)
plt.ylabel('tip_for_mean')
plt.title('The relationship between the date of the dinner party and the tip',pad=12)

在这里插入图片描述

周四,周五的小费金额是最少的,到了周六的小费金额开始升高,周日的小费金额则为最高的

5.顾客吸烟与否与小费金额的关系

tips['smoker'].cat.categories  # 查看吸烟字段的分类
Index(['Yes', 'No'], dtype='object')
smoker = tips.groupby('smoker').mean()['tip']
smoker=smoker.rename(index={'Yes':'yes_smoker','No':'no_smoker'})
smoker
smoker
yes_smoker    3.008710
no_smoker     2.991854
Name: tip, dtype: float64
smoker.plot(kind='bar',figsize=(8,6),rot=0)
plt.ylabel('tip_for_mean')
plt.title('The relationship between smoking and tip amount',pad=12)

在这里插入图片描述

从上图看出,吸烟和不吸烟的顾客的小费金额基本相同,因此,顾客吸烟与否与小费的金额高低没有直接关系

6.聚餐人数与小费金额的关系

size = tips.groupby('size').mean()['tip']
size
size
1    1.437500
2    2.582308
3    3.393158
4    4.135405
5    4.028000
6    5.225000
Name: tip, dtype: float64
size.plot(figsize=(8,6))
plt.ylabel('tip_for_mean')
plt.title('The relationship between the number of diners and the amount of tips',pad=12)

在这里插入图片描述

从数据集涵盖的数据看出,1人用餐时小费金额最少,随着聚餐人数的增多,小费的金额也随之上升。

但当聚餐人数达至5人时,小费金额略有下降,而聚餐人数达至6人小费金额达到最高。

总结

1.从数据集所涵盖的数据而言,顾客的小费最低金额为1美元,最高金额可达到10美元。而小费金额与消费总金额成正相关关系,即小费金额会随着消费金额升高而升高。

2.其次,小费金额也与性别和用餐时段、日期有关。从数据集所涵盖的数据而言,男性顾客的小费金额会比女性顾客的高,并且晚餐时段的小费金额会高于午餐时段的小费金额,而且周六日的小费金额更高,尤其是周日的时间。

3.另外,小费金额还和聚餐人数有关,1人用餐时小费金额最少,然而随着聚餐人数的增多,小费随之上升;但当聚餐人数达至5人时,小费金额略有下降,聚餐人数达至6人小费金额达到最高。

4.最后,顾客吸烟与否对小费金额的高低并没有影响。

发布了10 篇原创文章 · 获赞 0 · 访问量 18

猜你喜欢

转载自blog.csdn.net/weixin_45556639/article/details/105469280