【问题标题】:Error in if (!n.cat[i]) { : argument is of length zeroif (!n.cat[i]) { 中的错误:参数长度为零
【发布时间】:2016-11-26 14:45:59
【问题描述】:

我刚开始学习使用 R 编码,并尝试通过 C5.0 进行分类。但是我遇到了一些问题,我不明白。我怀着感激之情寻求帮助。下面是我从某人那里学到的代码,我试图用它来运行我自己的数据:

require(C50)

data.resultc50 <- c()

prematrixc50 <- c()

for(i in 3863:3993)

{

needdata$class <- as.factor(needdata$class)

trainc50 <- C5.0(class ~ ., needdata[1:3612,], trials=5, control=C5.0Control(noGlobalPruning = TRUE, CF = 0.25))

predc50 <- predict(trainc50, newdata=testdata[i, -1], trials=5, type="class")

data.resultc50[i-3862] <- sum(predc50==testdata$class[i])/length(predc50)

prematrixc50[i-3862] <- as.character.factor(predc50)

}

下面是我在上面代码中使用的两个对象needdatatestdata,分别是它们的部分头部:

    class Volume  MA20     MA10    MA120     MA40      MA340     MA24  BIAS10
1     1   2800    8032.00  8190.9  7801.867  7902.325  7367.976  1751   7.96
2     1   2854    8071.40  8290.3  7812.225  7936.550  7373.624  1766   6.27
3     0   2501    8117.45  8389.3  7824.350  7973.250  7379.444  1811   5.49
4     1   2409    8165.40  8488.1  7835.600  8007.900  7385.294  1825   4.02

# the above is "needdata" and actually has 15 variables with 3862 obs.

   class   Volume   MA20    MA10    MA120     MA40    MA340  MA24  BIAS10
1     1    2800     8032.00 8190.9 7801.867 7902.325 7367.976 1751   7.96
2     1    2854     8071.40 8290.3 7812.225 7936.550 7373.624 1766   6.27
3     0    2501     8117.45 8389.3 7824.350 7973.250 7379.444 1811   5.49
4     1    2409     8165.40 8488.1 7835.600 8007.900 7385.294 1825   4.02

# the above is "testdata" and has 15 variables with 4112 obs.

以上数据包含因子class,值为0 & 1。运行后,我收到以下警告:

在 predict.C5.0(trainc50, newdata = testdata[i, -1], trial = 5, ... : 对于这个对象,'trials' 应该

当我尝试查看刚刚创建的对象trainc50 时,我注意到由于提前停止,boosting 迭代的次数为 1,如下所示:

# trainc50
Call:
C5.0.formula(formula = class ~ ., data = needdata[1:3612, ], 
             trials  = 5, control = C5.0Control(noGlobalPruning = TRUE, 
             CF = 0.25), earlyStopping = FALSE)

Classification Tree
Number of samples: 3612
Number of predictors: 15
Number of boosting iterations: 5 requested;  1 used due to early stopping
Non-standard options: attempt to group attributes, no global pruning

我也尝试绘制决策树,但出现如下错误:

plot(trainc50)

if (!n.cat[i]) { 中的错误:参数长度为零 另外:警告信息: In 1:which(out == "Decision tree:") :数值表达式有2个元素:只使用第一个

这是否意味着我的代码太糟糕了,无法在运行 C5.0 时进行进一步的试验?怎么了?有人可以帮助我了解为什么我会遇到提前停止以及错误和警告消息是什么意思吗?我该如何解决?如果有人可以帮助我,我将非常感激。

【问题讨论】:

  • 而且我什至不知道 (!n.cat[i]) 和 "In 1:which(out == "Decision tree:")" 是什么意思...

标签: r


【解决方案1】:

用于 http://r-project-thanos.blogspot.tw/2014/09/plot-c50-decision-trees-in-r.html

using function 
C5.0.graphviz(firandomf,
              "a.txt",
              fontname='Arial',
              col.draw='black',
              col.font='blue',
              col.conclusion='lightpink',
              col.question='grey78',
              shape.conclusion='box3d',
              shape.question='diamond',
              bool.substitute=c('None', 'yesno', 'truefalse', 'TF'), 
              prefix=FALSE,
              vertical=TRUE)

在命令行中:

pip install graphviz 
dot -Tpng ~plot/a.txt >~/plot/a.png 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多