简体   繁体   中英

Accessing individual results from decision tree function JRip (RWeka library)

I am using library(RWeka) and running the JRip function on a data set. Does anybody know of a way of accessing the rules result set programmatically so I can access each rule individually?

Here is an example for illustrative purposes only:

> library(datasets)
> head(npk)
block N P K yield
1     1 0 1 1  49.5
2     1 1 1 0  62.8
3     1 0 0 0  46.8
4     1 1 0 1  57.0
5     2 1 0 0  59.8
6     2 1 1 1  58.5
> tree_rip <- JRip(block ~ ., data = npk)
> tree_rip
JRIP rules:
===========

(yield <= 48.8) => block=4 (5.0/2.0)
(yield <= 52) => block=5 (4.0/1.0)
=> block=3 (15.0/11.0)

Number of Rules : 3

I would like to access the results in a dataframe/table fashion. The closest is retrieving a single blob string in the following manner:

> tree_rip$classifier
[1] "Java-Object{JRIP rules:\n===========\n\n(yield <= 48.8) => block=4 (5.0/2.0)\n(yield <= 52) => block=5 (4.0/1.0)\n => block=3 (15.0/11.0)\n\nNumber of Rules : 3\n}"

I need something that would allow me to get each result separately, just as it is printed when I call tree_rip , so I can not only get the length of rules found, but access them one by one.

At the very least something like this (but ideally accessing each result variable separately for every row):

[1] (yield <= 48.8) => block=4 (5.0/2.0)
[2] (yield <= 52) => block=5 (4.0/1.0)
...

thanks!

This proved surprisingly difficult for me, being a non-user of R's integration with Java. At any rate after looking at these results in an effort to find out how the REPL had produced the results you were seeing:

str(tree_rip)
# omitting about 15 lines of output
# - attr(*, "class")= chr [1:3] "JRip" "Weka_rules" "Weka_classifier"

getAnywhere(print.JRIP)
# no object named ‘print.JRIP’ was found
getAnywhere(print.Weka_rules)
# no object named ‘print.Weka_rules’ was found
help(pack="RWeka")
getAnywhere(print.Weka_classifier)
# this did succeed ... so I though `.jcall` should also succeed

.jcall(tree_rip$classifier, "S", "toString")
 #    Error: could not find function ".jcall"
 RWeka:::.jcall(tree_rip$classifier, "S", "toString")
 #    Error in get(name, envir = asNamespace(pkg), inherits = FALSE) : 
 #      object '.jcall' not found

... I discovered that one needs to load pkg:rJava in order to get access to the .jcall function . Apparently this is one of those situations where the supporting library is not loaded but only attached. (Analogous to assuming [incorrectly] that grid.text should be available when only pkg:lattice is loaded. ) So this gives you the desired set of strings:

library(rJava)
as.matrix(scan(text=.jcall(tree_rip$classifier, "S", "toString") ,sep="\n", what="") )[
                                                                       -c(1:2, 6), ,drop=FALSE]
#------------
     [,1]                                  
[1,] "(yield <= 48.8) => block=4 (5.0/2.0)"
[2,] "(yield <= 52) => block=5 (4.0/1.0)"  
[3,] " => block=3 (15.0/11.0)" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM