简体   繁体   English

R RandomForest:新对象的邻近度

[英]R RandomForest: Proximity for new object

I trained a random forest: 我训练了一个随机森林:

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

When I want to predict y for new objects, I use 当我要为新对象预测y时,我使用

y_pred <- predict(model, xnew)

How can I calculate the proximity between the new objects (xnew) and the training set (x) based on the already existing forest (model)? 如何根据已经存在的森林(模型)计算新对象(xnew)与训练集(x)之间的接近度? The proximity option in the predict function gives only the proxmities among the new objects (xnew). 预测函数中的接近选项仅提供新对象(xnew)之间的邻近性。 I could run randomForest unsupervised again on a combined data set (x and xnew) to get the proximities, but I think there must be some way to avoid building the forest again and instead using the already existing one. 我可以在组合数据集(x和xnew)上再次在无人监督的情况下运行randomForest来获得邻近性,但是我认为必须有某种方法来避免再次构建森林,而要使用已经存在的森林。

Thanks! 谢谢! Kilian 基利安

I believe what you want is to specify your test observations in the randomForest call itself, something like this: 我相信您想要的是在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

So that gives you the proximity from the ten test cases to all 150. 这样一来,您可以从十个测试用例到全部150个用例。

The only other option would be to call predict on your new case rbind ed to the original training cases, I think. 唯一的其他选择是调用predict在新的情况下rbind版原来的训练情况,我想。 But that way you don't need to have your test cases up front with the randomForest call. 但是那样一来,您无需通过randomForest调用就randomForest

In that case, you'll want to use keep.forest = TRUE in the randomForest call and of course set proximity = TRUE when you call predict . 在这种情况下,您将需要在randomForest调用中使用keep.forest = TRUE ,并且在调用predict时当然要设置proximity = TRUE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM