r - How to get J48 size and number of leaves -


if build j48 tree by:

library(rweka)  fit <- j48(species~., data=iris) 

i following result:

> fit 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 tree :  9 

i number of leaves variable n (so n 5) , size of tree s (so s 9).

is there way information directly j48 tree?

as pointed out @lyzander not easy on j48 object directly. generally, objects returned fitting functions in rweka contain relatively few informations on r side (e.g., call , fitted predictions). main ingredient typically reference java object built weka weka's own methods can applied on java side via .jcall , returned in r.

however, j48 trees easy transform information java side r object standard functions , methods available. partykit package provides coercion function transforms j48 trees constparty objects (recursive partitions constant fits in leaves). methods length(), width(), or depth() can used query number of nodes, leaves, , depth of tree, respectively.

library("rweka") fit <- j48(species ~ ., data = iris) library("partykit") p <- as.party(fit) length(p) ## [1] 9 width(p) ## [1] 5 depth(p) ## [1] 4 

furthermore, predict(), plot(), print() , many other tools available party object.

i recommend using approach on text parsing suggested @lyzander because as.party conversion not rely on potentially error-prone text computations. instead, internally calls weka's own graph generator (via .jcall) , parses constparty structure.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -