开发者

How to use LibSVM with Weka in my Java code?

开发者 https://www.devze.com 2023-02-15 03:42 出处:网络
I want to use LibSVM classifier wit开发者_StackOverflowh Weka in my application. How can I (or where can I find good examples to) do this?A little late now, surely, but I\'ll answer anyways.You have t

I want to use LibSVM classifier wit开发者_StackOverflowh Weka in my application. How can I (or where can I find good examples to) do this?


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.


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


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消