简体   繁体   English

我应该如何绘制从RWeka J48()函数运行的多分割(非二进制分割)决策树?

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

I try to build a multi-split tree with the option of J48() function: 我尝试使用J48()函数的选项构建一个多叉树:

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

and it seems to work fine. 而且似乎工作正常。 But I cannot plot the decision tree by any of the following codes: 但是我无法通过以下任何代码绘制决策树:

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

by using the plot tool in the partykit package. 通过使用partykit软件包中的绘图工具。 The manual says it only plots the binary-split tree. 该手册说,它仅绘制二进制分割树。 Or the code: 或代码:

prp(mytree)

by using the plot function in the rpart.plot package. 通过使用rpart.plot包中的plot函数。 Because R shows the warning message 因为R显示警告消息

Error in prp(mytree) : Not an rpart object

Can anyone tell me how to plot this tree? 谁能告诉我如何绘制这棵树?

The data I use is the heart disease data from UCI repository, and it looks like > 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 The class variable is the variable num (0 or 1). 我使用的数据是来自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)。 And the result of typing dput(mytree) is 并且输入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"
))

Thanks a lot. 非常感谢。

After re-running the process, the code 重新运行该过程后,代码

 prp(mytree)

doesn't work with the warning message 不适用于警告消息

 Error in prp(mytree) : Not an rpart object

However, the following code works 但是,以下代码有效

 plot(as.party.Weka_tree(mytree))

referring How do you plot a CostSensitiveClassifier tree in R? 参考如何在R中绘制CostSensitiveClassifier树?

Is there anyone who knows the reason? 有谁知道原因吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM