【问题标题】:How does R judges positive and negative factor variables?R如何判断正负因子变量?
【发布时间】:2016-02-11 23:13:36
【问题描述】:

我想做一个二进制分类,一个是“top”,另一个是“bottom”。我在 h2o 包中使用 gbm 并将“底部”作为正类,将“顶部”作为负类。 这是我的代码:

fit <- h2o.gbm(x = regr.var, y = max.var,
             training_frame = ddd, 
             nfolds = 10, 
             distribution = 'multinomial',
             balance_classes = TRUE)
pred <- as.data.frame(h2o.predict(fit, newdata = eee))
threshold <- 0.5
pred1 <- factor( ifelse(pred[, 'top'] > threshold, 'top', 'bottom') )
err.res<-confusionMatrix(pred1 , hh$score_class)
err.res

结果如下:

Confusion Matrix and Statistics
           Reference
Prediction bottom top
bottom      420   123
top          1     6
Accuracy : 0.7745          
95% CI : (0.7373, 0.8088)
No Information Rate : 0.7655          
P-Value [Acc > NIR] : 0.3279          

Kappa : 0.0657          
Mcnemar's Test P-Value : <2e-16          

Sensitivity : 0.99762         
Specificity : 0.04651         
Pos Pred Value : 0.77348         
Neg Pred Value : 0.85714         
Prevalence : 0.76545         
Detection Rate : 0.76364         
Detection Prevalence : 0.98727         
Balanced Accuracy : 0.52207         

'Positive' Class : bottom          

但我想正确预测更多“顶部”。我尝试将阈值更改为 0.3,它的性能更好。但是,我是否应该在拟合过程中进行更改以对“ROC”指标等“顶部”做出更多预测?我应该将“顶部”翻转为正类,将“底部”翻转为负类,我该如何改变它?

【问题讨论】:

  • 你有一个班级不平衡的案例。如果您想正确预测较小的类别(即您的示例中的“top”),您将必须修改您的方法以使正确分类较小的类别具有更大的权重。这不是一个编程问题 - 您可能会在 statscs 等其他网站上找到更好的答案。
  • R 没有正负因子变量的概念。这完全由 h2o 包处理,所以你应该问那些人。通常大多数第三方工具都能正确理解 0 和 1。

标签: r classification roc gbm h2o


【解决方案1】:

如果你想直接在 h2o 中声明一个正类,为了有正确的指标(h2o.confusionMatrixh2o.performance 等),你可以使用函数h2o.relevel。例如在您的示例中,您应该在模型训练之前添加:

ddd[max.var] <- h2o.relevel(ddd[max.var],'bottom')

(默认情况下,我相信 h2o 会根据字母顺序决定正类,在您的示例中,h2o 度量函数应该立即起作用)

【讨论】:

    【解决方案2】:

    我认为你想在你的函数中添加 'positive' 参数:

    err.res <- confusionMatrix(pred1, hh$score_class, positive="top")
    

    【讨论】:

      【解决方案3】:

      我建议使用 h2o.confustionMatrix 并使用它来创建不同阈值的矩阵。

      例如。 h2o.confusionMatrix(object = fit, threshold = 0.3)

      谢谢,

      Avni

      【讨论】:

        猜你喜欢
        • 2012-08-29
        • 2011-12-11
        • 2018-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-28
        • 1970-01-01
        相关资源
        最近更新 更多