用SVR模型完成对Boston房价的回归预测
生活随笔
收集整理的這篇文章主要介紹了
用SVR模型完成对Boston房价的回归预测
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
用SVR模型完成對Boston房價的回歸預(yù)測
文章目錄
- 用SVR模型完成對Boston房價的回歸預(yù)測
- 實驗說明
- 實驗代碼
- 參數(shù)優(yōu)化
實驗說明
實驗要求:使用SVR模型實現(xiàn)對波士頓房價的預(yù)測 (load_boston),并使用r2-score 對回歸結(jié)果評測。
- 實驗環(huán)境:Pycharm
- Python版本:3.6
- 需要的第三方庫:sklearn
實驗代碼
同樣地,這里 SVR 模型采用的是高斯核函數(shù) kernel=‘rbf’,懲罰系數(shù) C=1,epsilon=0.2。
我們采用以下四項指標(biāo)來進(jìn)行評價:
- 平均絕對誤差 MAE
- 均方誤差 MSE
- 解釋方差分 EVS
- R2得分 R2_Score
有關(guān) SVR 模型的參數(shù)以及如何選擇, 可以參考這篇博客 sklearn.svm.SVR的參數(shù)介紹
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 #加載數(shù)據(jù)集 boston=load_boston() x=boston.data y=boston.target# 拆分?jǐn)?shù)據(jù)集 x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=10) # 預(yù)處理 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()#創(chuàng)建svR實例 svr=SVR(C=1, kernel='rbf', epsilon=0.2) svr=svr.fit(x_train,y_train) #預(yù)測 svr_predict=svr.predict(x_test) #評價結(jié)果 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)從結(jié)果中可以看到,R2得分還是比較可以的,達(dá)到了 0.80。
參數(shù)優(yōu)化
同理,我們繼續(xù)采用網(wǎng)格參數(shù)搜索的方式,核函數(shù)依然選擇高斯核函數(shù),我們針對 懲罰系數(shù) C 和核函數(shù)系數(shù) gamma ,以及 epsilon 進(jìn)行調(diào)參。
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# 加載數(shù)據(jù)集 boston_data = load_boston() # print(boston_data)# 拆分?jǐn)?shù)據(jù)集 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ù)處理 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()# 設(shè)置超參數(shù) 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] # 參數(shù)字典 params_dict = {'C': C,'gamma': gamma,'epsilon': epsilon }# 創(chuàng)建SVR實例 svr = SVR()# 網(wǎng)格參數(shù)搜索 gsCV = GridSearchCV(estimator=svr,param_grid=params_dict,n_jobs=2,scoring='r2',cv=6 ) gsCV.fit(x_train, y_train) # 輸出參數(shù)信息 print("最佳度量值:", gsCV.best_score_) print("最佳參數(shù):", gsCV.best_params_) print("最佳模型:", gsCV.best_estimator_)# 用最佳參數(shù)生成模型 svr = SVR(C=gsCV.best_params_['C'], kernel=kernel, gamma=gsCV.best_params_['gamma'],epsilon=gsCV.best_params_['epsilon'])# 獲取在訓(xùn)練集的模型 svr.fit(x_train, y_train)# 預(yù)測結(jié)果 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)從結(jié)果上可以看出,調(diào)優(yōu)的參數(shù)還是起了一點作用,R2得分變?yōu)榱?0.84。
總結(jié)
以上是生活随笔為你收集整理的用SVR模型完成对Boston房价的回归预测的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用SVC模型完成对手写数字的分类
- 下一篇: Windows下搭建PySpark环境