randomForest模型大小取决于训练集大小: A way to avoid?

标签 r machine-learning memory-management classification random-forest

我正在训练一个 randomForest 模型,目的是保存它以进行预测(它将被下载并在外部上下文中使用)。我希望这个模型尽可能最小。

我读到有很多optionspackages减少模型的内存大小。

但是,我不明白为什么训练集的大小与模型的大小相关?毕竟,一旦森林的系数存在,为什么还需要保留原始数据集?

df <- iris
model <- randomForest::randomForest(Species ~ ., data = df, 
                 localImp = FALSE,
                 importance = FALSE,
                 keep.forest = TRUE,
                 keep.inbag = FALSE,
                 proximity=FALSE,
                 ntree = 25)
object.size(model)/1000
#> 73.2 bytes

df <- df[sample(nrow(df), 50), ]
model <- randomForest::randomForest(Species ~ ., data = df, 
                 localImp = FALSE,
                 importance = FALSE,
                 keep.forest = TRUE,
                 keep.inbag = FALSE,
                 proximity=FALSE,
                 ntree = 25)
object.size(model)/1000
#> 43 bytes

reprex package于2019年5月21日创建(v0.2.1)

我已经尝试了上面提到的减少大小的技巧,但与训练集大小的作用相比,它们的效果是微乎其微的。有没有办法删除这些信息?

最佳答案

我认为您可以在拟合后删除模型的某些部分:

object.size(model)/1000
# 70.4 bytes

model$predicted <- NULL # remove predicted
model$y <- NULL # remove y
#.. possibly other parts aren't needed
object.size(model)/1000
# 48.3 bytes

我检查了 predict(model, df) 看看它是否仍然有效,而且确实有效。

使用names(model)检查model内的元素。

看起来$votes很大,你不需要它,这里我安全删除了更多项目:

model$predicted <- NULL
model$y <- NULL
model$err.rate <- NULL
model$test <- NULL
model$proximity <- NULL
model$confusion <- NULL
model$localImportance <- NULL
model$importanceSD <- NULL
model$inbag <- NULL
model$votes <- NULL
model$oob.times <- NULL


object.size(model)/1000
# 32.3 bytes

示例:

df <- iris
model <- randomForest::randomForest(Species ~ ., data = df, 
                 localImp = FALSE,
                 importance = FALSE,
                 keep.forest = TRUE,
                 keep.inbag = FALSE,
                 proximity=FALSE,
                 ntree = 25)

关于randomForest模型大小取决于训练集大小: A way to avoid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56234990/

相关文章:

r - Httr header 在数字文字中返回无效字符 '-'

r - 在同一帧中绘制两个函数

python-2.7 - 为什么下面的代码片段运行不成功?

r - 矩阵和向量形式的数据点数量

r - 以整洁的方式匿名化数据框的选定列

machine-learning - 具有更高抽象级别的机器学习

Python-Classifier-Xgboost - 在 GridSearchCV 中显示带有参数、持续时间、分数的 cv

c - 使用大量内存的堆实现 - C

将堆栈内存用于不完整结构的 C 最佳实践

memory-management - 寻址模式如何在物理层面上工作?