AI带你省钱旅游!精准预测民宿房源价格!( 四 )


最受欢迎的房型是什么# 基于评论量统计排序ax = gm_df.groupby('property_type').agg(median_rating=('review_scores_rating', 'median'),number_of_reviews=('number_of_reviews', 'max')).sort_values(by='number_of_reviews', ascending=False).reset_index()ax.head()

AI带你省钱旅游!精准预测民宿房源价格!

文章插图
在评论最多的前 10 种房产类型中,Entire rental unit 评论数量最多 , 其次是Private room in rental unit 。
# 可视化bx = ax.loc[:10]bx =sb.boxplot(data =https://www.huyubaike.com/biancheng/bx, x='median_rating', y='property_type')bx.set_xlim(4.5, 5)plt.title('Most Enjoyed Property types');plt.xlabel('Median Rating');plt.ylabel('Property Type')
AI带你省钱旅游!精准预测民宿房源价格!

文章插图
房东与房源分布# 持有房源最多的房东host_df = pd.DataFrame(gm_df['host_name'].value_counts()/gm_df['host_name'].count() *100).reset_index()host_df = host_df.rename(columns={'index':'name', 'host_name':'perc_count'})host_df.head(10)
AI带你省钱旅游!精准预测民宿房源价格!

文章插图
host_df['perc_count'].loc[:10].sum()从上述分析可以看出,房源最多的前 10 名房东占房源总数的 13.6% 。
大曼彻斯特地区提供的客房类型分布gm_df['room_type'].value_counts()
AI带你省钱旅游!精准预测民宿房源价格!

文章插图
# 分布绘图zx = sb.countplot(data=https://www.huyubaike.com/biancheng/gm_df, x='room_type')total = float(gm_df['room_type'].count())for p in zx.patches:width = p.get_width()height = p.get_height()zx.text(p.get_x() + p.get_width()/2.,height+5, '{:1.1f}%'.format((height/total)*100), ha='center')zx.set_title('Plot showing different type of rooms available');plt.xlabel('Room')
AI带你省钱旅游!精准预测民宿房源价格!

文章插图
大部分客房是 整栋房屋/公寓 ,占房源总数的 60% , 其次是私人客房,占房源总数的 39%,共享房间 和 酒店房间 分别占房源的 0.7% 和 0.5% 。
机器学习建模下面我们使用回归建模方法来对民宿房源价格进行预估 。
特征工程
关于特征工程,欢迎大家查阅ShowMeAI对应的教程文章,快学快用 。
  • 机器学习实战 | 机器学习特征工程最全解读
我们首先对原始数据进行特征工程,得到适合建模的数据特征 。
# 查看此时的数据集gm_df.head()
AI带你省钱旅游!精准预测民宿房源价格!

文章插图
# 回归数据集gm_regression_df = gm_df.copy()# 剔除无用字段gm_regression_df = gm_regression_df.drop(columns=['id', 'scrape_id', 'last_scraped', 'name', 'host_id', 'host_since', 'first_review', 'last_review', 'price_clusters', 'host_name'])# 再次查看数据gm_regression_df.head()
AI带你省钱旅游!精准预测民宿房源价格!

文章插图
我们发现host_response_ratehost_acceptance_rate字段带有百分号 , 我们再做一点数据清洗 。
# 去除百分号并转换为数值型gm_regression_df['host_response_rate'] =gm_regression_df['host_response_rate'].str.replace("%", "")gm_regression_df['host_acceptance_rate'] =gm_regression_df['host_acceptance_rate'].str.replace("%", "")# convert to intgm_regression_df['host_response_rate'] = pd.to_numeric(gm_regression_df['host_response_rate']).astype(int)gm_regression_df['host_acceptance_rate'] =pd.to_numeric(gm_regression_df['host_acceptance_rate']).astype(int)# 查看转换后结果gm_regression_df['host_response_rate'].head()
AI带你省钱旅游!精准预测民宿房源价格!

文章插图
bathrooms_text 列包含数字和文本数据的组合 , 我们对其做一些处理
# 查看原始字段gm_regression_df['bathrooms_text'].value_counts()
AI带你省钱旅游!精准预测民宿房源价格!

文章插图
# 切分与数据处理def split_bathroom(df, column, text, new_column):df_2 = df[df[column].str.contains(text, case=False)]df.loc[df[column].str.contains(text, case=False), new_column] = df_2[column]return df# 应用上述函数gm_regression_df = split_bathroom(gm_regression_df, column='bathrooms_text', text='shared', new_column='shared_bath')gm_regression_df = split_bathroom(gm_regression_df, column='bathrooms_text', text='private', new_column='private_bath')# 查看shared_bath字段gm_regression_df['shared_bath'].value_counts()
AI带你省钱旅游!精准预测民宿房源价格!

文章插图
# 查看private_bath字段gm_regression_df['private_bath'].value_counts()

推荐阅读