How to use LibSVM with Weka in my Java code?

随声附和 提交于 2019-12-17 16:18:27

问题


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


回答1:


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.




回答2:


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 );



回答3:


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.




回答4:


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.




回答5:


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...



来源:https://stackoverflow.com/questions/5223982/how-to-use-libsvm-with-weka-in-my-java-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!