随机森林最后的结果如下
r_forest_rmse, r_forest_r2# (218.7941962807868, 0.4208644494689676)
GBDT建模def GBDT_model(df):'''构建模型并返回评估结果输入: 数据dataframe输出: 特征重要度与评估准则(RMSE与R-squared)'''X = df.drop(['price'], axis=1)Y = df['price']X_columns = X.columnsX_train, X_test, y_train, y_test = train_test_split(X, Y, random_state=42)clf = GradientBoostingRegressor()parameters = {'learning_rate': [0.1, 0.5, 1],'min_samples_leaf': [10, 20, 40 , 60]}cv = GridSearchCV(estimator=clf, param_grid=parameters, cv=5, verbose=3)model = cvmodel.fit(X_train, y_train)pred = model.predict(X_test)r2 = r2_score(y_test, pred)mse = mean_squared_error(y_test, pred)rmse = mse**.5coefficients = model.best_estimator_.feature_importances_importance = np.abs(coefficients)feature_importance = pd.DataFrame(importance, index= X_columns,columns=['importance']).sort_values('importance', ascending=False)[:10]return r2, mse, rmse, feature_importanceGBDT_r2, GBDT_mse, GBDT_rmse, GBDT_feature_importance = GBDT_model(model_df)GBDT_r2, GBDT_rmse# (0.46352992147034244, 210.58063809645563)
结果&分析目前随机森林的表现最稳定,而集成模型GradientBoostingRegression 的R2很高,RMSE 值也偏高,Boosting的模型受异常值影响很大,这可能是因为数据集中的异常值引起的 。
下面我们来做一下优化,删除数据集中的异常值,看看是否可以提高模型性能 。
效果优化异常值在早些时候就已经被识别出来了,我们基于统计的方法来对其进行处理 。
# 基于统计方法计算价格边界q3, q1 = np.percentile(model_df['price'], [75, 25])iqr = q3 - q1q3 + (iqr*1.5)# 得到结果245.0
我们把任何高于 245 美元的值都视为异常值并删除 。
new_model_df = model_df[model_df['price']<245]# 绘制此时的价格分布sb.histplot(new_model_df['price'])plt.title('New price distribution in the dataset')
![AI带你省钱旅游!精准预测民宿房源价格!](http://shimg.jingyanzongjie.com/230726/2309423215-32.png)
文章插图
重新运行这些算法
linear_feat_importance, linear_rmse, linear_r2 = linear_reg(new_model_df)r_forest_rmse, r_forest_r2, r_fores_best_params, r_forest_importance = random_forest(new_model_df)GBDT_r2, GBDT_mse, GBDT_rmse, GBDT_feature_importance = GBDTboost(new_model_df)
得到的新结果如下![AI带你省钱旅游!精准预测民宿房源价格!](http://shimg.jingyanzongjie.com/230726/2309424142-33.png)
文章插图
归因分析那么,基于我们的模型来分析,在预测大曼彻斯特地区 Airbnb 房源的价格时,哪些因素更重要?
r_feature_importance = r_forest_importance.reset_index()r_feature_importance = r_feature_importance.rename(columns={'index':'Feature'})r_feature_importance[:15]
![AI带你省钱旅游!精准预测民宿房源价格!](http://shimg.jingyanzongjie.com/230726/2309425106-34.png)
文章插图
# 绘制最重要的15个因素r_feature_importance[:15].sort_values(by='AVG_Importance').plot(kind='barh', x='Feature', y='AVG_Importance', figsize=(8,6));plt.title('Top 15 Most Imporatant Features');
![AI带你省钱旅游!精准预测民宿房源价格!](http://shimg.jingyanzongjie.com/230726/2309424Y5-35.png)
文章插图
我们的模型给出的重要因素包括:
- accommodates :可以容纳的最大人数 。
- bathrooms_new :非共用或非私人浴室的数量 。
- minimum_nights :房源可预定的最少晚数 。
- number_of_reviews :总评论数 。
- Free street parking :免费路边停车位的存在是影响模型定价的最重要的便利设施 。
- Gym :健身房设施 。
![AI带你省钱旅游!精准预测民宿房源价格!](http://shimg.jingyanzongjie.com/230726/230942L24-36.png)
文章插图
我们通过对Airbnb的数据进行深入挖掘分析和建模,完成对于民宿租赁场景下的AI理解与建模预估 。我们后续还有一些可以做的事情,提升模型的表现,完成更精准地预估,比如:
经验总结扩展阅读
- 常熟旅游景点有哪些 常熟必去十大景点
- 2023年农历八月十二旅游吉日 2023年9月26日旅游好不好
- 一篇文章带你了解NoSql数据库——Redis简单入门
- 旅游发圈的精致句子出游短句唯美
- 两个人去张家界旅游三天大概需要多少钱
- 2023年9月27日旅游黄道吉日 2023年9月27日是旅游的黄道吉日吗
- 2023年2月2日是旅游的黄道吉日吗 2023年2月2日适合旅游吗
- 带你去看海底星空是什么意思
- 带你去看海底星空是什么梗
- 一篇文章带你了解服务器操作系统——Linux简单入门