数据分析项目实战——Airbnb数据分析

article/2025/9/26 22:45:45

 爱彼迎官网: 

1、业务背景与分析目的

Airbnb是一款短租产品,作为旅行者和房东之间的媒介颠覆了酒店行业。目前,Aribnb作为短租产品的头部品牌,业务涉及到190+个国家的34,000+ 个城市。

在Airbnb发展如此迅速的同时,是否可以从数据中发现业务存在的问题?

目的:探索数据集,发现数据规律和异常数据,提出改进建议指导业务发展。

定义问题: 1、价格区间是否合理

                    2、Airbnb的用户画像

                    3、通过评论数据集查看Airbnb流量渠道情况

                    4、数据的分析与预测

数据源:https://www.kaggle.com/dgomonov/new-york-city-airbnb-open-data​​​​​​​

2、calendar数据集分析(价格情况等)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as snscalendar = read_csv("./data/calendar.csv")
calendar.head()

去掉数据中的&和,符号,并将日期从字符串类型转化为日期类型:

calendar['Price'] = calendar['Price'].str.replace(r"[$,]","",regex=True).astype(np.float32)
calendar['adjusted_price'] = calendar['adjusted_price'].str.replace(r"[$,]","",regex = True).astype(np.float32)
calendar['date'] = pd.to_datetime(['date'])
calendar['date'].head()

添加星期和月的列:

calendar['weekday'] = calendar['date'].dt.weekday
calendar['month'] = calendar['date'].dt.month

 根据月份将价格进行分组并求出平均值,绘出柱状图:

month_price = calendar.groupby('month')['price'].mean()
sns.barplot(month_price.index,month_price.values)

 

 绘制价格小于300的直方图:

sns.barplot(calendar[calendar['price']<300]['price'])

 

3、listings数据集分析(房子情况)

将列转化成列表来查看所有数据情况:

listings = read_csv("./data/listings.csv")
listings.columns.values。tolist()

 

 去掉数据中的&和,符号,并将小费中的空值替换为0:

listings['Price'] = listings['Price'].str.replace(r"[$,]","",regex=True).astype(np.float32)
listings['cleaning_fee'] = listings['cleaning_fee'].str.replace(r"[$,]","",regex=True).astype(np.float32)
listings['cleaning_fee'].fillna(0,inplace = True)

添加新字段,最低消费:

listings['minimum_cost'] = (listings['Price'] + listings['cleaning_fee'])*listings['minimum_nights']
listings['minimum_nights'].head()

 

 设施数量:

listings['n_amenities'] = listings['amenities'].str[-1:1].str.split(",").apply(len)

 根据可容纳的人数,添加一个新的列,用来表示类型:Single(1),Couple(2),Family(5),Group(100)

listings['accommodates_type'] = pd.cut(listings['accommodates'],bins=[1,2,3,5,100],include_lowest=True,right=False,labels='Single','Couple','Family','Group')
listings['accommodates_type']

房子所在区域与评分:

listings['neighbourhood_group_cleansed'].head()
listings['review_scores_rating'].head()

 

 并先取出需要的数据:

 查看房间类型的情况,并绘制出饼图和柱状图:

room_type_counts=listings['room_type'].value_counts()
fig,axes=plt.subplots(1,2,figsize=(10,5))
axes[0].pie(room_type_counts.values,autopct="%.2f%%",labels=room_type_counts.index)
sns.barplot(room_type_counts.index,room_type_counts.values)
plt.tight_layout()

房子类型数量的条形图:

neighbourhood_counts = lingtings['neighbourhood_group_cleansed'].values_counts()
sns.barplot(y = neighbourhood_counts.index,x = neighbourhood_counts.values,orient = 'h')

 

将数据分组并进行进一步的处理,去掉空值,并按行求出所对应的比例,并按第一列数据进行从大到小的排序:

neighbour_room _type = listings.groupby['neighbourhood_group_cleansed','room_type'].size() \
.unstack('room_type').fillna(0) \ 
.apply(lambda row:row/row.sum(),axis=1) \
.sort_values('Entire home/apt',ascending = False)
neighbour_room _type

 

绘制出比例条形图: 

columns = neighbour_room _type.columns
plt.figure(figsize=(10,5))
index = neighbour_room _type.index
plt.barh(index,columns=neighbour_room _type[columns[0]]) 
left = neighbour_room _type[columns[0]].copy()
plt.barh(index,columns=neighbour_room _type[columns[1]],left=left) 
left += neighbour_room _type[columns[1]].copy()
plt.barh(index,columns=neighbour_room _type[columns[2]],left=left) 
left += neighbour_room _type[columns[2]].copy()
plt.barh(index,columns=neighbour_room _type[columns[3]],left=left)
plt.legend(columns) 

