簡體   English   中英

我應該如何繪制從RWeka J48()函數運行的多分割(非二進制分割)決策樹?

[英]How should I plot a multi-split (non binary split) decision tree run from RWeka J48() function?

我嘗試使用J48()函數的選項構建一個多叉樹:

mytree = J48(class ~ ., data=train.set, na.action = NULL, control= Weka_control(U=TRUE, B=FALSE))

而且似乎工作正常。 但是我無法通過以下任何代碼繪制決策樹:

if(require("partykit", quietly = TRUE)) plot(mytree)

通過使用partykit軟件包中的繪圖工具。 該手冊說,它僅繪制二進制分割樹。 或代碼:

prp(mytree)

通過使用rpart.plot包中的plot函數。 因為R顯示警告消息

Error in prp(mytree) : Not an rpart object

誰能告訴我如何繪制這棵樹?

我使用的數據是來自UCI存儲庫的心臟病數據,它看起來像> head(Hungarian) age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal num 1 28 1 2 130 132 0 2 185 0 0 <NA> NA <NA> 0 2 29 1 2 120 243 0 0 160 0 0 <NA> NA <NA> 0 3 29 1 2 140 NA 0 0 170 0 0 <NA> NA <NA> 0 4 30 0 1 170 237 0 1 170 0 0 <NA> NA 6 0 5 31 0 2 100 219 0 1 150 0 0 <NA> NA <NA> 0 6 32 0 2 105 198 0 0 165 0 0 <NA> NA <NA> 0類variable是變量num(0或1)。 並且輸入dput(mytree)的結果是

structure(list(classifier = new("jobjRef", jobj = <pointer: 0x0de0e848>, 
jclass = "java/lang/Object"), predictions = structure(c(1L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 
1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 
2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 
1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L), .Label = c("0", 
"1"), class = "factor"), call = J48(formula = num ~ ., data = train.set, 
    na.action = NULL, control = Weka_control(U = TRUE, B = FALSE)), 
    handlers = list(data = function (mf) 
    {
        terms <- attr(mf, "terms")
        if (any(attr(terms, "order") > 1L)) 
            stop("Interactions are not allowed.")
        factors <- attr(terms, "factors")
        varnms <- rownames(factors)[c(TRUE, rowSums(factors)[-1L] > 
            0)]
        mf[, sub("^`(.*)`$", "\\1", varnms), drop = FALSE]
    }), levels = c("0", "1"), terms = num ~ age + sex + cp + 
        trestbps + chol + fbs + restecg + thalach + exang + oldpeak + 
        slope + ca + thal), class = c("J48", "Weka_tree", "Weka_classifier"
))

非常感謝。

重新運行該過程后,代碼

 prp(mytree)

不適用於警告消息

 Error in prp(mytree) : Not an rpart object

但是,以下代碼有效

 plot(as.party.Weka_tree(mytree))

參考如何在R中繪制CostSensitiveClassifier樹?

有誰知道原因嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM