最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)H2O中的隨機森林算法介紹及其項目實戰(zhàn)

 更新時間:2019年08月29日 09:38:57   作者:鴻燕藏鋒  
這篇文章主要介紹了python實現(xiàn)H2O中的隨機森林算法介紹及其項目實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

H2O中的隨機森林算法介紹及其項目實戰(zhàn)(python實現(xiàn))

包的引入:from h2o.estimators.random_forest import H2ORandomForestEstimator

H2ORandomForestEstimator 的常用方法和參數(shù)介紹:

(一)建模方法:

model =H2ORandomForestEstimator(ntrees=n,max_depth =m)

model.train(x=random_pv.names,y='Catrgory',training_frame=trainData)

通過trainData來構(gòu)建隨機森林模型,model.train中的trainData:訓(xùn)練集,x:預(yù)測變量名稱,y:預(yù)測 響應(yīng)變量的名稱

(二)預(yù)測方法:

pre_tag=H2ORandomForestEstimator.predict(model ,test_data) 利用訓(xùn)練好的模型來對測試集進行預(yù)測,其中的model:訓(xùn)練好的模型, test_data:測試集。

(三)算法參數(shù)說明:

(1)ntrees:構(gòu)建模型時要生成的樹的棵樹。

(2)max_depth :每棵樹的最大深度。

項目要求:

題目一: 利用train.csv中的數(shù)據(jù),通過H2O框架中的隨機森林算法構(gòu)建分類模型,然后利用模型對 test.csv中的數(shù)據(jù)進行預(yù)測,并計算分類的準(zhǔn)確度進而評價模型的分類效果;通過調(diào)節(jié)參 數(shù),觀察分類準(zhǔn)確度的變化情況。 注:準(zhǔn)確度=預(yù)測正確的數(shù)占樣本數(shù)的比例

題目二: 通過H2o Flow 的隨機森林算法,用同題目一中所用同樣的訓(xùn)練數(shù)據(jù)和參數(shù),構(gòu)建模型; 參看模型中特征的重要性程度,從中選取前8個特征,再去訓(xùn)練模型,并重新預(yù)測結(jié)果, 進而計算分類的準(zhǔn)確度。

需求完成內(nèi)容:2個題目的代碼,認為最好的準(zhǔn)確度的輸出值和test數(shù)據(jù)與預(yù)測結(jié)果合并 后的數(shù)據(jù)集,命名為predict.csv

python實現(xiàn)代碼如下:

(1) 題目一:

#手動進行調(diào)節(jié)參數(shù)得到最好的準(zhǔn)確率
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import h2o
h2o.init()
from h2o.estimators.random_forest import H2ORandomForestEstimator
from __future__ import division 
df=h2o.import_file('train.csv')
trainData=df[2:]
 
model=H2ORandomForestEstimator(ntrees=6,max_depth =16)
model.train(x=trainData.names,y='Catrgory',training_frame=trainData)
df2=h2o.import_file('test.csv')
test_data=df2[2:]
pre_tag=H2ORandomForestEstimator.predict(model ,test_data)
predict=df2.concat(pre_tag)
dfnew=predict[predict['Catrgory']==predict['predict']]
Precision=dfnew.nrow/predict.nrow
 
print(Precision)
h2o.download_csv(predict,'predict.csv')

運行結(jié)果最好為87.0833%-6-16,如下

#for循環(huán)進行調(diào)節(jié)參數(shù)得到最好的準(zhǔn)確率
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import h2o
h2o.init()
from h2o.estimators.random_forest import H2ORandomForestEstimator
from __future__ import division 
df=h2o.import_file('train.csv')
trainData=df[2:]
df2=h2o.import_file('test.csv')
test_data=df2[2:]
Precision=0
nt=0
md=0
for i in range(1,50):
    for j in range(1,50):
      model=H2ORandomForestEstimator(ntrees=i,max_depth =j)
      model.train(x=trainData.names,y='Catrgory',training_frame=trainData)
      pre_tag=H2ORandomForestEstimator.predict(model ,test_data)
      predict=df2.concat(pre_tag)
      dfnew=predict[predict['Catrgory']==predict['predict']]
      p=dfnew.nrow/predict.nrow
      if Precision<p:
        Precision=p
        nt=i
        md=j
 
print(Precision)
print(i)
print(j)
h2o.download_csv(predict,'predict.csv')

運行結(jié)果最好為87.5%-49-49,如下

(2)題目二:建模如下,之后挑出排名前8的特征進行再次建模

