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:ca.uottawa.balie.WekaLearner.java

License:Open Source License

/**
 * Test the learned model./*from  www  .jav  a2 s .c  o  m*/
 * 
 * @return A summary string of the performance of the classifier
 */
public String TestModel() {
    if (DEBUG)
        DebugInfo.Out("Testing on " + m_TestingSet.numInstances() + " instances");
    Evaluation evaluation = null;
    try {
        evaluation = new Evaluation(m_TrainingSet);
        evaluation.evaluateModel(m_Scheme, m_TestingSet);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    String strSummary = evaluation.toSummaryString();
    strSummary += "\n\nConfusion Matrix: \n\n";
    m_ConfusionMatrix = evaluation.confusionMatrix();
    for (int i = 0; i != m_ConfusionMatrix.length; ++i) {
        for (int j = 0; j != m_ConfusionMatrix[i].length; ++j) {
            strSummary += String.valueOf(m_ConfusionMatrix[i][j]) + "\t";
        }
        strSummary += "\n";
    }
    return strSummary;
}

From source file:classify.Classifier.java

/**
 * @param args the command line arguments
 *///w  w  w. j ava2 s  .  c  om
public static void main(String[] args) {
    //read in data
    try {
        DataSource input = new DataSource("no_missing_values.csv");
        Instances data = input.getDataSet();
        //Instances data = readFile("newfixed.txt");
        missingValuesRows(data);

        setAttributeValues(data);
        data.setClassIndex(data.numAttributes() - 1);

        //boosting
        AdaBoostM1 boosting = new AdaBoostM1();
        boosting.setNumIterations(25);
        boosting.setClassifier(new DecisionStump());

        //build the classifier
        boosting.buildClassifier(data);

        //evaluate using 10-fold cross validation
        Evaluation e1 = new Evaluation(data);
        e1.crossValidateModel(boosting, data, 10, new Random(1));

        DecimalFormat nf = new DecimalFormat("0.000");

        System.out.println("Results of Boosting with Decision Stumps:");
        System.out.println(boosting.toString());
        System.out.println("Results of Cross Validation:");
        System.out.println("Number of correctly classified instances: " + e1.correct() + " ("
                + nf.format(e1.pctCorrect()) + "%)");
        System.out.println("Number of incorrectly classified instances: " + e1.incorrect() + " ("
                + nf.format(e1.pctIncorrect()) + "%)");

        System.out.println("TP Rate: " + nf.format(e1.weightedTruePositiveRate() * 100) + "%");
        System.out.println("FP Rate: " + nf.format(e1.weightedFalsePositiveRate() * 100) + "%");
        System.out.println("Precision: " + nf.format(e1.weightedPrecision() * 100) + "%");
        System.out.println("Recall: " + nf.format(e1.weightedRecall() * 100) + "%");

        System.out.println();
        System.out.println("Confusion Matrix:");
        for (int i = 0; i < e1.confusionMatrix().length; i++) {
            for (int j = 0; j < e1.confusionMatrix()[0].length; j++) {
                System.out.print(e1.confusionMatrix()[i][j] + "   ");
            }
            System.out.println();
        }
        System.out.println();
        System.out.println();
        System.out.println();

        //logistic regression
        Logistic l = new Logistic();
        l.buildClassifier(data);

        e1 = new Evaluation(data);

        e1.crossValidateModel(l, data, 10, new Random(1));
        System.out.println("Results of Logistic Regression:");
        System.out.println(l.toString());
        System.out.println("Results of Cross Validation:");
        System.out.println("Number of correctly classified instances: " + e1.correct() + " ("
                + nf.format(e1.pctCorrect()) + "%)");
        System.out.println("Number of incorrectly classified instances: " + e1.incorrect() + " ("
                + nf.format(e1.pctIncorrect()) + "%)");

        System.out.println("TP Rate: " + nf.format(e1.weightedTruePositiveRate() * 100) + "%");
        System.out.println("FP Rate: " + nf.format(e1.weightedFalsePositiveRate() * 100) + "%");
        System.out.println("Precision: " + nf.format(e1.weightedPrecision() * 100) + "%");
        System.out.println("Recall: " + nf.format(e1.weightedRecall() * 100) + "%");

        System.out.println();
        System.out.println("Confusion Matrix:");
        for (int i = 0; i < e1.confusionMatrix().length; i++) {
            for (int j = 0; j < e1.confusionMatrix()[0].length; j++) {
                System.out.print(e1.confusionMatrix()[i][j] + "   ");
            }
            System.out.println();
        }

    } catch (Exception ex) {
        //data couldn't be read, so end program
        System.out.println("Exception thrown, program ending.");
    }
}

From source file:de.unidue.langtech.grading.report.LearningCurveReport.java

License:Open Source License

@Override
public void execute() throws Exception {
    File storage = getContext().getStorageLocation(TestTask.TEST_TASK_OUTPUT_KEY, AccessMode.READONLY);

    for (Integer numberOfInstances : LearningCurveTask.NUMBER_OF_TRAINING_INSTANCES) {
        Properties props = new Properties();
        List<Double> kappas = new ArrayList<Double>();
        for (int iteration = 0; iteration < LearningCurveTask.ITERATIONS; iteration++) {
            File evaluationFile = new File(storage.getAbsolutePath() + "/" + TestTask.EVALUATION_DATA_FILENAME
                    + "_" + numberOfInstances + "_" + iteration);

            // we need to check non-existing files as we might skip some training sizes
            if (!evaluationFile.exists()) {
                continue;
            }//from w  w  w .j av  a  2 s  .c  om

            weka.classifiers.Evaluation eval = (weka.classifiers.Evaluation) SerializationHelper
                    .read(evaluationFile.getAbsolutePath());

            //               System.out.println(eval.toMatrixString());

            List<String> classLabels = TaskUtils.getClassLabels(eval.getHeader(), false);
            List<Integer> classLabelsInteger = new ArrayList<Integer>();
            for (String classLabel : classLabels) {
                classLabelsInteger.add(Integer.parseInt(classLabel));
            }

            double[][] confusionMatrix = eval.confusionMatrix();

            List<Integer> goldLabelsList = new ArrayList<Integer>();
            List<Integer> predictedLabelsList = new ArrayList<Integer>();

            // fill rating lists from weka confusion matrix
            for (int c = 0; c < confusionMatrix.length; c++) {
                for (int r = 0; r < confusionMatrix.length; r++) {
                    for (int i = 0; i < (int) confusionMatrix[c][r]; i++) {
                        goldLabelsList.add(classLabelsInteger.get(c));
                        predictedLabelsList.add(classLabelsInteger.get(r));
                    }
                }
            }

            double kappa = QuadraticWeightedKappa.getKappa(goldLabelsList, predictedLabelsList,
                    classLabelsInteger.toArray(new Integer[0]));
            kappas.add(kappa);
        }

        double min = -1.0;
        double max = -1.0;
        if (kappas.size() > 0) {
            min = Collections.min(kappas);
            max = Collections.max(kappas);
        }
        double meanKappa = QuadraticWeightedKappa.getMeanKappa(kappas);
        results.put(KappaReport.KAPPA, meanKappa);
        System.out.println(numberOfInstances + "\t" + meanKappa + "\t" + min + "\t" + max);

        for (String s : results.keySet()) {
            props.setProperty(s, results.get(s).toString());
        }

        // Write out properties
        getContext().storeBinary(TestTask.RESULTS_FILENAME + "_" + numberOfInstances,
                new PropertiesAdapter(props));
    }
}

From source file:ia02classificacao.IA02Classificacao.java

/**
 * @param args the command line arguments
 *///from  w  w  w.j  av  a  2 s . c  om
public static void main(String[] args) throws Exception {

    // abre o banco de dados arff e mostra a quantidade de instancias (linhas)
    DataSource arquivo = new DataSource("data/zoo.arff");
    Instances dados = arquivo.getDataSet();
    System.out.println("Instancias lidas: " + dados.numInstances());

    // FILTER: remove o atributo nome do animal da classificao
    String[] parametros = new String[] { "-R", "1" };
    Remove filtro = new Remove();
    filtro.setOptions(parametros);
    filtro.setInputFormat(dados);
    dados = Filter.useFilter(dados, filtro);

    AttributeSelection selAtributo = new AttributeSelection();
    InfoGainAttributeEval avaliador = new InfoGainAttributeEval();
    Ranker busca = new Ranker();
    selAtributo.setEvaluator(avaliador);
    selAtributo.setSearch(busca);
    selAtributo.SelectAttributes(dados);
    int[] indices = selAtributo.selectedAttributes();
    System.out.println("Selected attributes: " + Utils.arrayToString(indices));

    // Usa o algoritimo J48 e mostra a classificao dos dados em forma textual
    String[] opcoes = new String[1];
    opcoes[0] = "-U";
    J48 arvore = new J48();
    arvore.setOptions(opcoes);
    arvore.buildClassifier(dados);
    System.out.println(arvore);

    // Usa o algoritimo J48 e mostra a classificao de dados em forma grafica
    /*
    TreeVisualizer tv = new TreeVisualizer(null, arvore.graph(), new PlaceNode2());
    JFrame frame = new javax.swing.JFrame("?rvore de Conhecimento");
    frame.setSize(800,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(tv);
    frame.setVisible(true);
    tv.fitToScreen();
    */

    /*
    * Classificao de novos dados
    */

    System.out.println("\n\nCLASSIFICAO DE NOVOS DADOS");
    // criar atributos
    double[] vals = new double[dados.numAttributes()];
    vals[0] = 1.0; // hair
    vals[1] = 0.0; // feathers
    vals[2] = 0.0; // eggs
    vals[3] = 1.0; // milk
    vals[4] = 1.0; // airborne
    vals[5] = 0.0; // aquatic
    vals[6] = 0.0; // predator
    vals[7] = 1.0; // toothed
    vals[8] = 1.0; // backbone
    vals[9] = 1.0; // breathes
    vals[10] = 0.0; // venomous
    vals[11] = 0.0; // fins
    vals[12] = 4.0; // legs
    vals[13] = 1.0; // tail
    vals[14] = 1.0; // domestic
    vals[15] = 1.0; // catsize

    // Criar uma instncia baseada nestes atributos
    Instance meuUnicornio = new DenseInstance(1.0, vals);

    // Adicionar a instncia nos dados
    meuUnicornio.setDataset(dados);

    // Classificar esta nova instncia
    double label = arvore.classifyInstance(meuUnicornio);

    // Imprimir o resultado da classificao
    System.out.println("Novo Animal: Unicrnio");
    System.out.println("classificacao: " + dados.classAttribute().value((int) label));

    /*
    * Avaliao e predio de erros de mtrica
    */
    System.out.println("\n\nAVALIAO E PREDIO DE ERROS DE MTRICA");
    Classifier cl = new J48();
    Evaluation eval_roc = new Evaluation(dados);
    eval_roc.crossValidateModel(cl, dados, 10, new Random(1), new Object[] {});
    System.out.println(eval_roc.toSummaryString());

    /*
    * Matriz de confuso
    */
    System.out.println("\n\nMATRIZ DE CONFUSO");
    double[][] confusionMatrix = eval_roc.confusionMatrix();
    System.out.println(eval_roc.toMatrixString());

}

From source file:id3.MyID3.java

/**
 * Main method/*from  www  .j  av  a  2  s  .co m*/
 * @param args arguments
 */
public static void main(String[] args) {
    Instances instances;
    try {
        BufferedReader reader = new BufferedReader(new FileReader("D:\\Weka-3-6\\data\\weather.nominal.arff"));
        try {
            instances = new Instances(reader);
            instances.setClassIndex(instances.numAttributes() - 1);
            MyID3 id3 = new MyID3();
            try {
                id3.buildClassifier(instances);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // Test class distribution
            double[] classDistribution = id3.classDistribution(instances);
            for (int i = 0; i < classDistribution.length; i++) {
                System.out.println(classDistribution[i]);
            }
            // Test entrophy and information gain for each attribute
            System.out.println(id3.computeEntropy(instances));
            Enumeration attributes = instances.enumerateAttributes();
            while (attributes.hasMoreElements()) {
                System.out.println(id3.computeIG(instances, (Attribute) attributes.nextElement()));
            }
            // Test build classifier
            try {
                id3.buildClassifier(instances);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(id3.toString());
            // Evaluate model from build classifier (full training)
            Evaluation eval = null;
            try {
                eval = new Evaluation(instances);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                System.out.println(instances);
                eval.evaluateModel(id3, instances);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(eval.toSummaryString("\nResults Full-Training\n\n", false));
            // Evaluate model from build classifier (test set)
            // Test Confusion Matrix
            System.out.println("Confusion Matrix : ");
            double[][] cmMatrix = eval.confusionMatrix();
            for (int row_i = 0; row_i < cmMatrix.length; row_i++) {
                for (int col_i = 0; col_i < cmMatrix.length; col_i++) {
                    System.out.print(cmMatrix[row_i][col_i]);
                    System.out.print("|");
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:mao.datamining.ModelProcess.java

private void testWithExtraDS(Classifier classifier, Instances finalTrainDataSet, Instances finalTestDataSet,
        FileOutputStream testCaseSummaryOut, TestResult result) {
    //Use final training dataset and final test dataset
    double confusionMatrix[][] = null;

    long start, end, trainTime = 0, testTime = 0;
    if (finalTestDataSet != null) {
        try {//from   w w w  .j a  v  a 2s.  c  om
            //counting training time
            start = System.currentTimeMillis();
            classifier.buildClassifier(finalTrainDataSet);
            end = System.currentTimeMillis();
            trainTime += end - start;

            //counting test time
            start = System.currentTimeMillis();
            Evaluation testEvalOnly = new Evaluation(finalTrainDataSet);
            testEvalOnly.evaluateModel(classifier, finalTestDataSet);
            end = System.currentTimeMillis();
            testTime += end - start;

            testCaseSummaryOut.write("=====================================================\n".getBytes());
            testCaseSummaryOut.write((testEvalOnly.toSummaryString("=== Test Summary ===", true)).getBytes());
            testCaseSummaryOut.write("\n".getBytes());
            testCaseSummaryOut
                    .write((testEvalOnly.toClassDetailsString("=== Test Class Detail ===\n")).getBytes());
            testCaseSummaryOut.write("\n".getBytes());
            testCaseSummaryOut
                    .write((testEvalOnly.toMatrixString("=== Confusion matrix for Test ===\n")).getBytes());
            testCaseSummaryOut.flush();

            confusionMatrix = testEvalOnly.confusionMatrix();
            result.setConfusionMatrix4Test(confusionMatrix);

            result.setAUT(testEvalOnly.areaUnderROC(1));
            result.setPrecision(testEvalOnly.precision(1));
            result.setRecall(testEvalOnly.recall(1));
        } catch (Exception e) {
            ModelProcess.logging(null, e);
        }
        result.setTrainingTime(trainTime);
        result.setTestTime(testTime);
    } //using test data set , end

}

From source file:mao.datamining.ModelProcess.java

private void testCV(Classifier classifier, Instances finalTrainDataSet, FileOutputStream testCaseSummaryOut,
        TestResult result) {/*from  w  w w.  ja  v a  2  s.  c o  m*/
    long start, end, trainTime = 0, testTime = 0;
    Evaluation evalAll = null;
    double confusionMatrix[][] = null;
    // randomize data, and then stratify it into 10 groups
    Random rand = new Random(1);
    Instances randData = new Instances(finalTrainDataSet);
    randData.randomize(rand);
    if (randData.classAttribute().isNominal()) {
        //always run with 10 cross validation
        randData.stratify(folds);
    }

    try {
        evalAll = new Evaluation(randData);
        for (int i = 0; i < folds; i++) {
            Evaluation eval = new Evaluation(randData);
            Instances train = randData.trainCV(folds, i);
            Instances test = randData.testCV(folds, i);
            //counting traininig time
            start = System.currentTimeMillis();
            Classifier j48ClassifierCopy = Classifier.makeCopy(classifier);
            j48ClassifierCopy.buildClassifier(train);
            end = System.currentTimeMillis();
            trainTime += end - start;

            //counting test time
            start = System.currentTimeMillis();
            eval.evaluateModel(j48ClassifierCopy, test);
            evalAll.evaluateModel(j48ClassifierCopy, test);
            end = System.currentTimeMillis();
            testTime += end - start;
        }

    } catch (Exception e) {
        ModelProcess.logging(null, e);
    } //end test by cross validation

    // output evaluation
    try {
        ModelProcess.logging("");
        //write into summary file
        testCaseSummaryOut
                .write((evalAll.toSummaryString("=== Cross Validation Summary ===", true)).getBytes());
        testCaseSummaryOut.write("\n".getBytes());
        testCaseSummaryOut.write(
                (evalAll.toClassDetailsString("=== " + folds + "-fold Cross-validation Class Detail ===\n"))
                        .getBytes());
        testCaseSummaryOut.write("\n".getBytes());
        testCaseSummaryOut
                .write((evalAll.toMatrixString("=== Confusion matrix for all folds ===\n")).getBytes());
        testCaseSummaryOut.flush();

        confusionMatrix = evalAll.confusionMatrix();
        result.setConfusionMatrix10Folds(confusionMatrix);
    } catch (Exception e) {
        ModelProcess.logging(null, e);
    }
}

From source file:meddle.TrainModelByDomainOS.java

License:Open Source License

/**
 * Do evalution on trained classifier/model, including the summary, false
 * positive/negative rate, AUC, running time
 *
 * @param j48//from   w w w  .ja v  a  2 s  .  c o m
 *            - the trained classifier
 * @param domain
 *            - the domain name
 */
public static MetaEvaluationMeasures doEvaluation(Classifier classifier, String domainOS, Instances tras,
        MetaEvaluationMeasures mem) {
    try {
        Evaluation evaluation = new Evaluation(tras);
        evaluation.crossValidateModel(classifier, tras, 10, new Random(1));
        mem.numInstance = evaluation.numInstances();
        double M = evaluation.numTruePositives(1) + evaluation.numFalseNegatives(1);
        mem.numPositive = (int) M;
        mem.AUC = evaluation.areaUnderROC(1);
        mem.numCorrectlyClassified = (int) evaluation.correct();
        mem.accuracy = 1.0 * mem.numCorrectlyClassified / mem.numInstance;
        mem.falseNegativeRate = evaluation.falseNegativeRate(1);
        mem.falsePositiveRate = evaluation.falsePositiveRate(1);
        mem.fMeasure = evaluation.fMeasure(1);
        double[][] cmMatrix = evaluation.confusionMatrix();
        mem.confusionMatrix = cmMatrix;
        mem.TP = evaluation.numTruePositives(1);
        mem.TN = evaluation.numTrueNegatives(1);
        mem.FP = evaluation.numFalsePositives(1);
        mem.FN = evaluation.numFalseNegatives(1);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return mem;
}

From source file:org.openml.webapplication.io.Output.java

License:Open Source License

public static Map<Metric, MetricScore> evaluatorToMap(Evaluation evaluator, int classes, TaskType task)
        throws Exception {
    Map<Metric, MetricScore> m = new HashMap<Metric, MetricScore>();

    if (task == TaskType.REGRESSION) {

        // here all measures for regression tasks
        m.put(new Metric("mean_absolute_error", "openml.evaluation.mean_absolute_error(1.0)"),
                new MetricScore(evaluator.meanAbsoluteError(), (int) evaluator.numInstances()));
        m.put(new Metric("mean_prior_absolute_error", "openml.evaluation.mean_prior_absolute_error(1.0)"),
                new MetricScore(evaluator.meanPriorAbsoluteError(), (int) evaluator.numInstances()));
        m.put(new Metric("root_mean_squared_error", "openml.evaluation.root_mean_squared_error(1.0)"),
                new MetricScore(evaluator.rootMeanSquaredError(), (int) evaluator.numInstances()));
        m.put(new Metric("root_mean_prior_squared_error",
                "openml.evaluation.root_mean_prior_squared_error(1.0)"),
                new MetricScore(evaluator.rootMeanPriorSquaredError(), (int) evaluator.numInstances()));
        m.put(new Metric("relative_absolute_error", "openml.evaluation.relative_absolute_error(1.0)"),
                new MetricScore(evaluator.relativeAbsoluteError() / 100, (int) evaluator.numInstances()));
        m.put(new Metric("root_relative_squared_error", "openml.evaluation.root_relative_squared_error(1.0)"),
                new MetricScore(evaluator.rootRelativeSquaredError() / 100, (int) evaluator.numInstances()));

    } else if (task == TaskType.CLASSIFICATION || task == TaskType.LEARNINGCURVE
            || task == TaskType.TESTTHENTRAIN) {

        m.put(new Metric("average_cost", "openml.evaluation.average_cost(1.0)"),
                new MetricScore(evaluator.avgCost(), (int) evaluator.numInstances()));
        m.put(new Metric("total_cost", "openml.evaluation.total_cost(1.0)"),
                new MetricScore(evaluator.totalCost(), (int) evaluator.numInstances()));

        m.put(new Metric("mean_absolute_error", "openml.evaluation.mean_absolute_error(1.0)"),
                new MetricScore(evaluator.meanAbsoluteError(), (int) evaluator.numInstances()));
        m.put(new Metric("mean_prior_absolute_error", "openml.evaluation.mean_prior_absolute_error(1.0)"),
                new MetricScore(evaluator.meanPriorAbsoluteError(), (int) evaluator.numInstances()));
        m.put(new Metric("root_mean_squared_error", "openml.evaluation.root_mean_squared_error(1.0)"),
                new MetricScore(evaluator.rootMeanSquaredError(), (int) evaluator.numInstances()));
        m.put(new Metric("root_mean_prior_squared_error",
                "openml.evaluation.root_mean_prior_squared_error(1.0)"),
                new MetricScore(evaluator.rootMeanPriorSquaredError(), (int) evaluator.numInstances()));
        m.put(new Metric("relative_absolute_error", "openml.evaluation.relative_absolute_error(1.0)"),
                new MetricScore(evaluator.relativeAbsoluteError() / 100, (int) evaluator.numInstances()));
        m.put(new Metric("root_relative_squared_error", "openml.evaluation.root_relative_squared_error(1.0)"),
                new MetricScore(evaluator.rootRelativeSquaredError() / 100, (int) evaluator.numInstances()));

        m.put(new Metric("prior_entropy", "openml.evaluation.prior_entropy(1.0)"),
                new MetricScore(evaluator.priorEntropy(), (int) evaluator.numInstances()));
        m.put(new Metric("kb_relative_information_score",
                "openml.evaluation.kb_relative_information_score(1.0)"),
                new MetricScore(evaluator.KBRelativeInformation() / 100, (int) evaluator.numInstances()));

        Double[] precision = new Double[classes];
        Double[] recall = new Double[classes];
        Double[] auroc = new Double[classes];
        Double[] fMeasure = new Double[classes];
        Double[] instancesPerClass = new Double[classes];
        double[][] confussion_matrix = evaluator.confusionMatrix();
        for (int i = 0; i < classes; ++i) {
            precision[i] = evaluator.precision(i);
            recall[i] = evaluator.recall(i);
            auroc[i] = evaluator.areaUnderROC(i);
            fMeasure[i] = evaluator.fMeasure(i);
            instancesPerClass[i] = 0.0;/*from  www .ja  v a  2 s .  c  om*/
            for (int j = 0; j < classes; ++j) {
                instancesPerClass[i] += confussion_matrix[i][j];
            }
        }

        m.put(new Metric("predictive_accuracy", "openml.evaluation.predictive_accuracy(1.0)"),
                new MetricScore(evaluator.pctCorrect() / 100, (int) evaluator.numInstances()));
        m.put(new Metric("kappa", "openml.evaluation.kappa(1.0)"),
                new MetricScore(evaluator.kappa(), (int) evaluator.numInstances()));

        m.put(new Metric("number_of_instances", "openml.evaluation.number_of_instances(1.0)"),
                new MetricScore(evaluator.numInstances(), instancesPerClass, (int) evaluator.numInstances()));

        m.put(new Metric("precision", "openml.evaluation.precision(1.0)"),
                new MetricScore(evaluator.weightedPrecision(), precision, (int) evaluator.numInstances()));
        m.put(new Metric("recall", "openml.evaluation.recall(1.0)"),
                new MetricScore(evaluator.weightedRecall(), recall, (int) evaluator.numInstances()));
        m.put(new Metric("f_measure", "openml.evaluation.f_measure(1.0)"),
                new MetricScore(evaluator.weightedFMeasure(), fMeasure, (int) evaluator.numInstances()));
        if (Utils.isMissingValue(evaluator.weightedAreaUnderROC()) == false) {
            m.put(new Metric("area_under_roc_curve", "openml.evaluation.area_under_roc_curve(1.0)"),
                    new MetricScore(evaluator.weightedAreaUnderROC(), auroc, (int) evaluator.numInstances()));
        }
        m.put(new Metric("confusion_matrix", "openml.evaluation.confusion_matrix(1.0)"),
                new MetricScore(confussion_matrix));
    }
    return m;
}

From source file:statistics.BinaryStatisticsEvaluator.java

@Override
public double[][] getConfusionMatrix(Instances Training_Instances, Instances Testing_Instances,
        String classifier) {//from   ww w.  j  ava 2  s  .c  o m

    Classifier cModel = null;
    if ("NB".equals(classifier)) {
        cModel = (Classifier) new NaiveBayes();
        try {
            cModel.buildClassifier(Training_Instances);
        } catch (Exception ex) {
            Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if ("DT".equals(classifier)) {
        cModel = (Classifier) new J48();
        try {
            cModel.buildClassifier(Training_Instances);
        } catch (Exception ex) {
            Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if ("SVM".equals(classifier)) {
        cModel = (Classifier) new SMO();

        try {
            cModel.buildClassifier(Training_Instances);
        } catch (Exception ex) {
            Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if ("KNN".equals(classifier)) {
        cModel = (Classifier) new IBk();
        try {
            cModel.buildClassifier(Training_Instances);
        } catch (Exception ex) {
            Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    //Test the model
    Evaluation eTest;
    try {
        eTest = new Evaluation(Training_Instances);
        eTest.evaluateModel(cModel, Testing_Instances);
        //Print the result
        String strSummary = eTest.toSummaryString();
        System.out.println(strSummary);
        String strSummary1 = eTest.toMatrixString();
        System.out.println(strSummary1);
        String strSummary2 = eTest.toClassDetailsString();
        System.out.println(strSummary2);

        //Get the confusion matrix
        double[][] cmMatrix = eTest.confusionMatrix();
        return cmMatrix;
    } catch (Exception ex) {
        Logger.getLogger(BinaryStatisticsEvaluator.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}