Which Weka and LibSVM .jar files to use in Java code for SVM classification

青春壹個敷衍的年華 提交于 2019-12-05 20:12:49

The problem with my test code was environmental to do with the .jar files needed for Weka to programmatically run LibSVM.

If my code is:

public static void classify() {      
    try {            
        Instances train = new Instances (...);            
        train.setClassIndex(train.numAttributes() - 1);         
        Instances test = new Instances (...);            
        test.setClassIndex(test.numAttributes() - 1);                      
        ClassificationType classificationType = ClassificationTypeDAO.get(6);  // 6 is SVM.        
        LibSVM classifier = new LibSVM();
        String options = (classificationType.getParameters());
        String[] optionsArray = options.split(" ");                          
        classifier.setOptions(optionsArray);        
        classifier.buildClassifier(train);        
        Evaluation eval = new Evaluation(train);
        eval.evaluateModel(classifier, test);
        System.out.println(eval.toSummaryString("\nResults\n======\n", false));       
    } 
    catch (Exception ex) {            
        Misc_Utils.printStackTrace(ex);
    }                       
}

I found that I needed to place weka.jar (from Weka) and libsvm.jar (from http://www.csie.ntu.edu.tw/~cjlin/libsvm/ in the application's .lib folder. But because of the naming clash in Windows, I renamed the file LibSVM.jar (from Weka) to LibSVM_Weka.jar and added it to the .lib folder.

Running the program I now have results which match Weka's Explorer using keyword frequencies distributed unevenly across 5 categories of data.

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