如何获得J48的大小和叶子数 [英] How to get J48 size and number of leaves
问题描述
如果我通过以下方式构建 J48 树:
If I build a J48 tree by:
library(RWeka)
fit <- J48(Species~., data=iris)
我得到以下结果:
> 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 the tree : 9
我想将 Number of Leaves
放入变量 N
中(所以 N
将得到 5
)和树的大小
到S
(所以S
会得到9
).
I would like to get the Number of Leaves
into a variable N
(so N
will get 5
) and the Size of the tree
to S
(so S
will get 9
).
有没有办法直接从 J48 树中获取这些信息?
Is there a way to get this information directly from J48 tree?
推荐答案
正如@LyzandeR 之前指出的那样,直接在 J48
对象上执行此操作并不容易.通常,RWeka
中拟合函数返回的对象通常包含相对较少的 R 端信息(例如,只有调用和拟合预测).主要成分通常是对 Weka 构建的 Java 对象的引用,Weka 自己的方法可以通过 .jcall
应用到 Java 端,然后在 R 中返回.
As previously pointed out by @LyzandeR it is not easy to do this on the J48
object directly. Generally, the objects returned by the fitting functions in RWeka
usually contain relatively few informations on the R side (e.g., only the call and the fitted predictions). The main ingredient is typically a reference to Java object built by Weka to which Weka's own methods can be applied on the Java side via .jcall
and then returned in R.
然而,对于 J48
树,很容易将来自 Java 端的信息转换为 R 对象,该对象的标准函数和方法可用.partykit
包提供了一个强制函数,可以将 J48
树转换为 constparty
对象(在叶子中具有常量拟合的递归分区).然后可以使用length()
、width()
或depth()
等方法来查询节点数、叶子数和分别为树的深度.
However, for the J48
trees it is easy to transform the information from the Java side into an R object for which standard functions and methods are available. The partykit
package provides a coercion function that transforms J48
trees into constparty
objects (recursive partitions with constant fits in the leaves). Then methods like length()
, width()
, or depth()
can be used to query the number of nodes, leaves, and the depth of the 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
此外,predict()
、plot()
、print()
和许多其他工具可用于 party
对象.
Furthermore, predict()
, plot()
, print()
and many other tools are available for the party
object.
我建议在@LyzandeR 建议的文本解析上使用这种方法,因为 as.party
转换不依赖于潜在的容易出错的文本计算.相反,它在内部调用 Weka 自己的 graph
生成器(通过 .jcall
),然后将其解析为 constparty
结构.
I would recommend using this approach over the text parsing suggested by @LyzandeR because the as.party
conversion does not rely on potentially error-prone text computations. Instead, it internally calls Weka's own graph
generator (via .jcall
) and then parses this into the constparty
structure.
这篇关于如何获得J48的大小和叶子数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!