让 weka 交叉验证 Ruby 中的分类器

发布于 2024-12-14 07:02:07 字数 1642 浏览 0 评论 0原文

此处获取提示!我通过 RJB 从 Ruby 使用 weka 的分类器库。

我希望能够从 .arff 文件创建一个分类器,并用它运行 10 倍交叉验证以生成一个混淆矩阵,如下所述 Weka wiki

下面是涉及的基本代码。

# creating the classifier
Rjb::load("./weka.jar", jvmargs=["-Xmx2000M"])
classifier = Rjb::import("weka.classifiers.bayes.NaiveBayes").new

# importing the data
data_src = Rjb::import("java.io.FileReader").new("./the_data.arff")
data = Rjb::import("weka.core.Instances").new(data_src)

evaluation = Rjb::import("weka.classifiers.Evaluation").new(data)

folds = Rjb::import('java.lang.Integer').new(10)
rand = Rjb::import("java.util.Random").new(1)

evaluation.crossValidateModel(classifier, 
                              data, 
                              folds, 
                              rand )

print evaluation.toMatrixString()

从上面的 weka wiki 链接中我可以看出:这应该可行。 但是...

Fail: unknown method name `crossValidateModel' (RuntimeError)

根据我的理解,这通常意味着所讨论的方法尚未提供正确的参数,但我看不出情况会如何。

evaluation.java_methods 的输出包括 crossValidateModel([Ljava.lang.String;Lweka.core.Instances;I[Ljava.lang.String;Ljava.util.Random;, Lweka.classifiers) .Classifier;Lweka.core.Instances;ILjava.util.Random;[Ljava.lang.Object;])

我不知道如何解释。

有人知道我需要做什么吗?


编辑:虽然我无法解决这里提出的问题,但事实证明,我能够通过按照描述的方式重新使用 JRuby 来实现我想要的目标 此处。感谢 michaeltwofish 的提示:)

Taking a hint from here! I'm utilising weka's library of classifiers from Ruby via RJB.

I want to be able to create a classifier from an .arff file and run 10 fold cross-validation with it to produce a confusion matrix as explained in the Weka wiki.

Below is the essential code involved.

# creating the classifier
Rjb::load("./weka.jar", jvmargs=["-Xmx2000M"])
classifier = Rjb::import("weka.classifiers.bayes.NaiveBayes").new

# importing the data
data_src = Rjb::import("java.io.FileReader").new("./the_data.arff")
data = Rjb::import("weka.core.Instances").new(data_src)

evaluation = Rjb::import("weka.classifiers.Evaluation").new(data)

folds = Rjb::import('java.lang.Integer').new(10)
rand = Rjb::import("java.util.Random").new(1)

evaluation.crossValidateModel(classifier, 
                              data, 
                              folds, 
                              rand )

print evaluation.toMatrixString()

From what I can tell from the weka wiki link above: this should work.
But...

Fail: unknown method name `crossValidateModel' (RuntimeError)

Which from what I understand usually means that the method in question hasn't been supplied with the correct arguments, but I can't see how this would be the case.

The output of evaluation.java_methods includes crossValidateModel([Ljava.lang.String;Lweka.core.Instances;I[Ljava.lang.String;Ljava.util.Random;, Lweka.classifiers.Classifier;Lweka.core.Instances;ILjava.util.Random;[Ljava.lang.Object;])

which I'm not sure how to interpret.

Does anyone out there know what I need to do?


EDIT: although I wasn't able to solve the problem as posed here, it turns out that I was able to achieve what I wanted by starting over with JRuby as described here. Thanks to michaeltwofish for the tip :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒲公英的约定 2024-12-21 07:02:07

而不是使用 Rjb::import('java.lang.Integer').new(10)
尝试使用普通 10。

您正在从评估类调用 crossValidateModel 方法。其中有一个过载。见下文。请注意,第三个参数是 int。您正在使用 java.lang.Integer。在java中int和Integer不是一回事。如果您有兴趣,请查找 java 中的原始类型和包装类型。通常,自 java 5 以来,java 能够在 int 和 Integer 之间进行更改。但是您是从 Rjb 调用的,我认为 java Integer 出于 ruby​​ 目的而包装在某个对象中,这令人困惑。

来自 weka javadocs。

 crossValidateModel(Classifier, Instances, int)

对一组实例上的分类器执行(如果类是名义类,则分层)交叉验证。

crossValidateModel(String, Instances, int, String[])

对一组实例上的分类器执行(如果类是名义类,则分层)交叉验证。

Instead of using Rjb::import('java.lang.Integer').new(10)
try to use plain 10.

You are calling a method crossValidateModel from Evaluation class. Which has a overload. see below. Notice that third parameter is int. You are using java.lang.Integer. In java int and Integer are not the same thing. Look for primitive types and Wrapper types in java if you are interested. Normally java is able to change between int and Integer since java 5. But you are calling from Rjb , I think java Integer is wrapped in some object for ruby purposes and that is confusing things.

From weka javadocs.

 crossValidateModel(Classifier, Instances, int)

Performs a (stratified if class is nominal) cross-validation for a classifier on a set of instances.

crossValidateModel(String, Instances, int, String[])

Performs a (stratified if class is nominal) cross-validation for a classifier on a set of instances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文