如何在R中绘制CostSensitiveClassifier树?

时间:2014-06-25 23:23:22

标签: r plot tree weka

在这种情况下,我在成本敏感性分类器功能中使用RWeka包和J48。我知道包裹" party"我可以绘制一个普通的J48树,但不知道如何用CSC输出得到一个图。

library(RWeka)

csc <- CostSensitiveClassifier(Species ~ ., data = iris, 
control = Weka_control(`cost-matrix` = matrix(c(0,10, 0, 0, 0, 0, 0, 10, 0), 
ncol = 3), 
W = "weka.classifiers.trees.J48", 
M = TRUE))

csc
CostSensitiveClassifier using minimized expected misclasification cost

weka.classifiers.trees.J48 -C 0.25 -M 2

Classifier Model
J48 pruned tree
------------------

Petal.Width <= 0.6: setosa (50.0)
Petal.Width > 0.6
|   Petal.Width <= 1.7
|   |   Petal.Length <= 4.9: versicolor (48.0/1.0)
|   |   Petal.Length > 4.9
|   |   |   Petal.Width <= 1.5: virginica (3.0)
|   |   |   Petal.Width > 1.5: versicolor (3.0/1.0)
|   Petal.Width > 1.7: virginica (46.0/1.0)

Number of Leaves  :     5

Size of the tree :  9


Cost Matrix
  0  0  0
 10  0 10
  0  0  0
plot(csc)
  

xy.coords(x,y,xlabel,ylabel,log)中的错误:     &#39; X&#39;是一个列表,但没有组件&#39; x&#39;和&#39;

任何帮助都会很棒。

dput(csc)

structure(list(classifier = <S4 object of class structure("jobjRef", package = "rJava")>, 
    predictions = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("setosa", "versicolor", 
    "virginica"), class = "factor"), call = CostSensitiveClassifier(formula = Species ~ 
        ., data = iris, control = Weka_control(`cost-matrix` = matrix(c(0, 
        10, 0, 0, 0, 0, 0, 10, 0), ncol = 3), W = "weka.classifiers.trees.J48", 
        M = TRUE)), handlers = structure(list(control = list(
        function (x) 
        {
            if (inherits(x, "Weka_control")) {
                ind <- which(names(x) %in% substring(options, 
                  2L))
                if (any(ind)) 
                  x[ind] <- lapply(x[ind], fun, ...)
            }
            else {
                x <- as.character(x)
                ind <- which(x %in% options)
                if (any(ind)) 
                  x[ind + 1L] <- sapply(x[ind + 1L], fun, ...)
            }
            x
        }, function (x) 
        {
            if (inherits(x, "Weka_control")) {
                ind <- which(names(x) %in% substring(options, 
                  2L))
                if (any(ind)) 
                  x[ind] <- lapply(x[ind], fun, ...)
            }
            else {
                x <- as.character(x)
                ind <- which(x %in% options)
                if (any(ind)) 
                  x[ind + 1L] <- sapply(x[ind + 1L], fun, ...)
            }
            x
        }), 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]
    }), .Names = c("control", "data")), levels = c("setosa", 
    "versicolor", "virginica"), terms = Species ~ Sepal.Length + 
        Sepal.Width + Petal.Length + Petal.Width), .Names = c("classifier", 
"predictions", "call", "handlers", "levels", "terms"), class = c("CostSensitiveClassifier", 
"Weka_meta", "Weka_classifier"))

1 个答案:

答案 0 :(得分:2)

实际上,结果很简单。尝试

library(RWeka)
library(party)
library(partykit)


csc <- CostSensitiveClassifier(Species ~ ., data = iris, 
control = Weka_control(`cost-matrix` = matrix(c(0,10, 0, 0, 0, 0, 0, 10, 0), 
ncol = 3), 
W = "weka.classifiers.trees.J48", 
M = TRUE))

plot(as.party.Weka_tree(csc))

那给了我

CostSensitiveClassifier plot

问题是,这个模型将它的类报告为

> class(csc)
[1] "CostSensitiveClassifier" "Weka_meta"     "Weka_classifier"  

并且这些类没有方法。但是,有一个“Weka_tree”只调用as.party.Weka_tree并绘制结果。我必须承认我不知道CostSensitiveClassifier树和J48树之间的区别,所以我希望这个图是一个准确的表示。