R C50代码称为退出,值为1(使用因子决策变量a非空值)
我读到一个类似于此问题的错误代码,但我担心此错误代码是由于其他原因造成的。我有一个CSV文件,包含8个观察值和10个变量:R C50代码称为退出,值为1(使用因子决策变量a非空值),r,machine-learning,decision-tree,R,Machine Learning,Decision Tree,我读到一个类似于此问题的错误代码,但我担心此错误代码是由于其他原因造成的。我有一个CSV文件,包含8个观察值和10个变量: > 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
> 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)
我现正跟进其他建议,例如:
-使用因子作为决策变量
-避免空数据
这方面有什么帮助吗?我读到这是机器学习的最佳算法之一,但我一直都会遇到这个错误
以下是原始数据集:
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
提前感谢您的帮助
David您需要用几种方法清理数据
- 删除仅具有一个级别的不必要列。它们不包含任何信息并导致问题
- 将目标变量
的类转换为因子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
在二元分类问题中,通常的方法是建立混淆矩阵来比较实际值和预测值。在这个小数据集的情况下,我们可以很容易地看到只有一个假阴性结果。因此,代码似乎工作得很好,但由于观察的数量非常少,这个令人鼓舞的结果可能是欺骗性的
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 | |
#--------------|-----------|-----------|-----------|
库(gmodels)
你的数据不是太小了吗?你的变量甚至比观察值还要多,这可能是一个问题。p>n是一个问题,但即使这样,数据也相对较小。通常不建议尝试用很少的观察值创建一个健壮的模型。RHertel,感谢您的详细回复。它在修复1级问题后工作。我更喜欢通过以下方式调用算法:fit@DavidLeal不客气!我很高兴听到它现在起作用了——而且知道你有大量的观察结果让我感到放心。关于不同的语法,我想这属于“剥猫皮的不止一种方法”的范畴我不知道C5.0的公式
语法,但它确实有意义。如果答案有助于解决你的问题,请考虑点击左边的滴答声。干杯
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 | |
#--------------|-----------|-----------|-----------|