欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

H2O中的隨機(jī)森林算法介紹及其項目實戰(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)建隨機(jī)森林模型,model.train中的trainData:訓(xùn)練集,x:預(yù)測變量名稱,y:預(yù)測 響應(yīng)變量的名稱

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

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

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

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

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

項目要求:

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

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

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

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

(1) 題目一:

#手動進(jì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:]
 
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')

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

#for循環(huán)進(jì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')

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

(2)題目二:建模如下,之后挑出排名前8的特征進(jì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[['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')

運(yùn)行結(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')

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

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

相關(guān)文章

  • 手把手教你實現(xiàn)Python連接數(shù)據(jù)庫并快速取數(shù)的工具

    手把手教你實現(xiàn)Python連接數(shù)據(jù)庫并快速取數(shù)的工具

    在數(shù)據(jù)生產(chǎn)應(yīng)用部門,取數(shù)分析是一個很常見的需求,實際上業(yè)務(wù)人員需求時刻變化,最高效的方式是讓業(yè)務(wù)部門自己來取。本文就來手把手教大家搭建一個?Python?連接數(shù)據(jù)庫,快速取數(shù)工具,需要的可以參考一下
    2022-11-11
  • Python中最強(qiáng)大的重試庫Tenacity使用探索

    Python中最強(qiáng)大的重試庫Tenacity使用探索

    這篇文章主要為大家介紹了Python中最強(qiáng)大的重試庫Tenacity使用探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • python selenium反檢測問題

    python selenium反檢測問題

    這篇文章主要介紹了python selenium反檢測問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • DRF框架API版本管理實現(xiàn)方法解析

    DRF框架API版本管理實現(xiàn)方法解析

    這篇文章主要介紹了DRF框架API版本管理實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • python將字典內(nèi)容存入mysql實例代碼

    python將字典內(nèi)容存入mysql實例代碼

    這篇文章主要介紹了python將字典內(nèi)容存入mysql實例代碼,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • 詳解Django中的權(quán)限和組以及消息

    詳解Django中的權(quán)限和組以及消息

    這篇文章主要介紹了詳解Django中的權(quán)限和組以及消息,在Python百花齊放的web框架中,Django是人氣最高的一個,需要的朋友可以參考下
    2015-07-07
  • 詳解Python中time()方法的使用的教程

    詳解Python中time()方法的使用的教程

    這篇文章主要介紹了詳解Python中time()方法的使用的教程,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • Python求最小公倍數(shù)4種方法總結(jié)

    Python求最小公倍數(shù)4種方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于Python求最小公倍數(shù)4種方法的相關(guān)資料,最小公倍數(shù)不可以像最大公約數(shù)那樣直接利用輾轉(zhuǎn)相除法求出,但可以借助輾轉(zhuǎn)相除法求得的最大公約數(shù)來求最小公倍數(shù),需要的朋友可以參考下
    2023-10-10
  • django admin組件使用方法詳解

    django admin組件使用方法詳解

    這篇文章主要介紹了django admin組件使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • Python進(jìn)階之高級用法詳細(xì)總結(jié)

    Python進(jìn)階之高級用法詳細(xì)總結(jié)

    今天帶各位小伙伴學(xué)習(xí)一下Python高級語法,主要有Lambda表達(dá)式,map函數(shù),filter函數(shù),reduce函數(shù),三大推導(dǎo)式等,文中有非常詳細(xì)的介紹,需要的朋友可以參考下
    2021-05-05

最新評論