如何在我的 Java 代码中使用 LibSVM 和 Weka?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5223982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use LibSVM with Weka in my Java code?
提问by ruwanego
I want to use LibSVM classifier with Weka in my application. How can I (or where can I find good examples to) do this?
我想在我的应用程序中使用带有 Weka 的 LibSVM 分类器。我怎样才能(或在哪里可以找到好的例子)做到这一点?
回答by andrew
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.
现在肯定有点晚了,但无论如何我都会回答。您必须在项目中使用 weka.jar、libsvm.jar 和 wlsvm.jar(libsvm 包装器)。因此,只需在构建路径或类路径或其他路径中包含所有 3 个 jar。
You can get the wlsvm.jar from here: http://ailab.ist.psu.edu/yasser/wlsvm.html
您可以从这里获取 wlsvm.jar:http://ailab.ist.psu.edu/yasser/wlsvm.html
You can get weka from here: http://www.cs.waikato.ac.nz/ml/weka/
你可以从这里得到weka:http: //www.cs.waikato.ac.nz/ml/weka/
And you can get libsvm from here: http://www.csie.ntu.edu.tw/~cjlin/libsvm/
你可以从这里得到 libsvm: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).
我无法让它与 weka 3.7.7 或 3.7.8 一起工作,但我能够让它与 3.6.8(截至今天的最新稳定版本)一起工作。
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.
此外,因为我必须从 svnlib 中获取 .class 文件,并将这些文件包含在我的项目的构建路径中。要构建 .class 文件,请使用 SVNLib/java 中的 make 文件。
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.
祝你好运。
回答by Tharindu
With new version you just need the weka.jar and call svm like this,
使用新版本,您只需要 weka.jar 并像这样调用 svm,
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 );
回答by fran
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.
要在最新版本的weka (3.7.9) 中使用libSVM 库,您只需要使用weka 应用程序的“包管理器”并安装libSVM 包。
Finally from the java project, you have to add the LibSVM library that was created by the "Package Manager" to the Classpath.
最后从java项目中,你必须将“包管理器”创建的LibSVM库添加到类路径中。
Usually it's in the "(HOME)\wekafiles\packages\LibSVM" directory.
通常它在“ (HOME)\wekafiles\packages\LibSVM”目录中。
回答by Mathieu Dumoulin
Turns out the weka guys have made our job a LOT easier with the most recent versions by making things available from Maven Central.
事实证明,通过从 Maven Central 提供内容,weka 人员使用最新版本使我们的工作变得更加轻松。
Simply get the dependency from here: http://mvnrepository.com/artifact/nz.ac.waikato.cms.weka/LibSVM
只需从这里获取依赖项: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.
就依赖项而言,一切都会正常工作。不要乱用包装器和将 jars 添加到类路径或类似的东西。
I'm using version 3.7.12 but I assume it's been available since the package manager function was added to the GUI.
我使用的是 3.7.12 版,但我认为它已经可用,因为包管理器功能已添加到 GUI 中。
回答by Somnath Kadam
follow this link for combining Weka and libsvm http://www.cs.iastate.edu/~yasser/wlsvm/
按照这个链接结合 Weka 和 libsvm http://www.cs.iastate.edu/~yasser/wlsvm/
weka is good calculating ROC,recall,etc.... and libsvm is good for classification, regression,etc...
weka 擅长计算 ROC、召回等……而 libsvm 擅长分类、回归等……