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

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

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

H2ORandomForestEstimator 的經常使用方法和參數介紹:框架

(一)建模方法: dom

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

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

經過trainData來構建隨機森林模型,model.train中的trainData訓練集預測變量名稱預測 響應變量的名稱rest

(二)預測方法:blog

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

(三)算法參數說明:pandas

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

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

項目要求:

題目一: 利用train.csv中的數據,經過H2O框架中的隨機森林算法構建分類模型,而後利用模型對 test.csv中的數據進行預測,並計算分類的準確度進而評價模型的分類效果;經過調節參 數,觀察分類準確度的變化狀況。 注:準確度=預測正確的數佔樣本數的比例

題目二: 經過H2o Flow 的隨機森林算法,用同題目一中所用一樣的訓練數據和參數,構建模型; 參看模型中特徵的重要性程度,從中選取前8個特徵,再去訓練模型,並從新預測結果, 進而計算分類的準確度。

需求完成內容:2個題目的代碼,認爲最好的準確度的輸出值和test數據與預測結果合併 後的數據集,命名爲predict.csv

 

python實現代碼以下:

(1) 題目一:

#手動進行調節參數獲得最好的準確率
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')

運行結果最好爲87.0833%-6-16,以下

#for循環進行調節參數獲得最好的準確率
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')

 運行結果最好爲87.5%-49-49,以下

(2)題目二:建模以下,以後挑出排名前8的特徵進行再次建模

#手動調節參數獲得最大準確率
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')

  運行結果最好爲87.5%-5-18,以下

#for循環調節參數獲得最大正確率
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')

 運行結果最好爲87.5%-49-49,以下 

相關文章
相關標籤/搜索