R 自动调整随机林
我想自动调整随机森林模型,因为我的变量在实时基础上不断变化。我正在使用R 自动调整随机林,r,R,我想自动调整随机森林模型,因为我的变量在实时基础上不断变化。我正在使用iris数据集 我的代码是 jk = iris library(randomForest) library(caret) fgl.res <- tuneRF(lm[,-5], lm[,5], stepFactor=1.5) o/p: mtry = 2 OOB error = 5.33% Searching left ... Searching right ... mtry = 3 OOB error =
iris
数据集
我的代码是
jk = iris
library(randomForest)
library(caret)
fgl.res <- tuneRF(lm[,-5], lm[,5], stepFactor=1.5)
o/p:
mtry = 2 OOB error = 5.33%
Searching left ...
Searching right ...
mtry = 3 OOB error = 4%
0.25 0.05
mtry = 4 OOB error = 5.33%
-0.3333333 0.05
我已经尝试了很多方法,但是对于如何从
fgl.res
输出自动将值插入到代码中没有任何结果。我不知道我是否正确理解了您的问题,但您可能会使用这种方法。使用tuneRF时,必须选择OOB错误最低的mtry。 我使用
不可见(capture.output(…)
函数在进入tuneRF函数后隐藏并显示控制台中的任何输出
例如:
# load library
library(randomForest)
library(caret)
# data
data_iris = iris
# repeat the analysis
set.seed(4543)
# tuneRF
invisible(capture.output(fgl.res <- tuneRF(x = data_iris[,-5], y=
data_iris[,5], stepFactor=1.5)))
# choose the best mtry based on the lowest OOB error
best_mtry <- fgl.res[fgl.res[, 2] == min(fgl.res[, 2]), 1]
# choose the lowest OOB error
best_oob <- fgl.res[fgl.res[, 2] == min(fgl.res[, 2]), 2]
# caluclate RF
mod2<-randomForest(Species~., data=data_iris, ntree=50, mtry=best_mtry,
oob.error=best_oob)
#加载库
图书馆(森林)
图书馆(插入符号)
#资料
数据虹膜=虹膜
#重复分析
种子集(4543)
#调谐器
不可见(捕获输出(fgl.res
# load library
library(randomForest)
library(caret)
# data
data_iris = iris
# repeat the analysis
set.seed(4543)
# tuneRF
invisible(capture.output(fgl.res <- tuneRF(x = data_iris[,-5], y=
data_iris[,5], stepFactor=1.5)))
# choose the best mtry based on the lowest OOB error
best_mtry <- fgl.res[fgl.res[, 2] == min(fgl.res[, 2]), 1]
# choose the lowest OOB error
best_oob <- fgl.res[fgl.res[, 2] == min(fgl.res[, 2]), 2]
# caluclate RF
mod2<-randomForest(Species~., data=data_iris, ntree=50, mtry=best_mtry,
oob.error=best_oob)
# choose the best mtry based on the lowest OOB error
best_mtry <- fgl.res[1, 1]
# choose the lowest OOB error
best_oob <- fgl.res[1, 2]