[英]Accessing Importance of each element in Random Forest in R
I have a prediction model using random forest with rolling window.我有一个使用带有滚动窗口的随机森林的预测模型。 In each iteration, I am saving the
importance
of the rf model into a list (let's say list1).在每次迭代中,我都将 rf 模型的
importance
保存到一个列表中(比如 list1)。 At the end, this list (list1) includes many lists (list2) of length 1. In each of these lists (list2), I have the importance
of one iteration.最后,这个列表 (list1) 包括许多长度为 1 的列表 (list2)。在这些列表 (list2) 中的每一个中,我都有一个迭代的
importance
。 I want to access the %IncMSE
of each element in this list (list2).我想访问此列表 (list2) 中每个元素的
%IncMSE
。 For example, I want to reach lag1.mat1 %IncMSE
which is 26.01262.例如,我想达到 lag1.mat1
%IncMSE
,即 26.01262。
The problem is that the list (list2) is of length one, so I am not able to access the value itself.问题是列表 (list2) 的长度为 1,因此我无法访问该值本身。
Is there any way I can do this?有什么办法可以做到这一点吗?
I am guessing your data looks like this:我猜你的数据是这样的:
library(randomForest)
lst = lapply(1:2,function(i){
list(randomForest(mpg~.,data=mtcars,importance=TRUE)$importance)
})
str(lst)
List of 2
$ :List of 1
..$ : num [1:10, 1:2] 6.921 9.651 7.495 0.984 9.178 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:10] "cyl" "disp" "hp" "drat" ...
.. .. ..$ : chr [1:2] "%IncMSE" "IncNodePurity"
$ :List of 1
..$ : num [1:10, 1:2] 7.138 10.257 7.422 0.631 10.027 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:10] "cyl" "disp" "hp" "drat" ...
.. .. ..$ : chr [1:2] "%IncMSE" "IncNodePurity"
head(lst[[1]][[1]])
%IncMSE IncNodePurity
cyl 6.9211021 170.12901
disp 9.6509747 248.54840
hp 7.4951588 206.99009
drat 0.9839875 64.95373
wt 9.1778411 246.08167
qsec 0.7464294 43.20173
If you only want cyl:如果你只想要 cyl:
sapply(lst,function(x)x[[1]]["cyl","%IncMSE"])
If you need everything:如果您需要一切:
lapply(lst,function(x)x[[1]][,"%IncMSE"])
It would have been helpful to see at least parts of your code, but basically, you can access them by doing this: Assuming you've grown this tree:至少查看部分代码会很有帮助,但基本上,您可以通过以下方式访问它们:假设您已经种植了这棵树:
RF = purrr::accumulate( .init = update(rf_mod, ntree=1),
rep(1,100), randomForest::grow )
imp = purrr::map( RF, ~importance(.x)[,"%IncMSE"] )
From there you get the one you need in the list.从那里你会得到你需要的列表。 For a more concise answer, post your code.
要获得更简洁的答案,请发布您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.