H2O中的随机森林算法介绍及其项目实战(python实现)
包的引入:from h2o.estimators.random_forest import H2ORandomForestEstimator
H2ORandomForestEstimator 的常用方法和参数介绍:
(一)建模方法:
model =H2ORandomForestEstimator(ntrees=n,max_depth =m) model.train(x=random_pv.names,y='Catrgory',training_frame=trainData)
通过trainData来构建随机森林模型,model.train中的trainData:训练集,x:预测变量名称,y:预测 响应变量的名称
(二)预测方法:
pre_tag=H2ORandomForestEstimator.predict(model ,test_data) 利用训练好的模型来对测试集进行预测,其中的model:训练好的模型, test_data:测试集。
(三)算法参数说明:
(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,如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]