Weka分类:错误+正确<总实例数,怎么来的?

Posted

技术标签:

【中文标题】Weka分类:错误+正确<总实例数,怎么来的?【英文标题】:Weka classification: wrong+correct < total instances, how come? 【发布时间】:2017-12-28 08:16:15 【问题描述】:

我为著名的鸢尾花问题运行了这段代码,进行了 10 次交叉验证,然后使用 5 种不同的分类方法对它们进行了分类。

这应该使分类器在 135 个实例上训练并在 15 个实例上测试 10 次,所以我希望有错误的分类实例 + 正确的分类实例 = 15。

以下是代码和输出。

public class WekaTest 
   public static void main(String[] args) throws Exception       
    // Comments are denoted by "//" at the beginning of the line.

    BufferedReader datafile = readDataFile("C:\\Program Files\\Weka-3-8\\data\\iris.arff");
    //BufferedReader datafile = readDataFile("C:\\hwork\\titanic\\train.arff");

    Instances data = new Instances(datafile);
    data.setClassIndex(data.numAttributes() - 1);


    // Choose a type of validation split
    Instances[][] split = crossValidationSplit(data, 10);

    // Separate split into training and testing arrays
    Instances[] trainingSplits = split[0];
    Instances[] testingSplits  = split[1];

    // Choose a set of classifiers
    Classifier[] models =      new J48(),
                                new PART(),
                                new DecisionTable(),
                                new OneR(),
                                new DecisionStump() ;

    // Run for each classifier model
    double[][][] predictions = new double[100][100][2];
    for(int j = 0; j < models.length; j++)         

        for(int i = 0; i < trainingSplits.length; i++)                 


           Evaluation validation = new Evaluation(trainingSplits[i]);        
           models[j].buildClassifier(trainingSplits[i]);
           validation.evaluateModel(models[j],  testingSplits[i]);                                    


           predictions[j][i][0] = validation.correct();
           predictions[j][i][1] = validation.incorrect();

           System.out.println("Classifier: "+models[j].getClass()+" : Correct: "+predictions[j][i][0]+", Wrong: "+predictions[i][j][1]);               
        //training foreach fold.
        System.out.println("===================================================================");
    //training foreach classifier.

//main().





public static BufferedReader readDataFile(String filename) 
    BufferedReader inputReader = null;

    try 
        inputReader = new BufferedReader(new FileReader(filename));
     catch (FileNotFoundException ex) 
        System.err.println("File not found: " + filename);
            
    return inputReader;
//readDataFile().

public static Evaluation simpleClassify(Classifier model, Instances trainingSet, Instances testingSet) throws Exception 
    Evaluation validation = new Evaluation(trainingSet);        
    model.buildClassifier(trainingSet);
    validation.evaluateModel(model, testingSet);        
    return validation;
//simpleClassify().

public static double calculateAccuracy(FastVector predictions) 
    double correct = 0;

    for (int i = 0; i < predictions.size(); i++) 
        NominalPrediction np = (NominalPrediction) predictions.elementAt(i);
        if (np.predicted() == np.actual()) 
            correct++;
        
    

    return 100 * correct / predictions.size();
//calculateAccuracy().

public static Instances[][] crossValidationSplit(Instances data, int numberOfFolds) 
    Instances[][] split = new Instances[2][numberOfFolds];

    for (int i = 0; i < numberOfFolds; i++) 
        split[0][i] = data.trainCV(numberOfFolds, i);
        split[1][i] = data.testCV(numberOfFolds, i);
            
    return split;
//corssValidationSplit().


//class.

=====================

输出:

Classifier: class weka.classifiers.trees.J48 : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.J48 : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.J48 : Correct: 14.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.J48 : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.J48 : Correct: 14.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.J48 : Correct: 13.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.J48 : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.J48 : Correct: 13.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.J48 : Correct: 12.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.J48 : Correct: 15.0, Wrong: 0.0
===================================================================
Classifier: class weka.classifiers.rules.PART : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.PART : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.PART : Correct: 14.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.PART : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.PART : Correct: 14.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.PART : Correct: 13.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.PART : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.PART : Correct: 13.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.PART : Correct: 9.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.PART : Correct: 13.0, Wrong: 0.0
===================================================================
Classifier: class weka.classifiers.rules.DecisionTable : Correct: 15.0, Wrong: 1.0
Classifier: class weka.classifiers.rules.DecisionTable : Correct: 15.0, Wrong: 1.0
Classifier: class weka.classifiers.rules.DecisionTable : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.DecisionTable : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.DecisionTable : Correct: 13.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.DecisionTable : Correct: 13.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.DecisionTable : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.DecisionTable : Correct: 13.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.DecisionTable : Correct: 12.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.DecisionTable : Correct: 14.0, Wrong: 0.0
===================================================================
Classifier: class weka.classifiers.rules.OneR : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.OneR : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.OneR : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.OneR : Correct: 14.0, Wrong: 1.0
Classifier: class weka.classifiers.rules.OneR : Correct: 13.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.OneR : Correct: 12.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.OneR : Correct: 15.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.OneR : Correct: 14.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.OneR : Correct: 14.0, Wrong: 0.0
Classifier: class weka.classifiers.rules.OneR : Correct: 14.0, Wrong: 0.0
===================================================================
Classifier: class weka.classifiers.trees.DecisionStump : Correct: 15.0, Wrong: 1.0
Classifier: class weka.classifiers.trees.DecisionStump : Correct: 15.0, Wrong: 1.0
Classifier: class weka.classifiers.trees.DecisionStump : Correct: 15.0, Wrong: 2.0
Classifier: class weka.classifiers.trees.DecisionStump : Correct: 5.0, Wrong: 2.0
Classifier: class weka.classifiers.trees.DecisionStump : Correct: 0.0, Wrong: 15.0
Classifier: class weka.classifiers.trees.DecisionStump : Correct: 0.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.DecisionStump : Correct: 5.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.DecisionStump : Correct: 0.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.DecisionStump : Correct: 0.0, Wrong: 0.0
Classifier: class weka.classifiers.trees.DecisionStump : Correct: 0.0, Wrong: 0.0
===================================================================

【问题讨论】:

【参考方案1】:

在打印线上

System.out.println("Classifier: "+models[j].getClass()+" : Correct: "+predictions[j][i][0]+", Wrong: "+predictions[i][j][1]);      

以下部分

Wrong: "+predictions[i][j][1]);

应该是

Wrong: "+predictions[j][i][1]);

你切换了 ji

【讨论】:

以上是关于Weka分类:错误+正确<总实例数,怎么来的?的主要内容,如果未能解决你的问题,请参考以下文章

weka - 如何打印错误分类的实例

为啥决策树显示正确分类而某些实例被错误分类

weka中新实例的分类

如何在 Weka GUI 中识别错误分类实例的详细信息?

如何在weka中预处理数据以进行分类

机器学习基础4--评估线性分类