r - 如何从 R 中的 RandomForest 中提取节点大小默认值
问题描述
我知道如何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)
解决方案
碰巧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
推荐阅读
- javascript - 在 javascript/jquery 中从 Rmarkdown 生成的 html 笔记本中选择“隐藏”按钮
- c# - 如何在没有计时器的情况下使 if 语句变为真时执行的事件 C#
- node.js - MongoDB findOne 在服务器重启后返回 null
- rust - Actix 2.0 中如何从请求中获取 Cookie
- r - R中的for循环模拟密码门
- node.js - Nodejs node-mailjet - 导入行无法静态导入
- vim - vim 中的硬刷新(不是 :so %)
- java - 严重:servlet [Jersey Web Application] 在路径 [/restapp] 的上下文中的 Servlet.service() 引发异常 [java.util.NoSuchElementException]
- google-maps - 将谷歌地图图层添加到底图库 arcgis 3.32
- wowza - 我可以使用 Wowze 中的模块将多个线程合二为一吗?