WEKA API LibSVM ClassPath not found

2.8k views Asked by At

I'm trying to use LibSVM with Weka API.

My System: Win7 Weka 3.7.12 LibSVM 1.0.6 (Installed via Package Manager)

My Code:

import java.io.File;
import java.util.Random;

import javax.swing.JOptionPane;

import weka.classifiers.Evaluation;
import weka.classifiers.functions.LibSVM;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;

public class LibSVMClassifier {
// Method to build a SVM classifier with a given data file
public static double buildModel(File dataSet){

    // new instance of LibSVM
    LibSVM clsSVM = new LibSVM(); 

    try {
        Instances data = DataSource.read(dataSet.getAbsolutePath());

        // Sets the label feature
        data.setClassIndex(data.numAttributes()-1);

        String opts = "-S 0 -K 0 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.0010 -P 0.1"; 

        // set the options for the algorithm
        clsSVM.setOptions(weka.core.Utils.splitOptions(opts)); 

        Evaluation eval = new Evaluation(data);

        eval.crossValidateModel(clsSVM, data, 2, new Random(1));

        return eval.pctIncorrect();



    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
        e.printStackTrace();
    }
    return 100;
}
}

Code is called from here:

double error = LibSVMClassifier.buildModel(trainDataSet);

My problem: When I run my code and first use my J48 classifier (code at the end) and afterwards the LibSVM everything works fine.

If I run the LibSVM first I get the following error:

java.lang.Exception: libsvm classes not in CLASSPATH! weka.classifiers.functions.LibSVM.buildClassifier(LibSVM.java:1636) weka.classifiers.evaluation.Evaluation.crossValidateModel(Evaluation.java:764) weka.classifiers.Evaluation.crossValidateModel(Evaluation.java:374) totd.BuildModel.LibSVMClassifier.buildModel(LibSVMClassifier.java:34) totd.GUI.Gui$5.actionPerformed(Gui.java:215)

If I export the project to a runable jar and use it on another machine without weka installed the error will also occur if I run the J48 algorithm first. So no matter what I can't use the LibSVM on another machine.

I have read through all the other questions regarding this problem, but there was no solution for me. In order to prevent answers that will not help me here some things that wont work:

  • Explanation how to add a library to the project: Ive used the package manager from weka to install LibSVM and I added the resulting jar file for LIBSVM AS WELL AS weka jar file to my build path
  • Explanation how to use LibSVM with weka gui: I want to use LibSVM together with the weka api in a programmatic way, it already works in weka gui I dont need that!!!
  • Explanation how to change the classpath for your system: I want to export my project to a jar file and run it on any system I dont have access to the system class path

Possible solutions that I didnt understand but that I think might work if someone explains in detail:

  • https://stackoverflow.com/a/13766120/5006670 In this post it is mentioned to obtain the .class files from SVNLib (I suppose SVM?) and adding these to my buildpath. I do not understand which files he is speaking about and how I would compile the make file if I were to find it. But it sounds like my error message.

  • https://weka.wikispaces.com/LibSVM talks about using reflection. Im not sure how this is used

  • using a batch file to start the jar file together with the LibSVM.jar with -classpath command

J48 Code:

import java.io.File;

import javax.swing.JOptionPane;

import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.classifiers.trees.J48;
import weka.classifiers.Evaluation;
import java.util.Random;

public class J48Classifier {
// Method to build a J48 classifier with a given data file
public static double buildModel(File dataSet){

    // new instance of tree
    J48 clsJ48 = new J48(); 

    try {
        Instances data = DataSource.read(dataSet.getAbsolutePath());

        // Sets the label feature
        data.setClassIndex(data.numAttributes()-1);

        String[] options = new String[1];

        // unpruned tree
        options[0] = "-U"; 

        // set the options for the algorithm
        clsJ48.setOptions(options); 

        Evaluation eval = new Evaluation(data);
        eval.crossValidateModel(clsJ48, data, 2, new Random(1));

        return eval.pctIncorrect();



    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
        e.printStackTrace();
    }
    return 100;
}
}

My build path

1

There are 1 answers

0
Hasorko On

Okay, steps how magic work:

  1. Search for hours and fail
  2. Ask on a forum
  3. Try for 5 more minutes and succeed

SOLUTION: There are 2! LibSVM.jar files in the weka package folder and you need BOTH. So for all that try to use LibSVM using weka package manager go to: (HOME)\wekafiles\packages\LibSVM There you find the first FIRST LibSVM.jar

now go to: (HOME)\wekafiles\packages\LibSVM\lib here you will find libsvm.jar

ADD BOTH OF THESE JAR TO YOUR BUILD PATH!!!

Greetings