Example usage for weka.classifiers Evaluation toMatrixString

List of usage examples for weka.classifiers Evaluation toMatrixString

Introduction

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

Prototype

public String toMatrixString(String title) throws Exception 

Source Link

Document

Outputs the performance statistics as a classification confusion matrix.

Usage

From source file:Controller.CtlDataMining.java

public String arbolJ48(Instances data) {
    try {//from   w  ww  . j  a v a 2 s. co  m
        // Creamos un clasidicador J48
        J48 j48 = new J48();
        //creamos el clasificador  del J48 con los datos 
        j48.buildClassifier(data);

        //Creamos un objeto para la validacion del modelo con redBayesiana
        Evaluation evalJ48 = new Evaluation(data);

        /*Aplicamos el clasificador J48
        hacemos validacion cruzada, de redBayesiana, con 10 campos, 
        y el aleatorio arrancando desde 1 para la semilla*/
        evalJ48.crossValidateModel(j48, data, 10, new Random(1));
        //Obtenemos resultados
        String resJ48 = "<br><b><center>Resultados Arbol de decision J48"
                + "</center><br>========<br>Modelo generado indica los "
                + "siguientes resultados:<br>========<br></b>";

        resJ48 = resJ48
                + ("<b>1. Numero de instancias clasificadas:</b> " + (int) evalJ48.numInstances() + "<br>");
        resJ48 = resJ48 + ("<b>2. Porcentaje de instancias correctamente " + "clasificadas:</b> "
                + formato.format(evalJ48.pctCorrect()) + "<br>");
        resJ48 = resJ48 + ("<b>3. Numero de instancias correctamente " + "clasificadas:</b>"
                + (int) evalJ48.correct() + "<br>");
        resJ48 = resJ48 + ("<b>4. Porcentaje de instancias incorrectamente " + "clasificadas:</b> "
                + formato.format(evalJ48.pctIncorrect()) + "<br>");
        resJ48 = resJ48 + ("<b>5. Numero de instancias incorrectamente " + "clasificadas:</b> "
                + (int) evalJ48.incorrect() + "<br>");
        resJ48 = resJ48 + ("<b>6. Media del error absoluto:</b> " + formato.format(evalJ48.meanAbsoluteError())
                + "<br>");
        resJ48 = resJ48
                + ("<b>7. " + evalJ48.toMatrixString("Matriz de" + " confusion</b>").replace("\n", "<br>"));

        // SE GRAFICA EL ARBOL GENERADO
        //Se crea un Jframe Temporal
        final javax.swing.JFrame jf = new javax.swing.JFrame("Arbol de decision: J48");
        /*Se asigna un tamao*/
        jf.setSize(500, 400);
        /*Se define un borde*/
        jf.getContentPane().setLayout(new BorderLayout());
        /*Se instancia la grafica del arbol, estableciendo el tipo J48
        Parametros (Listener, Tipo de arbol, Tipo de nodos)
        El placeNode2 colocar los nodos para que caigan en forma uniforme
        por debajo de su padre*/
        TreeVisualizer tv = new TreeVisualizer(null, j48.graph(), new PlaceNode2());
        /*Aade el arbol centrandolo*/
        jf.getContentPane().add(tv, BorderLayout.CENTER);
        /*Aadimos un listener para la X del close*/
        jf.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                jf.dispose();
            }
        });
        /*Lo visualizamos*/
        jf.setVisible(true);
        /*Ajustamos el arbol al ancho del JFRM*/
        tv.fitToScreen();

        return resJ48;

    } catch (Exception e) {
        return "El error es" + e.getMessage();

    }
}

From source file:farm_ads.MyClassifier.java

public String printEvaluation(Evaluation e) throws Exception {
    String s = new String();
    s += e.toSummaryString("\nResults\n======\n", false);
    s += "\n" + e.toMatrixString("Matrix String");
    s += "\n" + e.toClassDetailsString();
    return s;/* www. j a  v a 2  s . c  o  m*/
}

From source file:main.mFFNN.java

