r - randomForest、randomForestSRC 或 cforest 中单棵树的重要性可变吗?

标签 r tree random-forest party ensemble-learning

我正在尝试在 R 中找到一种方法来计算随机森林或条件随机森林的单棵树的变量重要性。
一个好的起点是 rpart:::importance 命令,它计算 rpart 树的变量重要性的度量:

> library(rpart) 
> rp <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
> rpart:::importance(rp)
   Start      Age   Number 
8.198442 3.101801 1.521863

randomForest::getTree 命令是从 randomForest 对象中提取树结构的标准工具,但它返回一个 data.frame:

library(randomForest)
rf <- randomForest(Kyphosis ~ Age + Number + Start, data = kyphosis)
tree1 <- getTree(rf, k=1, labelVar=TRUE)
str(tree1)

'data.frame':   29 obs. of  6 variables:
$ left daughter : num  2 4 6 8 10 12 0 0 14 16 ...
$ right daughter: num  3 5 7 9 11 13 0 0 15 17 ...
$ split var     : Factor w/ 3 levels "Age","Number",..: 2 3 1 2 3 3 NA NA 3 1 ...
$ split point   : num  5.5 8.5 78 3.5 14.5 7.5 0 0 3.5 75 ...
$ status        : num  1 1 1 1 1 1 -1 -1 1 1 ...
erce$ prediction    : chr  NA NA NA NA ...

解决方案是使用 as.rpart 命令将 tree1 强制为 rpart 对象。不幸的是,我不知道任何 R 包中都有这个命令。

使用party包我发现了类似的问题。 varimp 命令适用于 cforest 对象,而不适用于单个树。

library(party) 
cf <- cforest(Kyphosis ~ Age + Number + Start, data = kyphosis) 
ct <- party:::prettytree(cf@ensemble[[1]], names(cf@data@get("input"))) 
tree2 <- new("BinaryTree") 
tree2@tree <- ct 
tree2@data <- cf@data 
tree2@responses <- cf@responses 
tree2@weights <- cf@initweights
varimp(tree2)

Error in varimp(tree2) : 
   no slot of name "initweights" for this object of class "BinaryTree"

感谢任何帮助。

最佳答案

根据@Alex 的建议,我参与了party:::varimp。此命令计算 cforest 的标准(平均降低精度)和条件变量重要性 (VI),并且可以轻松修改以计算森林中每棵树的 VI。

set.seed(12345)
y <- cforest(score ~ ., data = readingSkills,
       control = cforest_unbiased(mtry = 2, ntree = 10))

varimp_ctrees <- function (object, mincriterion = 0, conditional = FALSE,
threshold = 0.2, nperm = 1, OOB = TRUE, pre1.0_0 = conditional) {
    response <- object@responses
    if (length(response@variables) == 1 && inherits(response@variables[[1]], 
        "Surv")) 
        return(varimpsurv(object, mincriterion, conditional, 
            threshold, nperm, OOB, pre1.0_0))
    input <- object@data@get("input")
    xnames <- colnames(input)
    inp <- initVariableFrame(input, trafo = NULL)
    y <- object@responses@variables[[1]]
    if (length(response@variables) != 1) 
        stop("cannot compute variable importance measure for multivariate response")
    if (conditional || pre1.0_0) {
        if (!all(complete.cases(inp@variables))) 
            stop("cannot compute variable importance measure with missing values")
    }
    CLASS <- all(response@is_nominal)
    ORDERED <- all(response@is_ordinal)
    if (CLASS) {
        error <- function(x, oob) mean((levels(y)[sapply(x, which.max)] != 
            y)[oob])
    } else {
        if (ORDERED) {
            error <- function(x, oob) mean((sapply(x, which.max) != 
                y)[oob])
        } else {
            error <- function(x, oob) mean((unlist(x) - y)[oob]^2)
        }
    }
    w <- object@initweights
    if (max(abs(w - 1)) > sqrt(.Machine$double.eps)) 
        warning(sQuote("varimp"), " with non-unity weights might give misleading results")
    perror <- matrix(0, nrow = nperm * length(object@ensemble), 
        ncol = length(xnames))
    colnames(perror) <- xnames
    for (b in 1:length(object@ensemble)) {
        tree <- object@ensemble[[b]]
        if (OOB) {
            oob <- object@weights[[b]] == 0
        } else {
            oob <- rep(TRUE, length(y))
        }
        p <- .Call("R_predict", tree, inp, mincriterion, -1L, 
            PACKAGE = "party")
        eoob <- error(p, oob)
        for (j in unique(party:::varIDs(tree))) {
            for (per in 1:nperm) {
                if (conditional || pre1.0_0) {
                  tmp <- inp
                  ccl <- create_cond_list(conditional, threshold, 
                    xnames[j], input)
                  if (is.null(ccl)) {
                    perm <- sample(which(oob))
                  }  else {
                    perm <- conditional_perm(ccl, xnames, input, 
                      tree, oob)
                  }
                  tmp@variables[[j]][which(oob)] <- tmp@variables[[j]][perm]
                  p <- .Call("R_predict", tree, tmp, mincriterion, 
                    -1L, PACKAGE = "party")
                } else {
                  p <- .Call("R_predict", tree, inp, mincriterion, 
                    as.integer(j), PACKAGE = "party")
                }
                perror[(per + (b - 1) * nperm), j] <- (error(p, 
                  oob) - eoob)
            }
        }
    }
    perror <- as.data.frame(perror)
    return(list(MeanDecreaseAccuracy = colMeans(perror), VIMcTrees=perror))
}

VIMcTrees 是一个矩阵,其行数等于森林树木的数量,每个解释变量有一列。该矩阵的 (i,j) 元素是第 i 树中第 j 变量的 VI。

varimp_ctrees(y)$VIMcTrees

   nativeSpeaker       age  shoeSize
1       4.853855  30.06969 52.271824
2      15.740311  70.55825  5.409772
3      17.022082 113.86020  0.000000
4      22.003119  19.62134 50.634286
5       6.070659  28.58817 47.049866
6      16.508634 105.50321  2.302387
7      11.487349  31.80002 46.147677
8      19.250631  27.78282 43.589832
9      19.669478  98.73722  0.483079
10     11.748669  85.95768  5.812538

关于r - randomForest、randomForestSRC 或 cforest 中单棵树的重要性可变吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34346815/

相关文章:

R,我怎样才能用一个简单的矩阵制作热图?

algorithm - 在图中制作成本矩阵

最小直径生成树算法

machine-learning - 随机森林实现之间的差异

r - 如何计算R中的randomForest训练AUC

r - which.max()不返回NA

regex - 仅保留第一个连字符和连字符本身之前的数字

r - 按组值的 SD 列的数据表字符串串联

machine-learning - XGBoost/CatBoost 中具有大量类别的分类变量

java - 如果必须填充数组,我该如何使它们相等?