r - C50 代码调用 exit,值为 1(使用因子决策变量 a 非空值)

标签 r machine-learning decision-tree

我读过类似的post related到这个问题,但我担心这个错误代码是由于其他原因造成的。我有一个包含 8 个观察值和 10 个变量的 CSV 文件:

 > str(rorIn)

'data.frame':   8 obs. of  10 variables:
 $ Acuity             : Factor w/ 3 levels "Elective  ","Emergency ",..: 1 1 2 2 1 2 2 3
 $ AgeInYears         : int  49 56 77 65 51 79 67 63
 $ IsPriority         : int  0 0 1 0 0 1 0 1
 $ AuthorizationStatus: Factor w/ 1 level "APPROVED  ": 1 1 1 1 1 1 1 1
 $ iscasemanagement   : Factor w/ 2 levels "N","Y": 1 1 2 1 1 2 2 2
 $ iseligible         : Factor w/ 1 level "Y": 1 1 1 1 1 1 1 1
 $ referralservicecode: Factor w/ 4 levels "12345","278",..: 4 1 3 1 1 2 3 1
 $ IsHighlight        : Factor w/ 1 level "N": 1 1 1 1 1 1 1 1
 $ RealLengthOfStay   : int  25 1 1 1 2 2 1 3
 $ Readmit            : Factor w/ 2 levels "0","1": 2 1 2 1 2 1 2 1

我像这样调用算法:

library("C50")
rorIn <- read.csv(file = "RoRdataInputData_v1.6.csv", header = TRUE, quote = "\"")
rorIn$Readmit <- factor(rorIn$Readmit)
fit <- C5.0(Readmit~., data= rorIn)

然后我得到:

> source("~/R-workspace/src/RoR/RoR/testing.R")
c50 code called exit with value 1
> 

我正在遵循其他建议,例如: - 使用因子作为决策变量 - 避免空数据

对此有什么帮助吗?我读到这是机器学习的最佳算法之一,但我总是收到此错误。

这是原始数据集:

Acuity,AgeInYears,IsPriority,AuthorizationStatus,iscasemanagement,iseligible,referralservicecode,IsHighlight,RealLengthOfStay,Readmit
Elective  ,49,0,APPROVED  ,N,Y,SNF            ,N,25,1
Elective  ,56,0,APPROVED  ,N,Y,12345,N,1,0
Emergency ,77,1,APPROVED  ,Y,Y,OBSERVE        ,N,1,1
Emergency ,65,0,APPROVED  ,N,Y,12345,N,1,0
Elective  ,51,0,APPROVED  ,N,Y,12345,N,2,1
Emergency ,79,1,APPROVED  ,Y,Y,278,N,2,0
Emergency ,67,0,APPROVED  ,Y,Y,OBSERVE        ,N,1,1
Urgent    ,63,1,APPROVED  ,Y,Y,12345,N,3,0

预先感谢您的帮助,

大卫

最佳答案

您需要通过几种方式清理数据。

  • 删除仅一级的不必要列。它们不包含任何信息并会导致问题。
  • 将目标变量rorIn$Readmit的类转换为因子。
  • 将目标变量与您为训练提供的数据集分开。

这应该有效:

rorIn <- read.csv("RoRdataInputData_v1.6.csv", header=TRUE) 
rorIn$Readmit <- as.factor(rorIn$Readmit)
library(Hmisc)
singleLevelVars <- names(rorIn)[contents(rorIn)$contents$Levels == 1]
trainvars <- setdiff(colnames(rorIn), c("Readmit", singleLevelVars))
library(C50)
RoRmodel <- C5.0(rorIn[,trainvars], rorIn$Readmit,trials = 10)
predict(RoRmodel, rorIn[,trainvars])
#[1] 1 0 1 0 0 0 1 0
#Levels: 0 1

然后,您可以通过将此预测结果与目标变量的实际值进行比较来评估准确性、召回率和其他统计数据:

rorIn$Readmit
#[1] 1 0 1 0 1 0 1 0
#Levels: 0 1

通常的方法是建立一个混淆矩阵来比较二元分类问题中的实际值和预测值。在这个小数据集的情况下,人们可以很容易地看到只有一个假阴性结果。因此,代码似乎运行得很好,但由于观察数量非常少,这个令人鼓舞的结果可能具有欺骗性。

library(gmodels)
actual <- rorIn$Readmit
predicted <- predict(RoRmodel,rorIn[,trainvars])     
CrossTable(actual,predicted, prop.chisq=FALSE,prop.r=FALSE)
# Total Observations in Table:  8  
#
# 
#              | predicted 
#       actual |         0 |         1 | Row Total | 
#--------------|-----------|-----------|-----------|
#            0 |         4 |         0 |         4 | 
#              |     0.800 |     0.000 |           | 
#              |     0.500 |     0.000 |           | 
#--------------|-----------|-----------|-----------|
#            1 |         1 |         3 |         4 | 
#              |     0.200 |     1.000 |           | 
#              |     0.125 |     0.375 |           | 
#--------------|-----------|-----------|-----------|
# Column Total |         5 |         3 |         8 | 
#              |     0.625 |     0.375 |           | 
#--------------|-----------|-----------|-----------|

在较大的数据集上,如果没有必要,将数据集分为训练数据和测试数据会很有用。有很多关于机器学习的优秀文献可以帮助您微调模型及其预测。

关于r - C50 代码调用 exit,值为 1(使用因子决策变量 a 非空值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37219697/

相关文章:

R中的子集回归

python - 如何为 scikit-learn 分类器获取信息量最大的特征?

python - 如何使用tensorflow函数tf.contrib.legacy_seq2seq.sequence_loss_by_example的 'weights'参数?

python - 决策树 split

python - 如何消除 sklearn 上决策树的随机性?

r - 如何使用igraph或tnet在R中创建双向网络

r - 如何使用标识列将数据从 R 追加到 Oracle 数据库表

python - 使用哪些特征进行回归或分类?

machine-learning - 将 Adaboost(自适应提升)方法与决策树结合使用的示例是什么

r - 在 R 中使用 rpart 函数时,在 ExtractVars 中获取无效的模型公式