今天碰到了这个问题,网上搜了好久,才找到原因是版本问题
我weka用的是3.6,后来把libsvm也改成了3.2的就可以了
在eclipse里引用的weka的api,所以把libsvm.jar,weka.jar都加进工程了
public void test(){ File inputfile = new File("test/train_message.arff"); ArffLoader loader = new ArffLoader(); try { loader.setFile(inputfile); Instances insTrain = loader.getDataSet(); insTrain.setClassIndex(insTrain.numAttributes()-1); // Classifier classifier = new WLSVM(); Classifier classifier = (Classifier) Class.forName( "weka.classifiers.functions.LibSVM").newInstance(); String[] optSVM = weka.core.Utils.splitOptions("-c 8.0 -g 8.0"); classifier.setOptions(optSVM); classifier.buildClassifier(insTrain); // Instance current = null; // while ((current = loader.getNextInstance(insTrain)) != null) { // classifier.distributionForInstance(current); // } inputfile = new File("test/test_message.arff"); loader.setFile(inputfile); Instances insTest = loader.getDataSet(); insTest.setClassIndex(insTest.numAttributes()-1); Evaluation eval = new Evaluation(insTest); eval.crossValidateModel(classifier, insTest, 10, new Random(1)); System.out.println(eval.errorRate()); System.out.println(classifier.toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/13775.html