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

2

6

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

Flintlock answered 18/7, 2013 at 10:16 Comment(0)
A
7

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.

Aeneid answered 17/3, 2015 at 21:23 Comment(0)
A
-1

Set the class index of the column you want to predict. So just try this,

i.setClassIndex( train.numAttributes() - 1 );
Aphrodisia answered 10/8, 2015 at 5:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.