2

我训练了一个随机森林:

model <- randomForest(x, y, proximity=TRUE)

当我想预测新对象的 y 时,我使用

y_pred <- predict(model, xnew)

如何根据已经存在的森林(模型)计算新对象(xnew)和训练集(x)之间的接近度?predict 函数中的邻近选项仅给出新对象 (xnew) 之间的邻近度。我可以在组合数据集(x 和 xnew)上再次无监督地运行 randomForest 以获得近似值,但我认为必须有某种方法可以避免再次构建森林,而是使用已经存在的森林。

谢谢!基连

4

1 回答 1

5

我相信您想要的是在randomForest调用本身中指定您的测试观察结果,如下所示:

set.seed(71)
ind <- sample(1:150,140,replace = FALSE)
train <- iris[ind,]
test <- iris[-ind,]

iris.rf1 <- randomForest(x = train[,1:4],
                         y = train[,5],
                         xtest = test[,1:4],
                         ytest = test[,5], 
                         importance=TRUE,
                         proximity=TRUE)

dim(iris.rf1$test$prox)
[1]  10 150

这样一来,您就可以从 10 个测试用例到全部 150 个测试用例。

我认为,唯一的其他选择是将predict您的新案例rbind与原始培训案例联系起来。但这样一来,您就不需要在randomForest调用前预先准备好您的测试用例。

在这种情况下,您需要keep.forest = TRUErandomForest通话中使用,当然在通话proximity = TRUE时设置predict

于 2011-12-16T19:36:58.427 回答