britt
britt

Reputation: 83

weka - how to print incorrectly classified instances

my weka output shows:

Correctly Classified Instances       32083               94.0244 %
Incorrectly Classified Instances      2039                5.9756 %

I want to be able to print out what the incorrect instances were so i can make adjustments and understand why they were misclassified.

my print method is below.
i am attempting to find instances whose predicted class value were not equal to the actual class value and then print its attributes.
but when i do this the attribute enumeration is not printing anything.

Does anyone have a suggestion for how to print out the misclassified instances?

thanks much.

private void printSummary(Classifier base, Evaluation eval, Instances data) throws Exception
{
    // output evaluation
    System.out.println();
    System.out.println("=== Setup ===");
    System.out.println("Classifier: " + classifierName.getClass().getName() + " " + Utils.joinOptions(base.getOptions()));
    System.out.println("Dataset: " + data.relationName());
    System.out.println();

    // output predictions
    System.out.println("# - actual - predicted - error - distribution - token");
    for (int i = 0; i < data.numInstances(); i++) 
    {
        double pred = base.classifyInstance(data.instance(i));
        double actual = data.instance(i).classValue();
        double[] dist = base.distributionForInstance(data.instance(i));

        if (pred != actual)
        {
            System.out.print((i+1));
            System.out.print(" - ");
            System.out.print(data.instance(i).toString(data.classIndex()));
            System.out.print(" - ");
            System.out.print(data.classAttribute().value((int) pred));
            System.out.print(" - ");
            if (pred != data.instance(i).classValue())
                System.out.print("yes");
            else
                System.out.print("no");
            System.out.print(" - ");
            System.out.print(Utils.arrayToString(dist));
            System.out.print(" - ");
            data.instance(i).enumerateAttributes().toString();
            System.out.println();
        }
    }

    System.out.println(eval.toSummaryString());
    System.out.println(eval.toClassDetailsString());
    System.out.println(eval.toMatrixString());
}

Upvotes: 5

Views: 5311

Answers (2)

danny11
danny11

Reputation: 483

This is old post but I had the same problem and solved it differently. Maybe someone like me will need it.

What I did is that Evaluation have predictions method which returns ArrayList of prediction object.

Each Prediction object have actual and predicted and I simply printed each instance that his actual is not the same the predicted value.

My code:

ArrayList<Prediction> predictions = evaluation.predictions();
for (int i = 0, trainDataSize = trainData.size(); i < trainDataSize; i++) {
        Instance instance = trainData.get(i);
        Prediction prediction = predictions.get(i);

        if (prediction.actual() != prediction.predicted()) {

            System.out.println(instance);;

        }

    }

Hope it help someone.

Upvotes: 1

yura
yura

Reputation: 14645

I do this that way:

  1. Train classifier.
  2. For each instance I call 'classifier.explain'
  3. If classification is incorrect I store them by incorrect probability (from worst error to least confident error)
  4. Most confident error give me ideas what features should be added to classifier.

Upvotes: 1

Related Questions