public static void main(String[] args) throws Exception {
    mFFNN m = new mFFNN();
    BufferedReader breader = null;
    breader = new BufferedReader(new FileReader("src\\main\\iris.arff"));
    Instances fileTrain = new Instances(breader);
    fileTrain.setClassIndex(fileTrain.numAttributes() - 1);
    System.out.println(fileTrain);
    breader.close();/*from  w ww.  ja v a  2 s  .c  o  m*/
    System.out.println("mFFNN!!!\n\n");
    FeedForwardNeuralNetwork FFNN = new FeedForwardNeuralNetwork();

    Evaluation eval = new Evaluation(fileTrain);
    FFNN.buildClassifier(fileTrain);

    eval.evaluateModel(FFNN, fileTrain);

    //OUTPUT
    Scanner scan = new Scanner(System.in);
    System.out.println(eval.toSummaryString("=== Stratified cross-validation ===\n" + "=== Summary ===", true));
    System.out.println(eval.toClassDetailsString("=== Detailed Accuracy By Class ==="));
    System.out.println(eval.toMatrixString("===Confusion matrix==="));
    System.out.println(eval.fMeasure(1) + " " + eval.recall(1));
    System.out.println("\nDo you want to save this model(1/0)? ");
    FFNN.distributionForInstance(fileTrain.get(0));
    /* int c = scan.nextInt();
    if (c == 1 ){
     System.out.print("Please enter your file name (*.model) : ");
     String infile = scan.next();
     m.saveModel(FFNN,infile);
    }
    else {
    System.out.print("Model not saved.");
    } */
}

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   www .  ja v a2 s . c  o m
            //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) {/*  ww  w . j  a va  2s  .  co  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:myclassifier.wekaCode.java

public static void foldValidation(Instances dataSet, Classifier classifiers) throws Exception {
    Evaluation evaluation = new Evaluation(dataSet);
    evaluation.crossValidateModel(classifiers, dataSet, 10, new Random(1)); //Evaluates the classifier on a given set of instances.
    System.out.println(evaluation.toSummaryString("\n 10-fold cross validation", false));
    System.out.println(evaluation.toMatrixString("\n Confusion Matrix"));

}

From source file:textmining.TextMining.java

/**
 * Decision Table/*from   w  ww  .  j  a  v a 2 s  .  c o  m*/
 *
 * @param instances
 * @return string
 * @throws Exception
 */
private static String C_DecisionTable(Instances instances) throws Exception {
    Classifier decisionTable = (Classifier) new DecisionTable();
    String[] options = weka.core.Utils.splitOptions("-X 1 -S \"weka.attributeSelection.BestFirst -D 1 -N 5\"");
    decisionTable.setOptions(options);
    decisionTable.buildClassifier(instances);
    Evaluation eval = new Evaluation(instances);
    //        eval.evaluateModel(decisionTable, instances);
    eval.crossValidateModel(decisionTable, instances, 5, new Random(1));
    String resume = eval.toSummaryString();

    return eval.toMatrixString(resume);
}

From source file:textmining.TextMining.java

private static String setOptions(Classifier classifier, Instances instances, String[] options)
        throws Exception {
    classifier.setOptions(options);//w ww .j a v a2s.  c o m
    classifier.buildClassifier(instances);
    Evaluation eval = new Evaluation(instances);
    eval.crossValidateModel(classifier, instances, 5, new Random(1));
    eval.evaluateModel(classifier, instances);
    String resume = eval.toSummaryString();
    return eval.toMatrixString(resume);
}

From source file:tucil.dua.ai.TucilDuaAi.java

public static void fullTrainingSet() throws Exception {
    Classifier j48 = new J48();
    j48.buildClassifier(datas);//from  w  w  w  .j  a  va  2  s .co m

    Evaluation eval = new Evaluation(datas);
    eval.evaluateModel(j48, datas);
    System.out.println("=====Run Information======");
    System.out.println("======Classifier Model======");
    System.out.println(j48.toString());
    System.out.println(eval.toSummaryString("====Stats======\n", false));
    System.out.println(eval.toClassDetailsString("====Detailed Result=====\n"));
    System.out.println(eval.toMatrixString("======Confusion Matrix======\n"));
}

From source file:tucil.dua.ai.TucilDuaAi.java

public static void crossValidation() throws Exception {
    Evaluation evaluation = new Evaluation(datas);
    Classifier attr_tree = new J48();
    attr_tree.buildClassifier(datas);// ww w  . j  av a 2  s  .  c om
    evaluation.crossValidateModel(attr_tree, datas, 10, new Random(1));
    System.out.println("=====Run Information======");
    System.out.println("======Classifier Model======");
    System.out.println(attr_tree.toString());
    System.out.println(evaluation.toSummaryString("====Stats======\n", false));
    System.out.println(evaluation.toClassDetailsString("====Detailed Result=====\n"));
    System.out.println(evaluation.toMatrixString("======Confusion Matrix======\n"));
}