为什么装袋树的错误率比单棵树的错误率高?

问题描述 投票:0回答:1

我交叉发布了这个问题here,但在我看来,我不太可能收到任何答案。所以我在这里发布。


我正在运行分类方法Bagging Tree(Bootstrap Aggregation),并将误分类错误率与一棵单树中的错误率进行比较。我们期望装袋树的结果要比一棵单棵树的结果更好,即装袋的错误率低于一棵树。

我重复整个过程M = 100次(每次将原始数据集随机分为训练集和测试集),以获得100个测试错误和装袋测试错误(使用for循环)。然后,我使用箱线图来比较这两种类型的错误的分布。

# Loading package and data
library(rpart)
library(boot)
library(mlbench)
data(PimaIndiansDiabetes)

# Initialization
n <- 768
ntrain <- 468
ntest <- 300
B <- 100
M <- 100
single.tree.error <- vector(length = M)
bagging.error <- vector(length = M)

# Define statistic
estim.pred <- function(a.sample, vector.of.indices)
      {
      current.train <- a.sample[vector.of.indices, ]
      current.fitted.model <- rpart(diabetes ~ ., data = current.train, method = "class")
      predict(current.fitted.model, test.set, type = "class")
      }

for (j in 1:M)
      {
      # Split the data into test/train sets
      train.idx <- sample(1:n, ntrain, replace = FALSE)
      train.set <- PimaIndiansDiabetes[train.idx, ]
      test.set <- PimaIndiansDiabetes[-train.idx, ]

      # Train a direct tree model
      fitted.tree <- rpart(diabetes ~ ., data = train.set, method = "class")
      pred.test <- predict(fitted.tree, test.set, type = "class")
      single.tree.error[j] <- mean(pred.test != test.set$diabetes)


      # Bootstrap estimates
      res.boot = boot(train.set, estim.pred, B)
      pred.boot <- vector(length = ntest)
      for (i in 1:ntest)
            {
            pred.boot[i] <- ifelse (mean(res.boot$t[, i] == "pos")  >= 0.5, "pos", "neg")
            }
      bagging.error[j] <- mean(pred.boot != test.set$diabetes)
      }

boxplot(single.tree.error, bagging.error, ylab = "Misclassification errors", names = c("single.tree", "bagging"))

结果是

enter image description here

您能否解释为什么套袋树的错误率比单棵树高得多?我觉得这没有道理。我检查了我的代码,但未发现任何异常。

r machine-learning bootstrap-4 decision-tree
1个回答
0
投票

我已经收到https://stats.stackexchange.com/questions/452882/why-is-the-error-rate-from-bagging-trees-much-higher-than-that-from-a-single-tre的答复。我将其发布在此处以关闭此问题并供将来的访问者使用。

© www.soinside.com 2019 - 2024. All rights reserved.