网站建设资质证书,下载应用,wordpress 小工具调用,网络营销的优势是什么数据分析#xff1a;基于随机森林(RFC)对酒店预订分析预测 作者#xff1a;AOAIYI 作者简介#xff1a;Python领域新星作者、多项比赛获奖者#xff1a;AOAIYI首页 #x1f60a;#x1f60a;#x1f60a;如果觉得文章不错或能帮助到你学习#xff0c;可以点赞#x1f…数据分析基于随机森林(RFC)对酒店预订分析预测 作者AOAIYI 作者简介Python领域新星作者、多项比赛获奖者AOAIYI首页 如果觉得文章不错或能帮助到你学习可以点赞收藏评论关注哦 如果有小伙伴需要数据集和学习交流文章下方有交流学习区一起学习进步 专栏案例数据分析数据分析某电商优惠卷数据分析数据分析旅游景点销售门票和消费情况分析数据分析消费者数据分析数据分析餐厅订单数据分析文章目录数据分析基于随机森林(RFC)对酒店预订分析预测1、前言2、数据探索3、数据可视化分析3.1酒店预订量和取消量3.2酒店各月份预定量3.3客源地与预订取消率3.4客户类型3.5酒店预订途径3.6各类旅客日均开销3.7新老客数量与取消预订率3.8押金方式与预定取消率3.9房间类型与预定取消量4.数据预处理4.1处理分类型变量4.2处理连续型变量4.3各变量的相关性系数5.建模预测5.1随机森林分析总结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.导入数据 dfpd.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]).T3、数据可视化分析
3.1酒店预订量和取消量
plt.figure(figsize(15,8))
sns.countplot(xhotel,datadf,hueis_canceled,palettesns.color_palette(Set2,2))hotel_cancel(df.loc[df[is_canceled]1][hotel].value_counts()/df[hotel].value_counts()).sort_values(ascendingFalse)
print(酒店取消率.center(20),hotel_cancel,sep\n)City Hotel的预定量与取消量都高于Resort Hotel但Resort Hotel取消率为27.8%而City Hotel的取消率达到了41.7% 3.2酒店各月份预定量
city_hoteldf[(df[hotel]City Hotel) (df[is_canceled]0)]
resort_hoteldf[(df[hotel]Resort Hotel) (df[is_canceled]0)]
for i in [city_hotel,resort_hotel]:i.indexrange(i.shape[0])city_monthcity_hotel[arrival_date_month].value_counts()
resort_monthresort_hotel[arrival_date_month].value_counts()
nameresort_month.index
xlist(range(len(city_month.index)))
ycity_month.values
x1[i0.3 for i in x]
y1resort_month.values
width0.3
plt.figure(figsize(15,8),dpi80)
plt.plot(x,y,labelCity Hotel,colorlightsalmon)
plt.plot(x1,y1,labelResort Hotel,colorlightseagreen)
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,y0.1,%d % y,ha center,va bottom)for x,y in zip(x1,y1):plt.text(x,y0.1,%d % y,ha center,va bottom)3.3客源地与预订取消率
country_bookdf[country].value_counts()[:10]
country_canceldf[(df.country.isin (country_book.index)) (df.is_canceled1)][country].value_counts()
plt.figure(figsize(15,8))
sns.countplot(xcountry,datadf[df.country.isin (country_book.index)],hueis_canceled,palettesns.color_palette(Set2,2))
plt.title(Main Source of Guests)country_cancel_rate(country_cancel/country_book).sort_values(ascendingFalse)
print(各国客户取消率.center(10),country_cancel_rate,sep\n)Resort hotel和City hotel的旺季均为夏季7、8月份且客源主要为欧洲国家符合欧洲游客偏爱夏季出游的特点需要重点关注来自葡萄牙(PRT)和英国(BRT)等取消率高的主要客源 3.4客户类型
city_customercity_hotel.customer_type.value_counts()
resort_customerresort_hotel.customer_type.value_counts()
plt.figure(figsize(21,12),dpi80)
plt.subplot(1,2,1)
plt.pie(city_customer,labelscity_customer.index,autopct%.2f%%)
plt.legend(loc1)
plt.title(City Hotel Customer Type)
plt.subplot(1,2,2)
plt.pie(resort_customer,labelsresort_customer.index,autopct%.2f%%)
plt.title(Resort Hotel Customer Type)
plt.legend()
plt.show()酒店的主要客户类型都是散客(Transient)占比均为70%左右 3.5酒店预订途径
city_segmentcity_hotel.market_segment.value_counts()
resort_segmentresort_hotel.market_segment.value_counts()
plt.figure(figsize(21,12),dpi80)
plt.subplot(1,2,1)
plt.pie(city_segment,labelscity_segment.index,autopct%.2f%%)
plt.legend()
plt.title(City Hotel Market Segment)
plt.subplot(1,2,2)
plt.pie(resort_segment,labelsresort_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(xcustomer_type,yadr,huehotel,datadf[df.is_canceled0],palettesns.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(xis_repeated_guest,datadf,hueis_canceled,palettesns.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(ascendingFalse)
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(ascendingFalse)
plt.figure(figsize(8,5))
xrange(len(deposit_cancel.index))
ydeposit_cancel.values
plt.bar(x,y,labelCancel_Rate,color[orangered,lightsalmon,lightseagreen],width0.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(xassigned_room_type,datadf,hueis_canceled,palettesns.color_palette(Set2,2))
plt.title(Book Cancel Amount of Room Type)room_canceldf.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(ascendingFalse),sep\n)在预定量前7的房型中A、G房型的取消率均高于其他房型A房型的取消率更是高达44.5% 4.数据预处理
建模的目的是为了预测旅客是否会取消酒店的预订后续需要将’is_canceled’设为标签y其余为特征x。日期特征’is_cance’reservation_status_date’不会直接影响标签所以删除
df1df.drop(labels[reservation_status_date],axis1)4.1处理分类型变量 catedf1.columns[df1.dtypes object].tolist()
#用数字表现的分类型变量
num_cate[agent,company,is_repeated_guest]
catecatenum_cateresults{}
for i in [agent,company]:resultnp.sort(df1[i].unique())results[i]result
resultsagent和company列空值占比较多且无0值所以用0填补
df1[[agent,company]]df1[[agent,company]].fillna(0,axis0)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’四个特征
df1df1.drop(labels[reserved_room_type,assigned_room_type,agent,company],axis1)重新设置’is_repeated_guest’常客标为YES非常客为NO
df1[is_repeated_guest][df1[is_repeated_guest]0]NO
df1[is_repeated_guest][df1[is_repeated_guest]1]YESdf1[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’这一标签
coldf1.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])连续型变量进行无量纲化
from sklearn.preprocessing import StandardScaler
ss StandardScaler()
ss ss.fit(df1.loc[:,col])
df1.loc[:,col] ss.transform(df1.loc[:,col])4.3各变量的相关性系数
cordf1.corr()
corabs(cor[is_canceled]).sort_values()
corplt.figure(figsize(8,15))
xrange(len(cor.index))
namecor.index
yabs(cor.values)
plt.barh(x,y,colorsalmon)
plt.yticks(x,name)
for x,y in zip(x,y):plt.text(y,x-0.1,%.2f % y,ha center,va bottom)
plt.xlabel(Corrleation)
plt.ylabel(Varriance)
plt.show()预订状态‘reservation_status’)与是否取消预订的相关性最高达到了0.92但考虑到后续可能会导致模型过拟合所以删除押金类型(‘deposit_type’)则达到了0.47创造的特征预订和分配房型是否一致(‘same_assignment’)也有0.25的相关性 5.建模预测
划分特征x和标签y
df2df1.drop(reservation_status,axis1)
xdf2.loc[:,df2.columns ! is_canceled ]
ydf2.loc[:,is_canceled]
from sklearn.model_selection import train_test_split as tts
xtrain,xtest,ytrain,ytesttts(x,y,test_size0.3,random_state90)
for i in [xtrain,xtest,ytrain,ytest]:i.indexrange(i.shape[0])5.1随机森林
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score as cvs,KFold
from sklearn.metrics import accuracy_score
rfcRandomForestClassifier(n_estimators100,random_state90)
cvKFold(n_splits10, shuffle True, random_state90)
rfc_scorecvs(rfc,xtrain,ytrain,cvcv).mean()
rfc.fit(xtrain,ytrain)
y_scorerfc.predict_proba(xtest)[:,1]
rfc_predrfc.predict(xtest)
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score as AUC
FPR, recall, thresholds roc_curve(ytest,y_score, pos_label1)
rfc_auc AUC(ytest,y_score)绘制ROC曲线
plt.figure(figsize(8,8))
plt.plot(FPR, recall, colorred,labelROC curve (area %0.2f) % rfc_auc)
plt.plot([0, 1], [0, 1], colorblack, linestyle--)
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel(False Positive Rate)
plt.ylabel(Recall)
plt.title(Random Forest Classifier ROC Curve)
plt.legend(loclower right)
plt.show()查看模型classification_report
from sklearn.metrics import classification_report as CR
print(随机森林.center(50), CR(ytest,rfc_pred),sep\n)score{Model_score:[rfc_score],Auc_area:[rfc_auc]}
score_compd.DataFrame(datascore,index[RandomForest])
score_com.sort_values(by[Model_score],ascendingFalse)随机森林的准确度达到了88.8%AUC面积也有0.95可以通过调参来继续提升模型的效果 分析总结
1.City Hotel的预定量和取消率都远高于Resort Hotel该酒店应对客户展开调研深入了解导致客户最终放弃预订的因素从而降低客户的取消率
2.酒店应利用好每年7、 8月的旅游旺季可以在保证服务质量的同时适当提高价格获取更多利润在淡季冬季的时候进行优惠活动如圣诞大促和新年活动减少酒店空房率
3.酒店需分析来自葡萄牙、英国等主要客源国的客户画像了解这些客户的属性标签、偏好与消费特征推出专属服务从而降低客户的取消率
4.由于散客是酒店的主要客户群且消费水平较高酒店可以通过线上和线下旅游机构的途径加大对自由行的宣传营销从而吸引更多该类型的游客
5.新客的取消率比老客高24%因此酒店应重点关注新客的预订与入住体验为新客提供更多指引与优惠如为首次入住的客户提供折扣调研新客对入住满意与不满意的反馈以提升日后服务同时维护好老客
6.不退押金(Non Refund)这一类型的取消预订率高达99%酒店应优化这种方式如返还50%的押金或者可以直接取消这一种方式从而提高入住率
7.A、G房型的取消率远高于其他房型酒店方应在客户在预订的时候跟客户仔细确认房间信息让客户对房间情况有充分了解避免认知错误同时可以对房间设施进行优化并提高服务水平