一、項(xiàng)目背景
眾所周知,在西方國家的服務(wù)行業(yè)中,顧客會(huì)給服務(wù)員一定金額的小費(fèi)。本次項(xiàng)目研究對象是餐飲行業(yè)收集到的小費(fèi)數(shù)據(jù)。
二、數(shù)據(jù)獲取
本次項(xiàng)目的數(shù)據(jù)來源是python第三方庫seaborn中自帶的數(shù)據(jù)。數(shù)據(jù)集中含有7個(gè)字段,包括有消費(fèi)總金額(totall_bill)(不含小費(fèi)),小費(fèi)金額(tip),顧客性別(sex),消費(fèi)的星期(day),消費(fèi)的時(shí)間段(time),用餐人數(shù)(size),顧客是否抽煙(smoker)
# 設(shè)置cell多行輸出
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all' #默認(rèn)為'last'
# 導(dǎo)入相關(guān)庫
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
# 導(dǎo)入數(shù)據(jù)集
tips = sns.load_dataset('tips')
tips.head()

三、定義問題
本次研究將圍繞小費(fèi)數(shù)據(jù)集進(jìn)行。
研究小費(fèi)金額與消費(fèi)總金額是否存在相關(guān)性?
小費(fèi)金額與消費(fèi)的日期,時(shí)間段,用餐人數(shù)以及顧客是否吸煙是否存在一定的關(guān)聯(lián)?
四、數(shù)據(jù)清洗與整理
tips.info() # 查看數(shù)據(jù)結(jié)構(gòu)
# 數(shù)據(jù)結(jié)構(gòu) (244,7),也就是一共244條數(shù)據(jù),包含7個(gè)字段的信息
# 從結(jié)構(gòu)數(shù)據(jù)返回,觀察不存在缺失數(shù)據(jù),且各列的數(shù)據(jù)類型也符合實(shí)際情況
<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() # 進(jìn)一步判斷是否存在缺失數(shù)據(jù)
# 不存在缺失數(shù)據(jù)
total_bill 0
tip 0
sex 0
smoker 0
day 0
time 0
size 0
dtype: int64
五、數(shù)據(jù)探索
1.消費(fèi)總金額與小費(fèi)金額的關(guān)系
# 小費(fèi)金額基本情況描述
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)

# 繪制散點(diǎn)圖查看小費(fèi)金額和消費(fèi)總金額的關(guān)系
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)
# 計(jì)算兩個(gè)連續(xù)變量(消費(fèi)總金額和小費(fèi)金額)的pearson相關(guān)系數(shù)
corr = np.corrcoef(x=tips.total_bill,y=tips.tip)[0,1]
print('消費(fèi)總金額與小費(fèi)金額的相關(guān)系數(shù)pearson為:%.2f'%corr)
消費(fèi)總金額與小費(fèi)金額的相關(guān)系數(shù)pearson為:0.68

# 計(jì)算小費(fèi)金額占聚合所有費(fèi)用的百分比(聚合所有費(fèi)用含消費(fèi)總金額+小費(fèi)金額)
tips['percent'] = tips['tip']/(tips['tip']+tips['total_bill'])
tips.head()

# 描述小費(fèi)百分比的分布情況
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)

(1)從數(shù)據(jù)集所涵蓋的數(shù)據(jù)中,顧客的小費(fèi)金額最高達(dá)到10美金,最少為1美金,大部分小費(fèi)金額集中在均值3美金附近。
(2)從散點(diǎn)圖描述兩個(gè)變量之間的關(guān)系可知,小費(fèi)金額與消費(fèi)總金額存在正相關(guān)的關(guān)系,表示消費(fèi)的金額越多,小費(fèi)給得越多。
(3)其次從直方圖可看出,小費(fèi)金額占聚合所有花費(fèi)金額的百分比分布基本符合正態(tài)分布,大多集中在均值附近,但也有幾個(gè)異常的點(diǎn)。
2.顧客的性別與小費(fèi)金額的關(guān)系
# 男顧客的小費(fèi)金額分布情況
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)

# 女顧客小費(fèi)金額分布情況
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)從男顧客和女顧客的小費(fèi)金額分布情況知,男顧客的小費(fèi)金額最高達(dá)到10美金,最少為1美金;而女顧客的小費(fèi)金額最高為6.5美金,最少同樣為1美金。
(2)從柱狀圖反映出,男顧客的小費(fèi)金額高于女顧客的小費(fèi)金額。
3.顧客的消費(fèi)時(shí)間段與小費(fèi)金額的關(guān)系
# 查看消費(fèi)時(shí)間段time字段的分類 → 分為2個(gè)類別
tips['time'].cat.categories
Index(['Lunch', 'Dinner'], dtype='object')
# 按類別匯總計(jì)算平均小費(fèi)金額
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)

很顯然,從柱狀圖的分布看出,顧客晚餐時(shí)段的小費(fèi)金額比午餐時(shí)段高
4.顧客聚餐的日期與小費(fèi)的關(guān)系
tips['day'].cat.categories # 查看day字段的分類
# 返回4個(gè)分類,周四,周五,周六,周日
Index(['Thur', 'Fri', 'Sat', 'Sun'], dtype='object')
# 按星期幾統(tǒng)計(jì)平均小費(fèi)金額
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)

周四,周五的小費(fèi)金額是最少的,到了周六的小費(fèi)金額開始升高,周日的小費(fèi)金額則為最高的
5.顧客吸煙與否與小費(fèi)金額的關(guān)系
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)

從上圖看出,吸煙和不吸煙的顧客的小費(fèi)金額基本相同,因此,顧客吸煙與否與小費(fèi)的金額高低沒有直接關(guān)系
6.聚餐人數(shù)與小費(fèi)金額的關(guān)系
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)

從數(shù)據(jù)集涵蓋的數(shù)據(jù)看出,1人用餐時(shí)小費(fèi)金額最少,隨著聚餐人數(shù)的增多,小費(fèi)的金額也隨之上升。
但當(dāng)聚餐人數(shù)達(dá)至5人時(shí),小費(fèi)金額略有下降,而聚餐人數(shù)達(dá)至6人小費(fèi)金額達(dá)到最高。
六、總結(jié)
1.從數(shù)據(jù)集所涵蓋的數(shù)據(jù)而言,顧客的小費(fèi)最低金額為1美元,最高金額可達(dá)到10美元。而小費(fèi)金額與消費(fèi)總金額成正相關(guān)關(guān)系,即小費(fèi)金額會(huì)隨著消費(fèi)金額升高而升高。
2.其次,小費(fèi)金額也與性別和用餐時(shí)段、日期有關(guān)。從數(shù)據(jù)集所涵蓋的數(shù)據(jù)而言,男性顧客的小費(fèi)金額會(huì)比女性顧客的高,并且晚餐時(shí)段的小費(fèi)金額會(huì)高于午餐時(shí)段的小費(fèi)金額,而且周六日的小費(fèi)金額更高,尤其是周日的時(shí)間。
3.另外,小費(fèi)金額還和聚餐人數(shù)有關(guān),1人用餐時(shí)小費(fèi)金額最少,然而隨著聚餐人數(shù)的增多,小費(fèi)隨之上升;但當(dāng)聚餐人數(shù)達(dá)至5人時(shí),小費(fèi)金額略有下降,聚餐人數(shù)達(dá)至6人小費(fèi)金額達(dá)到最高。
4.最后,顧客吸煙與否對小費(fèi)金額的高低并沒有影響。