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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

随机森林回归实验

發(fā)布時間:2025/3/21 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 随机森林回归实验 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

隨機森林回歸實驗

文章目錄

  • 隨機森林回歸實驗
    • 實驗說明
    • 實驗步驟
    • 可視化

實驗說明

sklearn包里已經(jīng)實現(xiàn)了隨機森林回歸模型,導(dǎo)入使用即可。

數(shù)據(jù)集我們使用的是 sklearn包中自帶的波士頓房價數(shù)據(jù)集。

  • 實驗環(huán)境:Anaconda3+VScode
  • Python版本:3.7
  • 需要的第三方庫:sklearn、matplotlib、numpy

實驗步驟

一個簡單的隨機森林回歸實驗同樣分為六個步驟:

  • 加載數(shù)據(jù)集
  • 拆分數(shù)據(jù)集
  • 創(chuàng)建模型
  • 在訓(xùn)練集學(xué)習(xí)得到模型
  • 模型預(yù)測
  • 模型評測
  • 關(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é)

    以上是生活随笔為你收集整理的随机森林回归实验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。