AI帶你省錢旅游!精準預測民宿房源價格!( 三 )


def string_to_int(df, column):# 字符串替換清理df[column] = df[column].str.replace("$", "")df[column] = df[column].str.replace(",", "")# 轉為數值型df[column] = pd.to_numeric(df[column]).astype(int)return dfgm_df = string_to_int(gm_df, 'price')列表型字段編碼像host_verificationsamenities這樣的字段 , 取值為列表格式,我們對其進行編碼處理(用啞變量替換) 。
# 查看列表型取值字段gm_df_copy = gm_df.copy()gm_df_copy['amenities'].head()

AI帶你省錢旅游!精準預測民宿房源價格!

文章插圖
gm_df_copy['host_verifications'].head()
AI帶你省錢旅游!精準預測民宿房源價格!

文章插圖
# 啞變量編碼gm_df_copy['amenities'] = gm_df_copy['amenities'].str.replace('"', '')gm_df_copy['amenities'] = gm_df_copy['amenities'].str.replace(']', "")gm_df_copy['amenities'] = gm_df_copy['amenities'].str.replace('[', "")df_amenities = gm_df_copy['amenities'].str.get_dummies(sep = ",")gm_df_copy['host_verifications'] = gm_df_copy['host_verifications'].str.replace("'", "")gm_df_copy['host_verifications'] = gm_df_copy['host_verifications'].str.replace(']', "")gm_df_copy['host_verifications'] = gm_df_copy['host_verifications'].str.replace('[', "")df_host_ver = gm_df_copy['host_verifications'].str.get_dummies(sep = ",")編碼后的結果如下所示
df_amenities.head()df_host_ver.head()
AI帶你省錢旅游!精準預測民宿房源價格!

文章插圖
AI帶你省錢旅游!精準預測民宿房源價格!

文章插圖
# 刪除原始字段gm_df = gm_df.drop(['host_verifications', 'amenities'], axis=1)數據探索下一步我們要進行更全面一些的探索性數據分析 。
EDA數據分析部分涉及的工具庫,大家可以參考ShowMeAI制作的工具庫速查表和教程進行學習和快速使用 。
  • 數據科學工具庫速查表 | Pandas 速查表
  • 圖解數據分析:從入門到精通系列教程
哪些街區的房源最多?gm_df['neighbourhood_group_cleansed'].value_counts()
AI帶你省錢旅游!精準預測民宿房源價格!

文章插圖
bar_data = https://www.huyubaike.com/biancheng/gm_df['neighbourhood_group_cleansed'].value_counts().sort_values()# 從bar_data構建新的dataframebar_data = https://www.huyubaike.com/biancheng/pd.DataFrame(bar_data).reset_index()bar_data['size'] = bar_data['neighbourhood_group_cleansed']/gm_df['neighbourhood_group_cleansed'].count()# 排序bar_data.sort_values(by='size', ascending=False)bar_data = https://www.huyubaike.com/biancheng/bar_data.rename(columns={'index' : 'Towns', 'neighbourhood_group_cleansed' : 'number_of_listings','size':'fraction_of_total'})#繪圖展示#plt.figure(figsize=(10,10));bar_data.plot(kind='barh', x ='Towns', y='fraction_of_total', figsize=(8,6))plt.title('Towns with the Most listings');plt.xlabel('Fraction of Total Listings');
AI帶你省錢旅游!精準預測民宿房源價格!

文章插圖
曼徹斯特鎮擁有大曼徹斯特地區的大部分房源,占總房源的 53% (1849) , 其次是索爾福德,占總房源的 17% ;特拉福德,占總房源的 9% 。
大曼徹斯特地區的 Airbnb 房源價格分布gm_df['price'].mean(), gm_df['price'].min(), gm_df['price'].max(),gm_df['price'].median()# (143.47600446428572, 8, 7372, 79.0)Airbnb 房源的均價為 143 美元,中位價為 79 美元,數據集中觀察到的最高價格為 7372 美元 。
# 劃分價格檔位區間labels = ['$0 - $100', '$100 - $200', '$200 - $300', '$300 - $400', '$400 - $500', '$500 - $1000', '$1000 - $8000']price_cuts = pd.cut(gm_df['price'], bins = [0, 100, 200, 300, 400, 500, 1000, 8000], right=True, labels= labels)# 從價格檔構建dataframeprice_clusters = pd.DataFrame(price_cuts).rename(columns={'price': 'price_clusters'})# 拼接原始dataframegm_df = pd.concat([gm_df, price_clusters], axis=1)# 分布繪圖def price_cluster_plot(df, column, title):plt.figure(figsize=(8,6));yx = sb.histplot(data = https://www.huyubaike.com/biancheng/df[column]);total = float(df[column].count())for p in yx.patches:width = p.get_width()height = p.get_height()yx.text(p.get_x() + p.get_width()/2.,height+5,'{:1.1f}%'.format((height/total)*100), ha='center')yx.set_title(title);plt.xticks(rotation=90)return yxprice_cluster_plot(gm_df, column='price_clusters',title="Price distribution of Airbnb Listings in the Greater Manchester Area");
AI帶你省錢旅游!精準預測民宿房源價格!

文章插圖
從上面的分析和可視化結果可以看出,65.4% 的總房源價格在 0-100 美元之間,而價格在 100-200 美元的房源占總房源的 23.4% 。不過我們也觀察到數據分布有很明顯的長尾特性,也可以把特別高價的部分視作異常值,它們可能會對我們的分析有一些影響 。

推薦閱讀