你如何在 R 中绘制 CostSensitiveClassifier 树? [英] How do you plot a CostSensitiveClassifier tree in R?

查看:17
本文介绍了你如何在 R 中绘制 CostSensitiveClassifier 树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我在成本敏感分类器函数中使用 RWeka 包和 J48.我知道使用party"包我可以绘制一个普通的 J48 树,但不确定如何使用 CSC 输出绘制一个图.

库(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 = 真))服务中心CostSensitiveClassifier 使用最小化的预期错误分类成本weka.classifiers.trees.J48 -C 0.25 -M 2分类器模型J48 修剪过的树------------------Petal.Width <= 0.6: setosa (50.0)花瓣宽度 >0.6|花瓣.宽度 <= 1.7||花瓣长度 <= 4.9:杂色 (48.0/1.0)||花瓣.长度 >4.9|||Petal.Width <= 1.5:维吉尼亚 (3.0)|||花瓣宽度 >1.5:杂色 (3.0/1.0)|花瓣宽度 >1.7:弗吉尼亚(46.0/1.0)叶子数 : 5树的大小:9成本矩阵0 0 010 0 100 0 0情节(csc)

<块引用>

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

任何帮助都会很棒.

dput(csc)结构(列表(分类器 = ,预测=结构(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)),处理程序 = 结构(列表(控制 = 列表(功能 (x){如果(继承(x,Weka_control")){ind <- which(names(x) %in% substring(options,2L))如果(任何(索引))x[ind] <- lapply(x[ind], fun, ...)}别的 {x <- as.character(x)ind <- which(x %in% 选项)如果(任何(索引))x[ind + 1L] <- sapply(x[ind + 1L], fun, ...)}X}, 函数 (x){如果(继承(x,Weka_control")){ind <- which(names(x) %in% substring(options,2L))如果(任何(索引))x[ind] <- lapply(x[ind], fun, ...)}别的 {x <- as.character(x)ind <- which(x %in% 选项)如果(任何(索引))x[ind + 1L] <- sapply(x[ind + 1L], fun, ...)}X}), 数据 = 函数 (mf){条款 <- attr(mf, "条款")if (any(attr(terms, "order") > 1L))stop("不允许交互.")因素 <- 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"), 术语 = 物种 ~ Sepal.Length +Sepal.Width + Petal.Length + Petal.Width), .Names = c("classifier",预测",调用",处理程序",级别",术语"),class = c(CostSensitiveClassifier","Weka_meta", "Weka_classifier"))

解决方案

实际上,这很容易.试试

库(RWeka)图书馆(派对)图书馆(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 = 真))情节(as.party.Weka_tree(csc))

这给了我

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

>班级(csc)[1] "CostSensitiveClassifier" "Weka_meta" "Weka_classifier"

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

In this case I'm using the RWeka package and J48 within the Cost Sensitive Classifier function. I know with the package "party" I can plot a normal J48 tree, but not sure how to get a plot with the CSC output.

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)

Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y'

Any help would be great.

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"))

解决方案

Actually, it turns out to be pretty easy. Try

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

That gives me

The problem is, this model reports it's class as

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

and there is no method for those classes. However, there is one for "Weka_tree" which just calls as.party.Weka_tree and plots the result. I must admit I don't know the differences between a CostSensitiveClassifier tree and a J48 tree so I hope this plot is an accurate representation.

这篇关于你如何在 R 中绘制 CostSensitiveClassifier 树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