如何在我的 Java 代码中将 LibSVM 与 Weka 结合使用?

发布于 2024-10-20 11:51:37 字数 66 浏览 2 评论 0原文

我想在我的应用程序中将 LibSVM 分类器与 Weka 一起使用。我该如何(或者在哪里可以找到好的例子)做到这一点?

I want to use LibSVM classifier with Weka in my application. How can I (or where can I find good examples to) do this?

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

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

发布评论

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

评论(5

骄傲 2024-10-27 11:51:38

请点击此链接以结合 Weka 和 libsvm
http://www.cs.iastate.edu/~yasser/wlsvm/

weka 擅长计算 ROC、召回率等......
libsvm 适用于分类、回归等...

follow this link for combining Weka and libsvm
http://www.cs.iastate.edu/~yasser/wlsvm/

weka is good calculating ROC,recall,etc....
and libsvm is good for classification, regression,etc...

挽清梦 2024-10-27 11:51:37

当然现在有点晚了,但无论如何我都会回答。您必须在项目中使用 weka.jar、libsvm.jar 和 wlsvm.jar(libsvm 包装器)。因此,只需将所有 3 个 jar 包含在您的构建路径或类路径或其他路径中即可。

您可以从此处获取 wlsvm.jar:http://ailab.ist.psu。 edu/yasser/wlsvm.html

您可以从这里获取 weka:http ://www.cs.waikato.ac.nz/ml/weka/

你可以从这里获取libsvm:http://www.csie.ntu.edu.tw/~cjlin/libsvm/

我无法让它与 weka 3.7.7 或 3.7.8 一起使用,但我能够让它与 3.6.8(今天最新的稳定版本)一起工作。

另外,因为我必须从 svnlib 获取 .class 文件,并将这些文件包含在我的项目的构建路径中。要构建 .class 文件,请使用 SVNLib/java 中的 make 文件。

这里有一小段代码可以帮助您入门:

        DataSource source = new DataSource(new File("mycsvinputfile"));
        System.out.println(source.getStructure());
        Instances data = source.getDataSet();

        // setting class attribute if the data format does not provide this information
        // For example, the XRFF format saves the class attribute information as well
        if (data.classIndex() == -1)
            data.setClassIndex(data.numAttributes() - 1);

        //initialize svm classifier
        LibSVM svm = new LibSVM();
        svm.buildClassifier(data);

祝您好运。

A little late now, surely, but I'll answer anyways. You have to use weka.jar, libsvm.jar, and wlsvm.jar (the libsvm wrapper) in your project. So just include all 3 jars in your build path or class path or whatever.

You can get the wlsvm.jar from here: http://ailab.ist.psu.edu/yasser/wlsvm.html

You can get weka from here: http://www.cs.waikato.ac.nz/ml/weka/

And you can get libsvm from here: http://www.csie.ntu.edu.tw/~cjlin/libsvm/

I could not get this to work with weka 3.7.7 or 3.7.8, but I was able to get it working with 3.6.8 (latest stable version as of today).

Also, as I had to get the .class files from the svnlib and also include those in the build path to my project. To build the .class files, use the make file in the SVNLib/java.

Here is a small piece of code to get you started:

        DataSource source = new DataSource(new File("mycsvinputfile"));
        System.out.println(source.getStructure());
        Instances data = source.getDataSet();

        // setting class attribute if the data format does not provide this information
        // For example, the XRFF format saves the class attribute information as well
        if (data.classIndex() == -1)
            data.setClassIndex(data.numAttributes() - 1);

        //initialize svm classifier
        LibSVM svm = new LibSVM();
        svm.buildClassifier(data);

Good luck.

痴者 2024-10-27 11:51:37

在新版本中,您只需要 weka.jar 并像这样调用 svm,

WekaPackageManager.loadPackages( false, true, false );
AbstractClassifier classifier = ( AbstractClassifier ) Class.forName(
            "weka.classifiers.functions.LibSVM" ).newInstance();

如果您喜欢提供选项,请像这样设置选项

String options = ( "-S 0 -K 0 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.001 -P 0.1" );
String[] optionsArray = options.split( " " );
    classifier.setOptions( optionsArray );

最后训练分类器

classifier.buildClassifier( train );

With new version you just need the weka.jar and call svm like this,

WekaPackageManager.loadPackages( false, true, false );
AbstractClassifier classifier = ( AbstractClassifier ) Class.forName(
            "weka.classifiers.functions.LibSVM" ).newInstance();

If you prefer to give options set the options like this

String options = ( "-S 0 -K 0 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.001 -P 0.1" );
String[] optionsArray = options.split( " " );
    classifier.setOptions( optionsArray );

Finally train the classifier

classifier.buildClassifier( train );
陌若浮生 2024-10-27 11:51:37

要在最新版本的 weka (3.7.9) 中使用 libSVM 库,您只需使用 weka 应用程序的“包管理器”并安装 libSVM 包。

最后,在 java 项目中,您必须将“包管理器”创建的 LibSVM 库添加到类路径中。

通常它位于“(HOME)\wekafiles\packages\LibSVM”目录中。

To use the libSVM library with the newest version of weka (3.7.9), you only need to use the "Package Manager" of the weka application and install the libSVM package.

Finally from the java project, you have to add the LibSVM library that was created by the "Package Manager" to the Classpath.

Usually it's in the "(HOME)\wekafiles\packages\LibSVM" directory.

樱桃奶球 2024-10-27 11:51:37

事实证明,weka 人员通过从 Maven Central 提供最新版本的内容,使我们的工作变得更加轻松。

只需从这里获取依赖项:
http://mvnrepository.com/artifact/nz.ac.waikato.cms .weka/LibSVM

,只要有依赖关系,一切都会正常工作。不要乱搞包装器并向类路径添加 jar 或类似的东西。

我使用的是 3.7.12 版本,但我认为自从包管理器功能添加到 GUI 以来它就可用了。

Turns out the weka guys have made our job a LOT easier with the most recent versions by making things available from Maven Central.

Simply get the dependency from here:
http://mvnrepository.com/artifact/nz.ac.waikato.cms.weka/LibSVM

and everything will work as far as dependencies go. No messing around with wrappers and adding jars to the classpath or anything like that.

I'm using version 3.7.12 but I assume it's been available since the package manager function was added to the GUI.

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