如何从R中的RandomForest中提取节点大小的默认值

时间:2018-12-02 12:19:51

标签: r random-forest

我知道如何在nodesize中更改RandomForest的值(终端节点中的最小行数)。但是,我想知道在给定RandomForest模型的情况下nodesize的值是什么。

require(party)
require (data.table)
require (e1071)
require (randomForest)
dat1 <- fread('https://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data',stringsAsFactors=T)

## split data to train and test
set.seed(123)
dat1 <- subset(dat1, !is.na(V1))
smp_size<-0.8*nrow(dat1)
train_ind <- sample(seq_len(nrow(dat1)), size = smp_size)
train <- dat1[train_ind, ]
test <- dat1[-train_ind, ]
rf1 <- randomForest(V1 ~ ., data = train,keep.inbag = TRUE)
rf2 <- randomForest(V1 ~ ., data = train, ntree = 50,keep.inbag = TRUE)

1 个答案:

答案 0 :(得分:3)

碰巧randomForest不返回节点大小参数。但是,只有“三个”可能的值:或者由用户指定,或者由if (!is.null(y) && !is.factor(y)) 5 else 1设置(分类为1,回归为5)。因此,我们有

getNodesize <- function(x) {
  look <- pmatch(names(x$call), "nodesize")
  if(any(!is.na(look)))
    x$call[!is.na(look)][[1]]
  else if (!is.null(x$y) && !is.factor(x$y))
    5
  else
    1
}

rf1 <- randomForest(V1 ~ ., data = train)
getNodesize(rf1)
# [1] 1
rf1 <- randomForest(V1 ~ ., data = train, nodesi = 3)
getNodesize(rf1)
# [1] 3