2016-07-22 104 views
1

我剛剛開始學習使用R編碼,我嘗試通過C5.0進行分類。但我遇到一些問題,我不明白。我正在尋求感謝的幫助。下面是我從別人學到的代碼,我試圖用它來運行自己的數據:錯誤if(!n.cat [i]){:參數的長度爲零

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) 

} 

初級講座是兩個對象needdata & testdata我在上面的代碼分別用他們的頭的一部分:

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],試驗= 5,...: '試驗' 應該是< = 1 。此對象預測產生使用1個試驗

,當我嘗試一下剛剛創建的對象trainc50,我注意到提高迭代的次數爲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 

我也試過t Ø繪製決策樹,我得到了如下錯誤:

plot(trainc50) 

錯誤,如果{(n.cat [I]!):參數爲長度爲零 的另外:警告消息: 在1:其(下==「決策樹:」):數值表達式有2個要素:僅用於第一

是否意味着我的代碼是太糟糕執行而運行C5.0進一步的試驗?哪裏不對?有人能幫我解決一下爲什麼我會遇到提前停車,錯誤和戰鬥信息是什麼意思?我該如何解決它?如果有人能幫助我,我會非常感激。

+0

而且我甚至不知道該怎麼做,並「在1:哪個(out ==「決策樹:」)「的意思是...... – Wang

回答

0

用於 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) 

,並在命令行:(!n.cat [1])

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