从决策树函数JRip(RWeka库)访问单个结果
我正在使用库(RWeka)并在数据集上运行JRip函数。有人知道如何通过编程方式访问规则结果集,以便我可以单独访问每个规则吗 以下是仅用于说明目的的示例:从决策树函数JRip(RWeka库)访问单个结果,r,R,我正在使用库(RWeka)并在数据集上运行JRip函数。有人知道如何通过编程方式访问规则结果集,以便我可以单独访问每个规则吗 以下是仅用于说明目的的示例: > 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 5
> 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
谢谢 这对我来说非常困难,因为我不是R与Java集成的用户。无论如何,在查看这些结果以了解REPL如何产生您所看到的结果后:
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
。。。我发现需要加载pkg:rJava才能访问.jcall函数
。显然,这是支持库未加载而仅附加的情况之一。(类似于[错误地]假设只有pkg:lattice加载时,grid.text
才可用。)因此,这将为您提供所需的字符串集:
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)"
库(rJava)
as.matrix(扫描(text=.jcall(tree_rip$分类器,“S”,“toString”),sep=“\n”,what=“”)[
-c(1:2,6),drop=FALSE]
#------------
[,1]
[1,](屈服块=4(5.0/2.0)
[2,](屈服块=5(4.0/1.0)
[3,]“=>block=3(15.0/11.0)”
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
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)"