基本信息
源码名称:python多元线性回归房价预测_附代码和数据
源码大小:0.40M
文件格式:.rar
开发语言:Python
更新时间:2021-12-31
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
python多元线性回归房价预测_附代码和数据
python多元线性回归房价预测_附代码和数据
from sklearn.model_selection import KFold from sklearn.tree import DecisionTreeRegressor from sklearn.metrics import make_scorer from sklearn.model_selection import GridSearchCV # 利用GridSearchCV计算最优解 def fit_model(X, y): """ 基于输入数据 [X,y],利于网格搜索找到最优的决策树模型""" cross_validator = KFold(10, shuffle=True) regressor = DecisionTreeRegressor() params = {'max_depth': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]} scoring_fnc = make_scorer(performance_metric) grid = GridSearchCV(estimator=regressor, param_grid=params, scoring=scoring_fnc, cv=cross_validator) # 基于输入数据 [X,y],进行网格搜索 grid = grid.fit(X, y) # print pd.DataFrame(grid.cv_results_) return grid.best_estimator_ # 计算R2分数 def performance_metric(y_true, y_predict): """计算并返回预测值相比于预测值的分数""" from sklearn.metrics import r2_score score = r2_score(y_true, y_predict) return score ######################################################调参优化模型 #########################可视化模型学习曲线,观察是否出现过拟合问题 import visuals as vs # 分析模型 vs.ModelLearning(features_train, prices_train) vs.ModelComplexity(features_train, prices_train) optimal_reg1 = fit_model(features_train, prices_train) # 输出最优模型的 'max_depth' 参数 print("最理想模型的参数'max_depth'是{} 。".format(optimal_reg1.get_params()['max_depth'])) predicted_value = optimal_reg1.predict(features_test) r2 = performance_metric(prices_test, predicted_value) print("最优模型在测试数据上R^2分数{:,.2f}。".format(r2))