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()

推薦閱讀