随机森林和随机森林SRC的深度和OOB错误。

问题描述 投票:1回答:1

这是我在R中的随机森林和rfsrc的代码;有没有办法像sklearn版本一样在我的R代码中包含n_estimators和max_depth?另外,我怎么能像这样绘制OBB错误与树数的关系图?

enter image description here

set.seed(2234)
tic("Time to train RFSRC fast")
fast.o <- rfsrc.fast(Label ~ ., data = train[(1:50000),],forest=TRUE)
toc()
print(fast.o)

#print(vimp(fast.o)$importance)

set.seed(2367)
tic("Time to test RFSRC fast ")
#data(breast, package = "randomForestSRC")
fast.pred <- predict(fast.o, test[(1:50000),])
toc()
print(fast.pred)


set.seed(3)
tic("RF model fitting without Parallelization")
rf <-randomForest(Label~.,data=train[(1:50000),])
toc()
print(rf)
plot(rf)
varImp(rf,sort = T)
varImpPlot(rf, sort=T, n.var= 10, main= "Variable Importance", pch=16)

rf_pred <- predict(rf, newdata=test[(1:50000),])
confMatrix <- confusionMatrix(rf_pred,test[(1:50000),]$Label)
confMatrix

我很感激你的时间。

r machine-learning python random-forest
1个回答
0
投票

你需要设置 block.size=1 ,还要注意采样是不需要替换的,你可以检查小节的rfsrc。

与Breiman的随机森林不同,这里的默认操作是不替换取样。 因此,袋外(OOB)在技术上是指样本外,但由于传统的原因,我们保留了OOB这个术语。

所以用一个示例数据集。

library(mlbench)
library(randomForestSRC)
data(Sonar)
set.seed(911)
trn = sample(nrow(Sonar),150)
rf <- rfsrc(Class ~ ., data = Sonar[trn,],ntree=500,block.size=1,importance=TRUE)
pred <- predict(rf,Sonar[-trn,],block.size=1)
plot(rf$err.rate[,1],type="l",col="steelblue",xlab="ntrees",ylab="err.rate",
ylim=c(0,0.5))
lines(pred$err.rate[,1],col="orange")
legend("topright",fill=c("steelblue","orange"),c("test","OOB.train"))

enter image description here

在RandomForest中。

library(randomForest)
rf <- randomForest(Class ~ ., data = Sonar[trn,],ntree=500)
pred <- predict(rf,Sonar[-trn,],predict.all=TRUE)

不太确定是否有一个更容易得到ntrees的错误。

err_by_tree = sapply(1:ncol(pred$individual),function(i){
apply(pred$individual[,1:i,drop=FALSE],1,
function(i)with(rle(i),values[which.max(lengths)]))
})

err_by_tree = colMeans(err_by_tree!=Sonar$Class[-trn])

然后绘制。

plot(rf$err.rate[,1],type="l",col="steelblue",xlab="ntrees",ylab="err.rate",
    ylim=c(0,0.5))
    lines(err_by_tree,col="orange")
    legend("topright",fill=c("steelblue","orange"),c("test","OOB.train"))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.