我正在尝试为我的特定问题建立交叉验证过程。我将我的代码和WEKA GUI输出进行了比较,但是正确分类和错误分类实例的数量之间存在差异。我可以错过什么吗?
下面给出了我的代码(使用WEKA API版本3.8.3,我也尝试了3.8.2):
private static void crossValidate(Instances instances, Classifier classifier, int k_value) throws Exception {
int seed = 1;
int folds = k_value;
Random rand = new Random(seed);
Instances randData = new Instances(instances);
randData.randomize(rand);
int correctlyClassified = 0;
int incorrectlyClassified = 0;
for(int n = 0; n < folds; n++) {
Instances train = randData.trainCV(folds, n, rand);
Instances test = randData.testCV(folds, n);
classifier.buildClassifier(train);
for (int t = 0; t < test.numInstances(); t++) {
Instance aTestInstance = test.instance(t);
String actualClass = test.classAttribute().value((int)aTestInstance.classValue());
String predictedClass = test.classAttribute().value((int)classifier.classifyInstance(aTestInstance));
if(actualClass.equals(predictedClass)) correctlyClassified++;
else incorrectlyClassified ++;
}
}
System.out.println("Total True is:" + correctlyClassified + "/" + incorrectlyClassified);
}