Example usage for weka.classifiers Evaluation confusionMatrix

List of usage examples for weka.classifiers Evaluation confusionMatrix

Introduction

In this page you can find the example usage for weka.classifiers Evaluation confusionMatrix.

Prototype

public double[][] confusionMatrix() 

Source Link

Document

Returns a copy of the confusion matrix.

Usage

From source file:view.centerPanels.ClassificationPnlCenterPredict.java

private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed

    //imate vec objekat instances i objekat naiveBayes u ovoj klasi
    //treba da izvucete ono sto je uetu u text fieldove preko liste i da napravite
    //objekat Instance (nema "s"). kad naravite objekat Instance samo ga dodate
    //na instances.add(  )..
    //onda koristite takav Instances i naiveBayes za evaluaciju i u textArea
    //ispisujete kojoj klasi pripada.. Sad ne trba matrica konfuzije potrebno
    //je da ispisete string kojoj klasi pripada
    //pogledajte klasu ClusterinPnlCenterPredict tu je odradjeno 90% onog sto se i ovde trazi
    //        instances.delete();
    for (int i = 0; i < textFields.size(); i++) {
        String text = textFields.get(i).getText().trim();

        //prekace prazna pollja jer za klasterizaciju znaci da se ona ignorisu
        //to za klasifikaciju nije slucaj
        if (!text.equals("")) {

            if (instances.attribute(i).isNominal()) {
                boolean correct = false;
                for (int j = 0; j < instances.attribute(i).numValues(); j++) {
                    if (text.equals(instances.attribute(i).value(j))) {
                        correct = true;/*w  ww.ja va2s.c o  m*/
                    }
                }
                if (!correct) {
                    JOptionPane.showMessageDialog(this,
                            "Incorrect format for attribute " + instances.attribute(i).name());
                    break;
                }
            }

            if (instances.attribute(i).isNumeric()) {
                try {
                    double value = Double.parseDouble(text);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this,
                            "Incorrect format for attribute " + instances.attribute(i).name());
                    break;
                }
            }

        }
    }

    int numAttributes = instances.numAttributes();

    Instance instance = new Instance(numAttributes);

    for (int i = 0; i < textFields.size(); i++) {
        String text = textFields.get(i).getText().trim();

        try {
            double value = Double.parseDouble(text);
            instance.setValue(i, value);

        } catch (Exception e) {
            instance.setValue(i, text);
        }
    }

    String value = instances.attribute(numAttributes - 1).value(0);
    Attribute Att = instances.attribute(numAttributes - 1);

    instance.setValue(Att, value);
    //        instances.delete();
    //        instance.setValue((numAttributes - 1), instances.attribute(numAttributes - 1).value(0) );

    instances.add(instance);

    double[][] matrix = null;

    System.out.println(instances);
    System.out.println(naiveBayes);

    try {

        //                instances.setClassIndex(instances.numAttributes() - 1);
        Evaluation eval = new Evaluation(instances);

        eval.evaluateModel(naiveBayes, instances);

        textArea.setText(eval.toMatrixString());
        matrix = eval.confusionMatrix();

    } catch (Exception ex) {
        System.out.println("Greska: " + ex);
    }

    double[] array = new double[matrix.length];

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

        double sum = 0;
        for (int j = 0; j < array.length; j++) {

            sum = sum + matrix[j][i];

        }
        array[i] = sum;

    }

    String className = null;
    for (int i = 0; i < array.length; i++) {
        if (array[i] == 1) {
            className = instances.attribute(numAttributes - 1).value(i);
        }
    }

    textArea.setText("This instance belongs to class: \n\n== " + className + " ==");

    instances.delete();
}