r - C5.0 树模型上的高度不平衡数据

标签 r parameters decision-tree random-forest

我有一个不平衡的数据集,在所有 496,978 个 obs 中只有 87 个目标事件“F”,因为我想看到一个规则/树,我选择使用树模型,我一直在遵循“Applied”中的代码Max Kuhn 博士的《R 中的预测建模》一书,第 16 章很好地解决了这个不平衡问题。

这是示例数据结构:

str(training[,predictors])

'data.frame':496978 obs。 36 个变量:

$ Point_Of_Sale_Code:因子 w/5 个水平 "c0","c2","c90",..: 3 3 5 5 3 3 5 5 5 5 ...

$拖欠金额:num 0 0 0 0 0 0 0 0 0 0 ...

$ Delinquent_Days_Count : num 0 0 0 0 0 0 0 0 0 0 ...

$ 超限金额:num 0 0 0 0 0 0 0 0 0 0 ...

我尝试了随机森林的下采样,效果很好,测试数据和混淆矩阵的 auc=0.9997 很好

            Reference
Prediction      N      F
         N 140526      0
         F   1442     24

但是rf并没有给我具体的规则,所以我试了一下书上的代码,一模一样:

library(rpart)

library(e1071)

  initialRpart <- rpart(flag ~ ., data = training,
                  control = rpart.control(cp = 0.0001))
  rpartGrid <- data.frame(.cp = initialRpart$cptable[, "CP"])

  cmat <- list(loss = matrix(c(0, 1, 20, 0), ncol = 2))
  set.seed(1401)

  cartWMod1 <- train(x = training[,predictors],
                     y = training$flag,
                     method = "rpart",
                     trControl = ctrlNoProb,
                     tuneGrid = rpartGrid,
                     metric = "Kappa",
                     parms = cmat)
  cartWMod1

我每次都收到下面的错误消息,无论我尝试了什么,比如将所有 int 数据类型转换为 num 类型,不知道为什么我收到这个警告消息,

  Warning message:
  In ni[1:m] * nj[1:m] : ***NAs produced by integer overflow***

  Aggregating results
  Selecting tuning parameters
  Error in train.default(x = training[, predictors], y = training$flag,  : 
  ***final tuning parameters could not be determined***

我也试过c5.0包的代码:

library(C50)

  c5Grid <- expand.grid(.model = c("tree", "rules"),
                  .trials = c(1, (1:10)*10),
                  .winnow = FALSE)

  finalCost <- matrix(c(0, 150, 1, 0), ncol = 2)
  rownames(finalCost) <- colnames(finalCost) <- levels(training$flag)

set.seed(1401)

      C5CostFit1 <- train(training[,predictors],
               training$flag,
               method = "C5.0",
               metric = "Kappa",
               tuneGrid = c5Grid,
               cost = finalCost,
               control = C5.0Control(earlyStopping = FALSE),
               trControl = ctrlNoProb)

C5CostCM1 <- confusionMatrix(predict(C5CostFit, training), training$flag)

我得到了这个结果,它将所有目标事件 F 分类为非事件 N,我是否可以将成本惩罚从 150 增加到更大来解决这个问题?谢谢!

C5CostCM1

Confusion Matrix and Statistics

           Reference
  Prediction      N      F
           N 141968     ***24***
           F      0      0

           Accuracy : 0.9998          
             95% CI : (0.9997, 0.9999)
No Information Rate : 0.9998          
P-Value [Acc > NIR] : 0.554           
              Kappa : NA            
 Mcnemar's Test P-Value : 2.668e-06                                                 
        Sensitivity : 1.0000          
        Specificity : 0.0000          
     Pos Pred Value : 0.9998          
     Neg Pred Value :    NaN          
         Prevalence : 0.9998          
     Detection Rate : 0.9998          
   Detection Prevalence : 1.0000          
   Balanced Accuracy : 0.5000                                                    
    'Positive' Class : N     

过去一周我一直在用谷歌搜索这个问题,但没有找到解决方案,尽管书中的代码运行良好,但我的数据出现错误...任何建议都会被采纳!!非常感谢!

最佳答案

认为它是在告诉您输出中的某些内容(即列表)中包含 NA——Kappa 统计数据。

使用这样的东西:

results.matrix = confusionMatrix(data, reference)
results.df = as.data.frame(results.matrix[3])
summary(is.finite(results.df$overall))

给你这个:

   Mode   FALSE    TRUE    NA's 
logical       1       6       0 

所以我猜这就是它正在接收的信息。

关于r - C5.0 树模型上的高度不平衡数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24499394/

相关文章:

r - ggplot2 3.1.0 中的自定义 y 轴刻度和辅助 y 轴标签

php - 具有一个或多个(多个)参数的搜索表单

rust - 如何为字段编写具有生命周期约束的函数?

c - 库层次结构中的参数检查

c# - 如何为 Accord.NET DecisionTrees 正确提供输入数据

r - 在R中,如何创建错误对象?以后如何在上面抛出错误?

r - 使用 geom_sf() 绘制 sf 对象,其中包含除经纬度以外的任何投影

r - 在 R Shiny 应用程序 : accessing parent environment for updateTabsetPanel 中将附加参数传递给 moduleServer

machine-learning - 交互式决策树分类器

python - 使用 scikit-learn 时,如何找到我的树 split 的属性?