房东房源数量分析  

 可以看出大多房东都只有一套房子,少部分有两套,超过两套的都很少

host_number = listings.groupby('host_id')
sns.distplot(hostnumber[host_number<10])

 

host_number_bins = pd.cut(host_number,bins=[1,2,3,5,100],include_lowest=True,right=False, \ 
labels=['1','2','3-4','5+']).value_counts()
plt.pie(host_number_bins,autopct="%.2f%%",labels=host_number_bins.index)

         

 4、评论数据集分析

reviews = pd.read_csv("./data/reviews_detals.csv",parse_dates=['date'])
reviews.head()

 增加年和月两列,根据年和月分别进行分组,计算出每一年,每一月的评论数量,并绘制出柱状图(2019年10月-12月无数据):

reviews['year'] = reviews['date'].dt.year
reviews['month'] = reviews['date'].dt.monthn_reviews_year = reviews.groupby('year').size()
sns.barplot(n_reviews_year.index,n_reviews_year.values)

n_reviews_month = reviews.groupby('month').size()
sns.barplot(n_reviews_month.index,n_reviews_month.values)

 

由上面价格图知道每年五六月份为销售价格增长期,也即销售增长期,所以评论数量也会随之增长,从上图中也能表现出来

将两组数据进行汇总,并绘制出折线图:

year_month_reviews = reviews.groupby(['year','month']).unstack('month').fillna(0)
year_month_reviews

fig,ax = plt.subplots(figsize=(10,5))
for index in year_month_reviews.index:series=year_month_reviews.loc[index]sns.lineplot(x=series.index,y=series.values,ax=ax)
ax.legend(labels=year_month_reviews.index)
ax.grid()
pic=ax.set_xsticks(list(range(1,13)))

 之后可根据公司业务对上述图形进行分析,如2017-2018年评论数量增幅巨大,可能公司在这两年间做出了一些业务调整或业务拓展等等。

5、预测房间价格

导入相关模块

from sklearn.preprocessing import StandardScaler

对价格有影响的字段:

 查看这些字段的详细信息:

ml_listings = listings_detailed[listings['price']<300][['host_is_superhost','host_identity_verified','neighbour_group_cleansed','property_type','room_type','is_business_travel_ready']
disperse_features=features[disperse_columns]]
ml_listings.info()

 

 删除异常值,分割特征值和目标值:

ml_listings.dropna(axis=0,inplace=True)features=ml_listings.drop(columns=['price'])
targets=ml_listings['price']

 针对其中的离散型数据进行one-hot编码:

disperse_columns=['host_is_superhost','host_identity_verified','neighbour_group_cleansed','property_type','room_type','is_business_travel_ready']
disperse_features=features[disperse_columns]disperse_feature=pd.get_dummies(disperse_features)
disperse_feature

 对连续型数据进行标准化(因为特征值之间相差不是很大,所以标准化可能对预测的结果影响不是很大):

continuous_features=features.drop(columns=disperse_columns)
scaler=StandardScaler()
continuous_features=scaler.fit_transform(continuous_features)

对处理后的特征横向进行组合:

feature_array=np.hstack([disperse_features,continuous_features])

构建训练模型,并进行预测:

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error,r2_scoreX_train,X_test,y_train,y_test=train_test_split(feature_array,targets,test_size=0.25)
regressor=LinearRegression()
regressor.fit_transform(X_train,y_train)
y_predict=regressor.predict(X_test)print("平均误差:",mean_absolute_error(y_test,y_predict))
print("R2评分:",r2_score(y_test,y_predict))

模型准确度不太好,使用随机森林进行预测:

from sklearn.ensemble import RandomForestRegressorX_train,X_test,y_train,y_test=train_test_split(feature_array,targets,test_size=0.25)
regressor=RandomForestRegressor()
regressor.fit_transform(X_train,y_train)
y_predict=regressor.predict(X_test)print("平均误差:",mean_absolute_error(y_test,y_predict))
print("R2评分:",r2_score(y_test,y_predict))

 6、评论数量预测

 

reviews.head()

ym_reviews=reviews.groupby(['year','month']).size().reset_index().rename(coiumns={0:'count'})
ym_reviews

 构建模型,使用随机森林进行预测,并查看准确率:

features=ym_reviews[['year','month']]
targets=ym_reviews['count']X_train,X_test,y_train,y_test=train_test_split(feature_array,targets,test_size=0.25)
regressor=RandomForestRegressor(n_estimators=100)
regressor.fit(X_train,y_train)
y_predict=regressor.predict(X_test)print("平均误差:",mean_absolute_error(y_test,y_predict))
print("R2评分:",r2_score(y_test,y_predict))

 由结果可看出模型预测准确率较好,因此对2019年10月,11月和12月的数据进行预测:

