【问题标题】:Caret's train and confusionMatrix functionsCaret 的 train 和confusionMatrix 函数
【发布时间】:2014-05-23 00:03:51
【问题描述】:

我正在尝试按照 Max Khun 的 Applied Predictive Modeling 一书了解插入符号的工作原理,但无法理解插入符号的混淆矩阵函数的工作原理。

我使用glmnet训练了8190行1073列的训练数据集(training[,fullSet]),如下:

glmnGrid <- expand.grid(alpha = c(0,  .1,  .2, .4, .6, .8, 1),
                    lambda = seq(.01, .2, length = 40))

ctrl <- trainControl(method = "cv", 
                 number = 10,
                 summaryFunction = twoClassSummary,
                 classProbs = TRUE,
                 index = list(TrainSet = pre2008),
                 savePredictions = TRUE)

glmnFit <- train(x = training[,fullSet], 
             y = training$Class,
             method = "glmnet",
             tuneGrid = glmnGrid,
             preProc = c("center", "scale"),
             metric = "ROC",
             trControl = ctrl)

然后,我从拟合中打印出混淆矩阵:

glmnetCM <- confusionMatrix(glmnFit, norm = "none")

当我查看混淆矩阵时,我得到了以下结果:

               Reference
Prediction     successful unsuccessful
  successful          507          208
  unsuccessful         63          779

但是,我不明白为什么混淆表只有 1757 个观察值(1757 = 507 + 208 + 63 + 779),因为插入符号的confusionMatrix.train 文档说“当训练用于调整模型时,它会跟踪保留样本的混淆矩阵单元格条目。”由于训练数据集有 8190 行,我使用了 10 倍的 CV,所以我认为混淆矩阵应该基于 819 个数据点(819 = 8190 / 10),事实并非如此。

显然我不完全理解插入符号的 trainControl 或 train 是如何工作的。有人能解释一下我误解了什么吗?

非常感谢您的帮助。

李永珍

【问题讨论】:

    标签: r r-caret confusion-matrix


    【解决方案1】:

    问题在于控制参数。您正在使用method = "cv"number = 10,但您还指定了将用于拟合模型的精确重采样(通过index 参数)。我假设这是来自the book 的授权数据。在第 12 章中,我们描述了数据拆分方案,其中pre2008 向量表示 8,190 个样本中的 6,633 个将用于训练。在模型调整过程中剩下 1,557 个:

    > dim(training)
    [1] 8190 1785
    > length(pre2008)
    [1] 6633
    > 8190-6633
    [1] 1557
    

    对非pre2008 样本的预测就是您在表格中看到的。如果你想重现我们所拥有的,第 312 页的语法是正确的:

    ctrl <- trainControl(method = "LGOCV",
                         summaryFunction = twoClassSummary,
                         classProbs = TRUE,
                         index = list(TrainSet = pre2008))
    

    如果你只想做 10 倍 CV,请去掉 index 参数。

    tl;dr 控制函数表示 10 倍 CV,但 index 参数表示应使用 1,557 个样本中的一个。

    最大

    【讨论】:

      猜你喜欢
      • 2018-03-16
      • 2017-07-20
      • 1970-01-01
      • 2021-03-28
      • 2017-01-13
      • 1970-01-01
      • 2015-12-28
      • 2018-11-16
      • 2018-03-11
      相关资源
      最近更新 更多