我使用eclipse在Java上编写了一个SVM代码,这是我的代码:
import weka.core.Instances;
import java.util.Random;
import weka.core.converters.ConverterUtils.DataSource;
import weka.classifiers.trees.J48;
import weka.classifiers.evaluation.*;
import weka.classifiers.functions.SMO;
public class Evaluate {
public static void main (String args [])throws Exception{
DataSource source = new DataSource("/Users/kdash/workspace/Weka/Negative.arff");
Instances dataset = source.getDataSet();
//set class index to last attribute
dataset.setClassIndex(dataset.numAttributes()-1);
//Create Classifier
//J48 tree = new J48();
//tree.buildClassifier(dataset);
SMO svm = new SMO();
svm.buildClassifier(dataset);
Evaluation eval = new Evaluation(dataset);
// test dataset
DataSource source1 = new DataSource("/Users/kdash/workspace/Weka/Negative.arff");
Instances testDataset = source1.getDataSet();
testDataset.setClassIndex(testDataset.numAttributes() -1);
eval.evaluateModel(svm, testDataset);
System.out.println(eval.toSummaryString("Evaluation: \n", false));
System.out.println("Correct % = " + eval.pctCorrect());
System.out.println("Incorrect % = " + eval.pctIncorrect());
System.out.println("AUC % = " + eval.areaUnderROC(1));
System.out.println("Kappa % = " + eval.kappa());
System.out.println("MAE % = " + eval.meanAbsoluteError());
System.out.println("RMSE % = " + eval.rootMeanSquaredError());
System.out.println("RAE % = " + eval.relativeAbsoluteError());
System.out.println("RRSE % = " + eval.rootRelativeSquaredError());
System.out.println("Precision % = " + eval.precision(1));
System.out.println("Recall % = " + eval.recall(1));
System.out.println("F-Measure % = " + eval.fMeasure(1));
System.out.println("Error rate % = " + eval.errorRate());
//confusion matrix
System.out.println(eval.toMatrixString("Overall Confusion Matrix === \n"));
}
}
但我收到以下错误:
Exception in thread "main" weka.core.UnsupportedAttributeTypeException:
weka.classifiers.functions.SMO:无法处理数字类! at weka.core.Capabilities.test(Capabilities.java:953) at weka.core.Capabilities.test(Capabilities.java:1109) at weka.core.Capabilities.test(Capabilities.java:1022) at weka.core.Capabilities.testWithFail(Capabilities.java:1301) at weka.classifiers.functions.SMO.buildClassifier(SMO.java:1250) 在Evaluate.main(Evaluate.java:25)
我的数据集包含三个不同的行 1.所有行的极性(-1) 2.阶级 - 否定 3.单词出现
答案 0 :(得分:0)
数据集示例:
发生:1 等级:-1 极性:否定
答案 1 :(得分:0)
我不知道arff文件是如何精确组织的,但错误假定最后一个属性(类索引设置的位置)是数字(例如,单词出现次数)
无法使用数字类构建SVM。尝试将类索引设置为正确(取决于arff文件)或修改arff-header(如果将正确的类索引分配给错误的类型(数字)。