How to identifying the exact instances that are wrongly classified in weka

343 views Asked by At

Here is my code, I'm using weka API. I want to printout wrongly classified instances and instances that are classified accurately. please help me, or tell me about any other text classification java API which is capable of doing what I want.

    public void evaluation() throws Exception{
    BufferedReader reader=null;
    reader= new BufferedReader(new FileReader("SparseDTM.arff"));

    Instances train= new Instances(reader);
    train.setClassIndex(0);
    train.toSummaryString();
    reader.close();
    SMO svm=new SMO();
    svm.buildClassifier(train);

    NaiveBayes nB = new NaiveBayes();
    nB.buildClassifier(train);

    weka.classifiers.Evaluation eval= new weka.classifiers.Evaluation(train);
    eval.crossValidateModel(nB, train,10,new Random(1));
    //eval.crossValidateModel(nB, train,10,new Random(1), new Object[] { });

    System.out.println("\n\t************Results by Naive Bayes Classifier************\n");
    System.out.println(eval.toSummaryString("", true));
    System.out.println(eval.toClassDetailsString());
//  System.out.println("F Measure: "+eval.fMeasure(1) + " " + "Precision: "+eval.precision(1) + " " + "Precision: "+eval.recall(1));
//  System.out.println("Correct :" + eval.correct());
//  System.out.println("Weighted True Negative Rate: " + eval.weightedTrueNegativeRate());
//  System.out.println("Weighted False Positive Rate:" + eval.weightedFalsePositiveRate());
//  System.out.println("Weighted False Negative Rate:" + eval.weightedFalseNegativeRate());
//  System.out.println("Weighted True Positive Rate:" + eval.weightedTruePositiveRate());
    System.out.println(eval.toMatrixString());
    }
1

There are 1 answers

3
Alex Rego On BEST ANSWER

Below is a method that will help you to solve your problem. So, You can edit it to reach your aim.

public void showPredictions(  ){    

    BufferedReader reader=null;
    reader= new BufferedReader(new FileReader("SparseDTM.arff"));

    Instances data = new Instances(reader);

    double[] predictions;
    try {

        NaiveBayes classifier = new NaiveBayes();
        classifier.buildClassifier(data);

        predictions = eval.evaluateModel(classifier, data );

        int classIndex = data.numAttributes()-1;
        // getting the array of predictions for each instance
        System.out.println("predictions: ");
        for (int i=0; i < data.numInstances(); i++ ) {
            double realValue = testData.instance(i).classValue(); // test or train data.
            System.out.print("Real Value: " + testData.instance(i).stringValue( classIndex ));
            System.out.println("\tClassification predicted value: " + predictions[i]);

            if( realValue != predictions[i] ) {
                System.out.println("misclassified instance: " + testData.instance(i).toString());
            }
        }       
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Replace "testData" by "data" if you can observe misclassified instances related to trainning set. Otherwise, you must provide a test set.