Weka error "cannot handle numeric class" in Java code using LibSVM

accand picture accand · Jul 18, 2013 · Viewed 9.3k times · Source

I'm trying to use a classifier LibSVM-based using Weka, but i got this error:

Exception in thread "main" weka.core.UnsupportedAttributeTypeException:weka.classifiers.functions.LibSVM: Cannot handle numeric class!
    at weka.core.Capabilities.test(Unknown Source)
    at weka.core.Capabilities.test(Unknown Source)
    at weka.core.Capabilities.test(Unknown Source)
    at weka.core.Capabilities.testWithFail(Unknown Source)
    at weka.classifiers.functions.LibSVM.buildClassifier(Unknown Source)
    at imgclassifier.ImgClassifier.main(ImgClassifier.java:45)
Java Result: 1

this is my code:

try {

   File f = new File("australian.txt");
   LibSVMLoader loader = new LibSVMLoader();
   loader.setSource(f);
   Instances i = loader.getDataSet();

   LibSVM svm = new LibSVM();
   svm.buildClassifier(i);

}catch (IIOException e) {
   e.printStackTrace();
}

australian.txt is an example taken here:LibSVM DataSets can anyone explain me where is the error? thanks in advance

Answer

Rob Cranfill picture Rob Cranfill · Mar 17, 2015

I can't critique your entire approach, but one essential item you are missing is telling SVM that you want to do regression. (Contrary to many people's impression, LibSVM can do regression; see http://www.csie.ntu.edu.tw/~cjlin/libsvm/ - "LIBSVM is … for … regression (epsilon-SVR, nu-SVR)…")

You need to do this to tell it to do regression:

svm.setSVMType(new SelectedTag(LibSVM.SVMTYPE_EPSILON_SVR, LibSVM.TAGS_SVMTYPE)); // -S 3=epsilon-SVR

/rob

PS - I still got the 'Cannot handle numeric class' error until I coded all the parameters using the Java object-oriented approach, using methods on the LibSVM object, instead of using the String-based "Options" approach. I don't know why this is, and may be a red herring, but there it is.