数据分析:基于随机森林(RFC)对酒店预订分析预测
Posted AOAIYII
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据分析:基于随机森林(RFC)对酒店预订分析预测相关的知识,希望对你有一定的参考价值。
数据分析:基于随机森林(RFC)对酒店预订分析预测
作者:AOAIYI
作者简介:Python领域新星作者、多项比赛获奖者:AOAIYI首页
😊😊😊如果觉得文章不错或能帮助到你学习,可以点赞👍收藏📁评论📒+关注哦!👍👍👍
📜📜📜如果有小伙伴需要数据集和学习交流,文章下方有交流学习区!一起学习进步!💪
专栏案例:数据分析 |
---|
数据分析:某电商优惠卷数据分析 |
数据分析:旅游景点销售门票和消费情况分析 |
数据分析:消费者数据分析 |
数据分析:餐厅订单数据分析 |
文章目录
1、前言
数据准备
2、数据探索
1.导入所需模块
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline
import os
for dirname, _, filenames in os.walk('/home/mw/input/'):
for filename in filenames:
print(os.path.join(dirname, filename))
2.导入数据
df=pd.read_csv('/home/mw/input/1119442/hotel_bookings.csv')
df.head()
3.查看每列空值占比
df.isnull().mean()
4.查看数据基本信息
df.info()
5.使用describe()函数,计算数据集中每列的总数、均值、标准差、最小值、25%、50%、75%分位数以及最大值并转置。
df.describe([0.01,0.05,0.1,0.25,0.5,0.75,0.99]).T
3、数据可视化分析
3.1酒店预订量和取消量
plt.figure(figsize=(15,8))
sns.countplot(x='hotel'
,data=df
,hue='is_canceled'
,palette=sns.color_palette('Set2',2)
)
hotel_cancel=(df.loc[df['is_canceled']==1]['hotel'].value_counts()/df['hotel'].value_counts()).sort_values(ascending=False)
print('酒店取消率'.center(20),hotel_cancel,sep='\\n')
City Hotel的预定量与取消量都高于Resort Hotel,但Resort Hotel取消率为27.8%,而City Hotel的取消率达到了41.7%
3.2酒店各月份预定量
city_hotel=df[(df['hotel']=='City Hotel') & (df['is_canceled']==0)]
resort_hotel=df[(df['hotel']=='Resort Hotel') & (df['is_canceled']==0)]
for i in [city_hotel,resort_hotel]:
i.index=range(i.shape[0])
city_month=city_hotel['arrival_date_month'].value_counts()
resort_month=resort_hotel['arrival_date_month'].value_counts()
name=resort_month.index
x=list(range(len(city_month.index)))
y=city_month.values
x1=[i+0.3 for i in x]
y1=resort_month.values
width=0.3
plt.figure(figsize=(15,8),dpi=80)
plt.plot(x,y,label='City Hotel',color='lightsalmon')
plt.plot(x1,y1,label='Resort Hotel',color='lightseagreen')
plt.xticks(x,name)
plt.legend()
plt.xlabel('Month')
plt.ylabel('Count')
plt.title('Month Book')
for x,y in zip(x,y):
plt.text(x,y+0.1,'%d' % y,ha = 'center',va = 'bottom')
for x,y in zip(x1,y1):
plt.text(x,y+0.1,'%d' % y,ha = 'center',va = 'bottom')
3.3客源地与预订取消率
country_book=df['country'].value_counts()[:10]
country_cancel=df[(df.country.isin (country_book.index)) & (df.is_canceled==1)]['country'].value_counts()
plt.figure(figsize=(15,8))
sns.countplot(x='country'
,data=df[df.country.isin (country_book.index)]
,hue='is_canceled'
,palette=sns.color_palette('Set2',2)
)
plt.title('Main Source of Guests')
country_cancel_rate=(country_cancel/country_book).sort_values(ascending=False)
print('各国客户取消率'.center(10),country_cancel_rate,sep='\\n')
Resort hotel和City hotel的旺季均为夏季7、8月份,且客源主要为欧洲国家,符合欧洲游客偏爱夏季出游的特点,需要重点关注来自葡萄牙(PRT)和英国(BRT)等取消率高的主要客源
3.4客户类型
city_customer=city_hotel.customer_type.value_counts()
resort_customer=resort_hotel.customer_type.value_counts()
plt.figure(figsize=(21,12),dpi=80)
plt.subplot(1,2,1)
plt.pie(city_customer,labels=city_customer.index,autopct='%.2f%%')
plt.legend(loc=1)
plt.title('City Hotel Customer Type')
plt.subplot(1,2,2)
plt.pie(resort_customer,labels=resort_customer.index,autopct='%.2f%%')
plt.title('Resort Hotel Customer Type')
plt.legend()
plt.show()
酒店的主要客户类型都是散客(Transient),占比均为70%左右
3.5酒店预订途径
city_segment=city_hotel.market_segment.value_counts()
resort_segment=resort_hotel.market_segment.value_counts()
plt.figure(figsize=(21,12),dpi=80)
plt.subplot(1,2,1)
plt.pie(city_segment,labels=city_segment.index,autopct='%.2f%%')
plt.legend()
plt.title('City Hotel Market Segment')
plt.subplot(1,2,2)
plt.pie(resort_segment,labels=resort_segment.index,autopct='%.2f%%')
plt.title('Resort Hotel Market Segment')
plt.legend()
plt.show()
两间酒店的客源主要来自线上旅游机构,其在City Hotel的占比甚至超过5成;线下旅游机构的比重次之,均为20%左右
3.6各类旅客日均开销
plt.figure(figsize=(15,8))
sns.boxplot(x='customer_type'
,y='adr'
,hue='hotel'
,data=df[df.is_canceled==0]
,palette=sns.color_palette('Set2',2)
)
plt.title('Average Daily Rate of Different Customer Type')
City Hotel各类客户的日均开销均高于Resort Hotel;在四种类型的客户中,散客(Transient)的消费最高,团体客(Group)最低
3.7新老客数量与取消预订率
plt.figure(figsize=(15,8))
sns.countplot(x='is_repeated_guest'
,data=df
,hue='is_canceled'
,palette=sns.color_palette('Set2',2)
)
plt.title('New/Repeated Guest Amount')
plt.xticks(range(2),['no','yes'])
guest_cancel=(df.loc[df['is_canceled']==1]['is_repeated_guest'].value_counts()/df['is_repeated_guest'].value_counts()).sort_values(ascending=False)
guest_cancel.index=['New Guest', 'Repeated Guest']
print('新老客取消率'.center(15),guest_cancel,sep='\\n')
老客的取消率为14.4%,而新客的取消率则达到了37.8%,高出老客24个百分点
3.8押金方式与预定取消率
print('三种押金方式预定量'.center(15),df['deposit_type'].value_counts(),sep='\\n')
deposit_cancel=(df.loc[df['is_canceled']==1]['deposit_type'].value_counts()/df['deposit_type'].value_counts()).sort_values(ascending=False)
plt.figure(figsize=(8,5))
x=range(len(deposit_cancel.index))
y=deposit_cancel.values
plt.bar(x,y,label='Cancel_Rate',color=['orangered','lightsalmon','lightseagreen'],width=0.4)
plt.xticks(x,deposit_cancel.index)
plt.legend()
plt.title('Cancel Rate of Deposite Type')
for x,y in zip(x,y):
plt.text(x,y,'%.2f' % y,ha = 'center',va = 'bottom')
无需押金(‘No Deposit’)是预定量最高的方式,且取消率较低,而不退押金(Non Refund)这一类型的取消预订率高达99%,可以减少这一类型的押金方式以减少客户取消率
3.9房间类型与预定取消量
plt.figure(figsize=(15,8))
sns.countplot(x='assigned_room_type'
,data=df
,hue='is_canceled'
,palette=sns.color_palette('Set2',2)
)
plt.title('Book & Cancel Amount of Room Type')
room_cancel=df.loc[df['is_canceled']==1]['assigned_room_type'].value_counts()[:7]/df['assigned_room_type'].value_counts()[:7]
print('不同房型取消率'.center(5),room_cancel.sort_values(ascending=False),sep='\\n')
在预定量前7的房型中,A、G房型的取消率均高于其他房型,A房型的取消率更是高达44.5%
4.数据预处理
建模的目的是为了预测旅客是否会取消酒店的预订,后续需要将’is_canceled’设为标签y,其余为特征x。日期特征’is_cance’reservation_status_date’不会直接影响标签,所以删除
df1=df.drop(labels=['reservation_status_date'],axis=1)
4.1处理分类型变量
cate=df1.columns[df1.dtypes == "object"].tolist()
#用数字表现的分类型变量
num_cate=['agent','company','is_repeated_guest']
cate=cate+num_cate
results=
for i in ['agent','company']:
result=np.sort(df1[i].unique())
results[i]=result
results
agent和company列空值占比较多且无0值,所以用0填补
df1[['agent','company']]=df1[['agent','company']].fillna(0,axis=0)
df1.loc[:,cate].isnull().mean()
创造新变量in_company和in_agent对旅客分类,company和agent为0的设为NO,非0的为YES
df1.loc[df1['company'] == 0,'in_company']='NO'
df1.loc[df1['company'] != 0,'in_company']='YES'
df1.loc[df1['agent'] == 0,'in_agent']='NO'
df1.loc[df1['agent'] != 0,'in_agent']='YES'
创造新特征same_assignment,若预订的房间类型和分配的类型一致则为yes,反之为no
df1.loc[df1['reserved_room_type'] == df1['assigned_room_type'],'same_assignment']='Yes'
df1.loc[df1['reserved_room_type'] != df1['assigned_room_type'],'same_assignment']='No'
删除’reserved_room_type’,‘assigned_room_type’,‘agent’,'company’四个特征
df1=df1.drop(labels=['reserved_room_type','assigned_room_type','agent','company'],axis=1)
重新设置’is_repeated_guest’,常客标为YES,非常客为NO
df1['is_repeated_guest'][df1['is_repeated_guest']==0]='NO'
df1['is_repeated_guest'][df1['is_repeated_guest']==1]='YES'
df1['country']=df1['country'].fillna(df1['country'].mode()[0])
for i in ['in_company','in_agent','same_assignment']:
cate.append(i)
for i in ['reserved_room_type','assigned_room_type','agent','company']:
cate.remove(i)
cate
对分类型特征进行编码
from sklearn.preprocessing import OrdinalEncoder
oe = OrdinalEncoder()
oe = oe.fit(df1.loc[:,cate])
df1.loc[:,cate] = oe.transform(df1.loc[:,cate])
4.2处理连续型变量
筛选出连续型变量,需要先删除’is_canceled’这一标签
col=df1.columns.tolist()
col.remove('is_canceled')
for i in cate:
col.remove(i)
col
统计空值
df1[col].isnull().sum()
使用众数填补xtrain children列的空值
df1['children']=df1['children'].fillna(df1['children'].mode()[0])
以上是关于数据分析:基于随机森林(RFC)对酒店预订分析预测的主要内容,如果未能解决你的问题,请参考以下文章基于回归分析的广告投入销售额预测——K邻近,决策树,随机森林,线性回归,岭回归