0

我尝试使用 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 包中的绘图功能。因为 R 显示警告信息

Error in prp(mytree) : Not an rpart object

谁能告诉我如何绘制这棵树?

4

2 回答 2

0

我使用的数据是来自 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 类变量是变量 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"
))

非常感谢。

于 2018-08-27T04:00:24.377 回答
0

重新运行该过程后,代码

 prp(mytree)

不适用于警告消息

 Error in prp(mytree) : Not an rpart object

但是,以下代码有效

 plot(as.party.Weka_tree(mytree))

参考 您如何在 R 中绘制 CostSensitiveClassifier 树?

有谁知道原因吗?

于 2018-08-30T02:44:24.500 回答