2014-06-25 36 views
2

在這種情況下,我在成本敏感分類器功能中使用RWeka軟件包和J48。我知道用包裝「派對」我可以繪製一個普通的J48樹,但不知道如何獲得CSC輸出的情節。如何繪製R中的CostSensitiveClassifier樹?

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,登錄): 'X' 是一個列表,但沒有組件 'X' 和 'Y'

任何幫助將是偉大的。

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

如果不是太大,你還可以發佈'dput(csc)'嗎? – MrFlick

回答

1

事實上,事實證明這很容易。嘗試

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樹之間的區別,所以我希望這個圖是一個準確的表示。

+0

這是完美的!謝謝! 它是關於R的其中一件事,如果你不知道它,那麼找出答案可能是非常不直觀的。 – user3712632

+0

我以前從未使用過這些軟件包,但是您在問題中提供了足夠的有用信息,並且包含了一個可重複使用的示例,它總能讓問題更容易解決。一旦你使用R足夠的一切開始看起來一樣。有些軟件包比其他軟件包有更好的文檔。 – MrFlick