随机森林回归实验
隨機森林回歸實驗
文章目錄
- 隨機森林回歸實驗
- 實驗說明
- 實驗步驟
- 可視化
實驗說明
sklearn包里已經(jīng)實現(xiàn)了隨機森林回歸模型,導(dǎo)入使用即可。
數(shù)據(jù)集我們使用的是 sklearn包中自帶的波士頓房價數(shù)據(jù)集。
- 實驗環(huán)境:Anaconda3+VScode
- Python版本:3.7
- 需要的第三方庫:sklearn、matplotlib、numpy
實驗步驟
一個簡單的隨機森林回歸實驗同樣分為六個步驟:
關(guān)于訓(xùn)練集和測試集的劃分我們使用的是留出法,最后的結(jié)果我們使用四項指標來進行評估:
- 平均絕對誤差
- 均方誤差
- 解釋方差分
- R2得分
這一步用到的第三方庫是 sklearn。
代碼如下:
from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor from sklearn.metrics import mean_absolute_error from sklearn.metrics import mean_squared_error from sklearn.metrics import explained_variance_score from sklearn.metrics import r2_score #1.加載數(shù)據(jù)集 boston_data = load_boston() # print(boston_data) #2.拆分數(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); #3.創(chuàng)建模型 rfr = RandomForestRegressor(random_state=10, max_depth=8) #4.獲取在訓(xùn)練集的模型 rfr.fit(x_train, y_train) #5.預(yù)測結(jié)果 rfr_predict = rfr.predict(x_test) #6.模型評測 mae = mean_absolute_error(y_test, rfr_predict) mse = mean_squared_error(y_test, rfr_predict) evs = explained_variance_score(y_test, rfr_predict) r2 = r2_score(y_test, rfr_predict) print("平均絕對誤差MAE:{}".format(mae)) print("均方誤差MSE:{}".format(mse)) print("解釋方差分EVS:{}".format(evs)) print("R2得分:{}".format(r2))可以看到,得到的四項指標為:平均絕對誤差為2.78,均方誤差為14.24,解釋方差分為0.86,R2得分為0.86
可視化
我們將剛才訓(xùn)練的隨機森林,借助散點圖進行可視化。
這一步需要的第三方庫是 sklearn、matplotlib、numpy。
代碼如下:
from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestRegressor from sklearn.metrics import mean_absolute_error from sklearn.metrics import mean_squared_error from sklearn.metrics import explained_variance_score from sklearn.metrics import r2_score import matplotlib.pyplot as plt from matplotlib.colors import ListedColormap import numpy as np #1.加載數(shù)據(jù)集 boston_data = load_boston() # print(boston_data) #2.拆分數(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) #3.創(chuàng)建模型 rfr = RandomForestRegressor(random_state=10, max_depth=8) #4.獲取在訓(xùn)練集的模型 rfr.fit(x_train, y_train) #5.預(yù)測結(jié)果 rfr_predict = rfr.predict(x_test) #6.模型評測 mae = mean_absolute_error(y_test, rfr_predict) mse = mean_squared_error(y_test, rfr_predict) evs = explained_variance_score(y_test, rfr_predict) r2 = r2_score(y_test, rfr_predict) print("平均絕對誤差MAE:{}".format(mae)) print("均方誤差MSE:{}".format(mse)) print("解釋方差分EVS:{}".format(evs)) print("R2得分:{}".format(r2)) # 設(shè)置散點顏色 point_color = ListedColormap(['#FF0000', '#00FF00']) # 設(shè)置坐標軸 y_min, y_max = y_test.min()-1,y_test.max()+1 xx = np.arange(0,102,1) yy = y_test yy.sort() z = rfr_predict z.sort() # print(xx.shape) # print(yy.shape) # print(z.shape)# 創(chuàng)建圖片 plt.figure() plt.scatter(xx, yy, cmap=point_color, edgecolors='black') plt.scatter(xx, z, cmap=point_color, edgecolors='black') # 繪制刻度 plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) # 設(shè)置標題 plt.title("RandomForestRegressor") # 展示圖表 plt.grid(True) plt.show()散點圖展示:
可以看到,散點的分布位置貼合較近,模型訓(xùn)練得還不錯。
總結(jié)
- 上一篇: 决策树分类实验
- 下一篇: CentOS7安装Docker详细教程