【问题标题】:How to get the predicted class instead of class probabilities?如何获得预测类别而不是类别概率?
【发布时间】:2016-12-04 19:54:56
【问题描述】:

我已经使用caret 包训练了一个随机森林来预测二元分类任务。

library(caret)
set.seed(78)
inTrain <- createDataPartition(disambdata$Response, p=3/4, list = FALSE)
trainSet <- disambdata[inTrain,]
testSet <- disambdata[-inTrain,]
ctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 10)
grid_rf <- expand.grid(.mtry = c(3,5,7,9))
set.seed(78)
m_rf <- train(Response ~ ., data=trainSet, 
          method= "rf", metric = "Kappa", trcontrol=ctrl, tuneGrid = grid_rf)

Response 变量包含值 {Valid, Invalid}。 使用以下我得到测试数据的类概率:

pred <- predict.train(m_rf, newdata = testSet, 
                  type="prob", models=m_rf$finalModel)

但是我有兴趣获得预测的类别ValidInvalid 而不是类别概率生成混淆矩阵 .

我已经在predict.train 函数中尝试了参数type="raw",但它返回了NAs 的列表。

【问题讨论】:

    标签: r random-forest r-caret confusion-matrix


    【解决方案1】:

    通过在 predict() 函数中分配 type = "prob" ,您具体要求的是概率。只需删除它,它就会提供标签

    pred <- predict.train(m_rf, newdata = testSet,models=m_rf$finalModel)
    

    【讨论】:

    • 不幸的是,它对我不起作用,删除 type=prob 没有好处。 pred 接收 NAs
    【解决方案2】:

    似乎插入符号包(caret_6.0-70)仍然存在公式界面问题。将公式从 Response ~ . 扩展为明确提及所有预测变量的公式 Response ~ MaxLikelihood + n1 + n2 + count 解决了问题,predict.train(m_rf, newdata=testSet) 返回预测的类。

    【讨论】:

    • 如果是bug,请在caret的github页面上报告。
    • @phiver,是的,我确实计划报告它,因为它也被报告为包插入符号的先前版本之一。我拥有的版本(6.0-70)的发行说明提到这个问题已经得到解决。不管怎样,我会在 github 上报告这个错误
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 2018-04-19
    • 2020-08-25
    • 2017-06-12
    • 2019-05-25
    • 1970-01-01
    • 2021-02-11
    • 2013-01-26
    相关资源
    最近更新 更多