#手動調(diào)節(jié)參數(shù)得到最大準(zhǔn)確率
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import h2o
h2o.init()
from h2o.estimators.random_forest import H2ORandomForestEstimator
from __future__ import division 
df=h2o.import_file('train.csv')
trainData=df[['Average_speed','r_a','r_b','v_a','v_d','Average_RPM','Variance_speed','v_c','Catrgory']]
df2=h2o.import_file('test.csv')
test_data=df2[['Average_speed','r_a','r_b','v_a','v_d','Average_RPM','Variance_speed','v_c','Catrgory']]
 
model=H2ORandomForestEstimator(ntrees=5,max_depth =18)
model.train(x=trainData.names,y='Catrgory',training_frame=trainData)
 
pre_tag=H2ORandomForestEstimator.predict(model ,test_data)
predict=df2.concat(pre_tag)
dfnew=predict[predict['Catrgory']==predict['predict']]
Precision=dfnew.nrow/predict.nrow
 
print(Precision)
h2o.download_csv(predict,'predict.csv')

運行結(jié)果最好為87.5%-5-18,如下

#for循環(huán)調(diào)節(jié)參數(shù)得到最大正確率
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import h2o
h2o.init()
from h2o.estimators.random_forest import H2ORandomForestEstimator
from __future__ import division 
df=h2o.import_file('train.csv')
trainData=df[['Average_speed','r_a','r_b','v_a','v_d','Average_RPM','Variance_speed','v_c','Catrgory']]
df2=h2o.import_file('test.csv')
test_data=df2[['Average_speed','r_a','r_b','v_a','v_d','Average_RPM','Variance_speed','v_c','Catrgory']]
Precision=0
nt=0
md=0
for i in range(1,50):
    for j in range(1,50):
      model=H2ORandomForestEstimator(ntrees=i,max_depth =j)
      model.train(x=trainData.names,y='Catrgory',training_frame=trainData)
      pre_tag=H2ORandomForestEstimator.predict(model ,test_data)
      predict=df2.concat(pre_tag)
      dfnew=predict[predict['Catrgory']==predict['predict']]
      p=dfnew.nrow/predict.nrow
      if Precision<p:
        Precision=p
        nt=i
        md=j
 
print(Precision)
print(i)
print(j)
h2o.download_csv(predict,'predict.csv')

運行結(jié)果最好為87.5%-49-49,如下 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python 合并拼接字符串的方法

    Python 合并拼接字符串的方法

    這篇文章主要介紹了Python 合并拼接字符串的方法,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 利用Python編寫一個記憶翻牌游戲

    利用Python編寫一個記憶翻牌游戲

    本文帶大家寫個小游戲,不過老是用pygame也沒啥意思,這次我們換點新花樣,用python自帶的tkinter包寫一個記憶翻牌小游戲,感興趣的可以了解一下
    2022-03-03
  • python裝飾器"@"使用實例深入探究

    python裝飾器"@"使用實例深入探究

    這篇文章主要為大家介紹了python裝飾器"@"使用實例深入探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • 20行python代碼實現(xiàn)人臉識別

    20行python代碼實現(xiàn)人臉識別

    這篇文章主要介紹了python人臉識別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • python commands模塊的適用方式

    python commands模塊的適用方式

    這篇文章主要介紹了python commands模塊的適用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決

    Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決

    這篇文章主要介紹了Matlab中的mat數(shù)據(jù)轉(zhuǎn)成python中使用的npy數(shù)據(jù)遇到的坑及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • python的圖形用戶界面介紹

    python的圖形用戶界面介紹

    大家好,本篇文章主要講的是python的圖形用戶界面介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Python import自定義模塊方法

    Python import自定義模塊方法

    python包含子目錄中的模塊方法比較簡單,關(guān)鍵是能夠在sys.path里面找到通向模塊文件的路徑。下面給大家介紹python import自定義模塊方法,需要的的朋友參考下
    2015-02-02
  • Django零基礎(chǔ)入門之模板變量詳解

    Django零基礎(chǔ)入門之模板變量詳解

    這篇文章主要介紹了Django零基礎(chǔ)入門之模板變量詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • 詳解python單元測試框架unittest

    詳解python單元測試框架unittest

    本篇文章給大家詳解了python單元測試框架unittest的相關(guān)知識點,有興趣的朋友參考學(xué)習(xí)下。
    2018-07-07

最新評論

柏乡县| 祁门县| 山东| 故城县| 调兵山市| 济阳县| 潢川县| 邵武市| 新龙县| 甘孜县| 鹤壁市| 龙里县| 冕宁县| 弥勒县| 阿拉善盟| 田东县| 株洲县| 长寿区| 秦安县| 老河口市| 门头沟区| 张北县| 岑巩县| 隆子县| 分宜县| 邓州市| 精河县| 合江县| 内丘县| 临洮县| 防城港市| 江西省| 班玛县| 特克斯县| 绥江县| 凤庆县| 昌平区| 涟源市| 富裕县| 剑阁县| 环江|