日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用SVR模型完成对Boston房价的回归预测

發布時間:2025/3/21 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用SVR模型完成对Boston房价的回归预测 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用SVR模型完成對Boston房價的回歸預測

文章目錄

  • 用SVR模型完成對Boston房價的回歸預測
    • 實驗說明
    • 實驗代碼
    • 參數優化

實驗說明

實驗要求:使用SVR模型實現對波士頓房價的預測 (load_boston),并使用r2-score 對回歸結果評測。

  • 實驗環境:Pycharm
  • Python版本:3.6
  • 需要的第三方庫:sklearn

實驗代碼

同樣地,這里 SVR 模型采用的是高斯核函數 kernel=‘rbf’,懲罰系數 C=1,epsilon=0.2。

我們采用以下四項指標來進行評價:

  • 平均絕對誤差 MAE
  • 均方誤差 MSE
  • 解釋方差分 EVS
  • R2得分 R2_Score

有關 SVR 模型的參數以及如何選擇, 可以參考這篇博客 sklearn.svm.SVR的參數介紹

from sklearn.datasets import load_boston from sklearn.svm import SVR from sklearn.preprocessing import StandardScaler from sklearn.metrics import mean_absolute_error,mean_squared_error,explained_variance_score,r2_score from sklearn.model_selection import train_test_split import numpy as np #加載數據集 boston=load_boston() x=boston.data y=boston.target# 拆分數據集 x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=10) # 預處理 y_train = np.array(y_train).reshape(-1, 1) y_test = np.array(y_test).reshape(-1, 1) x_train = StandardScaler().fit_transform(x_train) x_test = StandardScaler().fit_transform(x_test) y_train = StandardScaler().fit_transform(y_train).ravel() y_test = StandardScaler().fit_transform(y_test).ravel()#創建svR實例 svr=SVR(C=1, kernel='rbf', epsilon=0.2) svr=svr.fit(x_train,y_train) #預測 svr_predict=svr.predict(x_test) #評價結果 mae = mean_absolute_error(y_test, svr_predict) mse = mean_squared_error(y_test, svr_predict) evs = explained_variance_score(y_test, svr_predict) r2 = r2_score(y_test, svr_predict) print("MAE:", mae) print("MSE:", mse) print("EVS:", evs) print("R2:", r2)

從結果中可以看到,R2得分還是比較可以的,達到了 0.80。

參數優化

同理,我們繼續采用網格參數搜索的方式,核函數依然選擇高斯核函數,我們針對 懲罰系數 C 和核函數系數 gamma ,以及 epsilon 進行調參。

from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.model_selection import GridSearchCV from sklearn.svm import SVR from sklearn.metrics import mean_absolute_error, mean_squared_error, explained_variance_score, r2_score import numpy as np# 加載數據集 boston_data = load_boston() # print(boston_data)# 拆分數據集 x = boston_data.data y = boston_data.target x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=10) # 預處理 y_train = np.array(y_train).reshape(-1, 1) y_test = np.array(y_test).reshape(-1, 1) x_train = StandardScaler().fit_transform(x_train) x_test = StandardScaler().fit_transform(x_test) y_train = StandardScaler().fit_transform(y_train).ravel() y_test = StandardScaler().fit_transform(y_test).ravel()# 設置超參數 C = [0.1, 0.2, 0.5, 0.8, 0.9, 1, 2, 5, 10] kernel = 'rbf' gamma = [0.001, 0.01, 0.1, 0.2, 0.5, 0.8] epsilon = [0.01, 0.05, 0.1, 0.2, 0.5, 0.8] # 參數字典 params_dict = {'C': C,'gamma': gamma,'epsilon': epsilon }# 創建SVR實例 svr = SVR()# 網格參數搜索 gsCV = GridSearchCV(estimator=svr,param_grid=params_dict,n_jobs=2,scoring='r2',cv=6 ) gsCV.fit(x_train, y_train) # 輸出參數信息 print("最佳度量值:", gsCV.best_score_) print("最佳參數:", gsCV.best_params_) print("最佳模型:", gsCV.best_estimator_)# 用最佳參數生成模型 svr = SVR(C=gsCV.best_params_['C'], kernel=kernel, gamma=gsCV.best_params_['gamma'],epsilon=gsCV.best_params_['epsilon'])# 獲取在訓練集的模型 svr.fit(x_train, y_train)# 預測結果 svr_predict = svr.predict(x_test)# 模型評測 mae = mean_absolute_error(y_test, svr_predict) mse = mean_squared_error(y_test, svr_predict) evs = explained_variance_score(y_test, svr_predict) r2 = r2_score(y_test, svr_predict) print("MAE:", mae) print("MSE:", mse) print("EVS:", evs) print("R2:", r2)

從結果上可以看出,調優的參數還是起了一點作用,R2得分變為了 0.84。

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的用SVR模型完成对Boston房价的回归预测的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。