【问题标题】:R caret train Error in evalSummaryFunction: cannnot compute class probabilities for regressionevalSummaryFunction中的R插入符号训练错误:无法计算回归的类概率
【发布时间】:2014-07-07 09:03:27
【问题描述】:
> cv.ctrl <- trainControl(method = "repeatedcv", repeats = 3,
+                         summaryFunction = twoClassSummary,
+                         classProbs = TRUE)
> 
> set.seed(35)
> glm.tune.1 <- train(y ~ bool_3,
+                     data = train.batch,
+                     method = "glm",
+                     metric = "ROC",
+                     trControl = cv.ctrl)
Error in evalSummaryFunction(y, trControl, classLevels, metric, method) : 
  train()'s use of ROC codes requires class probabilities. See the classProbs option of trainControl()
In addition: Warning message:
In train.default(x, y, weights = w, ...) :
  cannnot compute class probabilities for regression


 > str(train.batch)
'data.frame':   128046 obs. of  42 variables:
 $ offer               : int  1194044 1194044 1194044 1194044 1194044 1194044 1194044 1194044 1194044 1194044 ...
 $ avgPrice            : num  2.68 2.68 2.68 2.68 2.68 ...
 ...
 $ bool_3              : int  0 0 0 0 0 0 0 1 0 0 ...
 $ y                   : num  0 1 0 0 0 1 1 1 1 0 ...

由于 cv.ctrl 将 classProbs 设置为 TRUE,我不明白为什么会出现此错误消息。

有人可以建议吗?

【问题讨论】:

    标签: r r-caret


    【解决方案1】:

    显然这个错误是由于我的 y 不是因素

    以下代码可以正常工作:

    library(caret)
    library(mlbench)
    data(Sonar)
    
    ctrl <- trainControl(method = "cv", 
                         summaryFunction = twoClassSummary, 
                         classProbs = TRUE)
    set.seed(1)
    gbmTune <- train(Class ~ ., data = Sonar,
                     method = "gbm",
                     metric = "ROC",
                     verbose = FALSE,                    
                     trControl = ctrl)
    

    然后做:

    Sonar$Class = as.numeric(Sonar$Class)
    

    同样的代码会抛出错误:

    > gbmTune <- train(Class ~ ., data = Sonar,
    +                  method = "gbm",
    +                  metric = "ROC",
    +                  verbose = FALSE,                    
    +                  trControl = ctrl)
    Error in evalSummaryFunction(y, trControl, classLevels, metric, method) : 
      train()'s use of ROC codes requires class probabilities. See the classProbs option of trainControl()
    In addition: Warning message:
    In train.default(x, y, weights = w, ...) :
      cannnot compute class probabilities for regression
    

    但是插入符号火车文档说:

    y   a numeric or factor vector containing the outcome for each sample.
    

    【讨论】:

    • train 适用于回归(当y 是一个数字时)和分类(当它是一个因素时)。
    【解决方案2】:

    如果您将 y 中的值分别更改为“YES”和“NO”而不是 1 和 0,则代码将运行。

    y=ifelse(train.batch$y==0,"No","Yes")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 2018-06-24
      • 2017-03-09
      • 1970-01-01
      • 2018-09-19
      • 2018-06-08
      • 1970-01-01
      相关资源
      最近更新 更多