features=ym_reviews[['year','month']]
targets=ym_reviews['count']regressor=RandomForestRegressor(n_estimators=100)
regressor.fit(features,targets)y_predict=regressor.predict([2019,10],[2019,11],[2019,12])
y_predict

预测后结果为:

将预测值转换为DataFrame类型,并将预测值和源数据进行堆叠拼接,然后绘制出折线图:

predict_reviews=pd.DataFrame([[2019,10+index,x] for index,x in enumerate(y_predict)],columns=['year','month','count'])
final_reviews=pd.concat([ym_reviews,predict_reviews]).reset_index()years=final_reviews['year'].unique()
fig,ax = plt.subplots(figsize=(10,5))
for year in years:df=final_reviews[final_reviews['year']==year]sns.lineplot(x='month',y='count',data=df)ax.legend(labels=year_month_reviews.index)
ax.grid()
pic=ax.set_xsticks(list(range(1,13)))

 


http://chatgpt.dhexx.cn/article/dwf9dgmb.shtml

相关文章

一个企业级数据挖掘实战项目|教育数据挖掘

数据集描述 本案例数据集来自Balochistan的6000名学生。其基本情况&#xff1a;一共13个字段&#xff0c;其中RESULT为结果标签&#xff1b; 语言字段是经过独热编码后的四个字段&#xff0c;分别为Lang1, Lang2, Lang3, Lang4&#xff1b; 另外性别、学校、是否残疾、宗教信仰…

二、大数据实践项目——数据分析与处理

一、数据处理主要任务 二、数据集处理 1、查看数据集基本情况 调用 info() 函数来查看数据data的基本情况&#xff0c;包括数据维度&#xff0c;字段名称和类型以及有无缺失值&#xff0c;数据占用内存等。&#xff08;以下为部分字段信息&#xff09; 可见总的数据47447行&a…

数据挖掘项目总结文档

数据挖掘项目总结文档 1、文档概述 1.1 编写目的 记录本次实验思路及流程&#xff0c;备忘用。 1.2 适用对象 个人学习总结&#xff0c;描述有偏差之处陆续更进。 2、业务理解与分析定义 2.1 需求概述 针对传统网络入侵检测系统存在的误判以及漏检情况&#xff0c;采用数据挖掘…

数据挖掘开源项目立项

项目背景 因为最近一直都在搞数据挖掘类的项目&#xff0c;且现在国内的大数据潮火热。在前几天与群里的几位兄弟聊天所以有了做一个开源项目的想法&#xff0c;以前也搞过一个开源的项目&#xff0c;当时只是想把权限集中化做一下&#xff0c;项目的名称和地址是&#xff1a; …

数据挖掘项目(1)对数据进行探索和分析

1.数据类型的分析&#xff08;假设数据为data.csv&#xff09; 首先读入数据&#xff0c;这个数据是csv格式&#xff0c;可以用pandas来读&#xff0c;如果读不进来的时候&#xff0c;可以用记事本打开data.csv然后另存为data_2.csv并且保存为utf-8的编码格式。然后读取数据。…

python数据挖掘项目——航空公司客户价值分析(详解)

一、选题背景 信息时代的来临使得企业营销的焦点从产品中心转变为客户中心&#xff0c;客户关系管理成为企业的核心问题&#xff0c;客户关系管理的关键问题是客户分类&#xff0c;通过客户分类&#xff0c;区分不同客户的价值&#xff0c;企业针对不同价值的客户制定个性化的服…

网络通信协议分类

协议分类 通信的协议还是比较复杂的&#xff0c;java.net 包中包含的类和接口&#xff0c;它们提供低层次的通信细节。我们可以直接使用这些类和接口&#xff0c;来专注于网络程序开发&#xff0c;而不用考虑通信的细节。 java.net 包中提供了两种常见的网络协议的支持&#…

各种基础协议

了解几个概念&#xff1a; 1. HTTP 协议&#xff1a;基于TCP协议&#xff0c;超文本传输协议&#xff0c;对应于应用层&#xff0c;用于如何封装数据.。也就是在底层是基于socket&#xff0c; http只不过是在收发数据的时候定义了很多规则&#xff0c;http头信息之类。 TCP/I…

TCP协议格式

1、16位源端口号&#xff1a;16位的源端口中包含初始化通信的端口。源端口和源IP地址的作用是标识报文的返回地址。 2、16位目的端口号&#xff1a;16位的目的端口域定义传输的目的。这个端口指明报文接收计算机上的应用程序地址接口。 3、32位序号&#xff1a;32位的序列号由…

以太网各种协议详解

板子处于复位状态时&#xff0c;先做好一系列的准备工作。 1、从EEROM中读取板子的MAC 地址&#xff08;事先已经写在地址0xFA to 0xFF 中了&#xff09;。 2、配置PHY 寄存器&#xff0c;并读取该寄存器的值&#xff0c;检查一下&#xff0c;此时的寄存器配置时候是合理的&am…

汽车通信协议系列1_通信协议类型

当前比较通用的五种协议&#xff0c;任何一款标准OBD2的车辆都会采用以下五种协议中的一种。以下几种按照物理层编码相关性来划分 ISO9141&#xff0c;ISO 14230.这类是以UART为基础的。 CAN ISO 11898&#xff08;车载网络&#xff09;&#xff0c;ISO15768&#xff0c;SAE J…

网络协议分类

Http协议&#xff1a;应用层。 tcp/udp协议&#xff1a;传输层。udp提供不可靠通信&#xff1a;无流控等。 ip协议&#xff1a;网络层。 TCP/IP是是一套协议族&#xff0c;是一种说法&#xff0c;由早期的某标准化组织制定&#xff0c;规定了使用tcp和ip等一系列协议的规范。…

IP协议及分类

文章目录 TCP/IP协议IP地址分类默认子网掩码网关公有IP地址和私有IP地址 基本环境设置连网介质及设备重点 TCP/IP协议 TCP/IP通信协议是目前最完整、最被广泛支持的通信协议&#xff0c;它可以让不同网络架构、不同操作系统的计算机之间通信&#xff0c;是Internet的标准通信协…

常见的协议汇总(小白个人理解,大佬勿喷)

背景 接触了一些芯片&#xff0c;发现所有的芯片而言&#xff0c;库和API接口随着芯片&#xff0c;开发环境甚至开发情况&#xff0c;公司都有着明显的区别。基于这种情况的话&#xff0c;了解协议的底层个人觉得是十分必要的&#xff0c;同时也是找工作的必备选项。所以对常见…

常用协议类型值

Ethertype ( 十六进制 ) 协议 0x0000 - 0x05DC IEEE 802.3 长度 0x0101 – 0x01FF 实验 0x0600 XEROX NS IDP 0x0660 0x0661 DLOG 0x0800 网际协议&#xff08;IP&#xff09; 0x0801 X.75 Internet 0x0802 NBS Internet 0x0803 ECMA Internet 0x0804 Chao…

TCP协议格式和特点

文章目录 1.协议格式&#xff1a;2.协议特性&#xff1a;2.1 面向链接2.1.1三次握手建立连接2.1.1四次挥手断开连接相关问题和知识点&#xff1a;1. 握手为啥三次&#xff0c;挥手是四次&#xff1f;2. 三次握手失败两端是如何处理的&#xff1f;3. SYN泛洪攻击是怎么回事?4. …

典型几种协议(协议以及作用)

一 .典型协议&#xff1a; 传输层&#xff1a; 常见的协议有 TCP/UDP 协议 应用层&#xff1a; 常见的协议有 HTTP&#xff0c;FTP 协议 网络层&#xff1a; 常见的协议有 IP 协议&#xff0c;ICMP 协议&#xff0c;IG…

ICMP协议 详解,ICMP协议的功能及实现原理,ICMP协议报文类型。

「作者主页」&#xff1a;士别三日wyx 「作者简介」&#xff1a;CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 「专栏简介」&#xff1a;此文章已录入专栏《计算机网络零基础快速入门》 ICMP协议 一、工作原理二、数据报格式三、报文类型 ICMP协议是IP的…

几个的常见基础协议类型数据格式以及协议内容简介

给大家简单梳理一下几种学习中常会出现的协议格式&#xff0c;咋们数通就像交通&#xff0c;各种各样的协议规则来规范大家&#xff0c;制定科学的管理手段来帮助大家快速&#xff0c;安全的到家。 一、 HDLC协议 HDLC叫高级链路控制协议&#xff08;High Level Data Link Cont…

几种常用协议介绍

几种常见协议介绍 TCP/IP协议&#xff0c;其实是一个协议集合&#xff0c;这个集合里面包含了网络通讯所需的所有协议&#xff0c;里面不仅有TCP(传输控制协议)、IP(网际协议)&#xff0c;还有UDP、ICMP、RIP、TELNET、FTP、SMTP、ARP、TFTP等许多协议&#xff0c;还有物联网中…