Example usage for weka.classifiers Evaluation toSummaryString

List of usage examples for weka.classifiers Evaluation toSummaryString

Introduction

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

Prototype

@Override
public String toSummaryString() 

Source Link

Document

Calls toSummaryString() with no title and no complexity stats.

Usage

From source file:FlexDMThread.java

License:Open Source License

public void run() {
    try {/*from   w w w .  ja  v a2  s .  c  o m*/
        //Get the data from the source

        FlexDM.getMainData.acquire();
        Instances data = dataset.getSource().getDataSet();
        FlexDM.getMainData.release();

        //Set class attribute if undefined
        if (data.classIndex() == -1) {
            data.setClassIndex(data.numAttributes() - 1);
        }

        //Process hyperparameters for classifier
        String temp = "";
        for (int i = 0; i < classifier.getNumParams(); i++) {
            temp += classifier.getParameter(i).getName();
            temp += " ";
            if (classifier.getParameter(i).getValue() != null) {
                temp += classifier.getParameter(i).getValue();
                temp += " ";
            }
        }

        String[] options = weka.core.Utils.splitOptions(temp);

        //Print to console- experiment is starting
        if (temp.equals("")) { //no parameters
            temp = "results_no_parameters";
            try {
                System.out.println("STARTING CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset "
                        + dataset.getName().substring(dataset.getName().lastIndexOf("\\") + 1)
                        + " with no parameters");
            } catch (Exception e) {
                System.out.println("STARTING CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset "
                        + dataset.getName() + " with no parameters");
            }
        } else { //parameters
            try {
                System.out.println("STARTING CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset "
                        + dataset.getName().substring(dataset.getName().lastIndexOf("\\") + 1)
                        + " with parameters " + temp);
            } catch (Exception e) {
                System.out.println("STARTING CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset "
                        + dataset.getName() + " with parameters " + temp);
            }
        }

        //Create classifier, setting parameters
        weka.classifiers.Classifier x = createObject(classifier.getName());
        x.setOptions(options);
        x.buildClassifier(data);

        //Process the test selection
        String[] tempTest = dataset.getTest().split("\\s");

        //Create evaluation object for training and testing classifiers
        Evaluation eval = new Evaluation(data);
        StringBuffer predictions = new StringBuffer();

        //Train and evaluate classifier
        if (tempTest[0].equals("testset")) { //specified test file
            //Build classifier
            x.buildClassifier(data);

            //Open test file, load data
            //DataSource testFile = new DataSource(dataset.getTest().substring(7).trim());
            // Instances testSet = testFile.getDataSet();
            FlexDM.getTestData.acquire();
            Instances testSet = dataset.getTestFile().getDataSet();
            FlexDM.getTestData.release();

            //Set class attribute if undefined
            if (testSet.classIndex() == -1) {
                testSet.setClassIndex(testSet.numAttributes() - 1);
            }

            //Evaluate model
            Object[] array = { predictions, new Range(), new Boolean(true) };
            eval.evaluateModel(x, testSet, array);
        } else if (tempTest[0].equals("xval")) { //Cross validation
            //Build classifier
            x.buildClassifier(data);

            //Cross validate
            eval.crossValidateModel(x, data, Integer.parseInt(tempTest[1]), new Random(1), predictions,
                    new Range(), true);
        } else if (tempTest[0].equals("leavexval")) { //Leave one out cross validation
            //Build classifier
            x.buildClassifier(data);

            //Cross validate
            eval.crossValidateModel(x, data, data.numInstances() - 1, new Random(1), predictions, new Range(),
                    true);
        } else if (tempTest[0].equals("percent")) { //Percentage split of single data set
            //Set training and test sizes from percentage
            int trainSize = (int) Math.round(data.numInstances() * Double.parseDouble(tempTest[1]));
            int testSize = data.numInstances() - trainSize;

            //Load specified data
            Instances train = new Instances(data, 0, trainSize);
            Instances testSet = new Instances(data, trainSize, testSize);

            //Build classifier
            x.buildClassifier(train);

            //Train and evaluate model
            Object[] array = { predictions, new Range(), new Boolean(true) };
            eval.evaluateModel(x, testSet, array);
        } else { //Evaluate on training data
            //Test and evaluate model
            Object[] array = { predictions, new Range(), new Boolean(true) };
            eval.evaluateModel(x, data, array);
        }

        //create datafile for results
        String filename = dataset.getDir() + "/" + classifier.getDirName() + "/" + temp + ".txt";
        PrintWriter writer = new PrintWriter(filename, "UTF-8");

        //Print classifier, dataset, parameters info to file
        try {
            writer.println("CLASSIFIER: " + classifier.getName() + "\n DATASET: " + dataset.getName()
                    + "\n PARAMETERS: " + temp);
        } catch (Exception e) {
            writer.println("CLASSIFIER: " + classifier.getName() + "\n DATASET: " + dataset.getName()
                    + "\n PARAMETERS: " + temp);
        }

        //Add evaluation string to file
        writer.println(eval.toSummaryString());
        //Process result options
        if (checkResults("stats")) { //Classifier statistics
            writer.println(eval.toClassDetailsString());
        }
        if (checkResults("model")) { //The model
            writer.println(x.toString());
        }
        if (checkResults("matrix")) { //Confusion matrix
            writer.println(eval.toMatrixString());
        }
        if (checkResults("entropy")) { //Entropy statistics
            //Set options req'd to get the entropy stats
            String[] opt = new String[4];
            opt[0] = "-t";
            opt[1] = dataset.getName();
            opt[2] = "-k";
            opt[3] = "-v";

            //Evaluate model
            String entropy = Evaluation.evaluateModel(x, opt);

            //Grab the relevant info from the results, print to file
            entropy = entropy.substring(entropy.indexOf("=== Stratified cross-validation ===") + 35,
                    entropy.indexOf("=== Confusion Matrix ==="));
            writer.println("=== Entropy Statistics ===");
            writer.println(entropy);
        }
        if (checkResults("predictions")) { //The models predictions
            writer.println("=== Predictions ===\n");
            if (!dataset.getTest().contains("xval")) { //print header of predictions table if req'd
                writer.println(" inst#     actual  predicted error distribution ()");
            }
            writer.println(predictions.toString()); //print predictions to file
        }

        writer.close();

        //Summary file is semaphore controlled to ensure quality
        try { //get a permit
              //grab the summary file, write the classifiers details to it
            FlexDM.writeFile.acquire();
            PrintWriter p = new PrintWriter(new FileWriter(summary, true));
            if (temp.equals("results_no_parameters")) { //change output based on parameters
                temp = temp.substring(8);
            }

            //write percent correct, classifier name, dataset name to summary file
            p.write(dataset.getName() + ", " + classifier.getName() + ", " + temp + ", " + eval.correct() + ", "
                    + eval.incorrect() + ", " + eval.unclassified() + ", " + eval.pctCorrect() + ", "
                    + eval.pctIncorrect() + ", " + eval.pctUnclassified() + ", " + eval.kappa() + ", "
                    + eval.meanAbsoluteError() + ", " + eval.rootMeanSquaredError() + ", "
                    + eval.relativeAbsoluteError() + ", " + eval.rootRelativeSquaredError() + ", "
                    + eval.SFPriorEntropy() + ", " + eval.SFSchemeEntropy() + ", " + eval.SFEntropyGain() + ", "
                    + eval.SFMeanPriorEntropy() + ", " + eval.SFMeanSchemeEntropy() + ", "
                    + eval.SFMeanEntropyGain() + ", " + eval.KBInformation() + ", " + eval.KBMeanInformation()
                    + ", " + eval.KBRelativeInformation() + ", " + eval.weightedTruePositiveRate() + ", "
                    + eval.weightedFalsePositiveRate() + ", " + eval.weightedTrueNegativeRate() + ", "
                    + eval.weightedFalseNegativeRate() + ", " + eval.weightedPrecision() + ", "
                    + eval.weightedRecall() + ", " + eval.weightedFMeasure() + ", "
                    + eval.weightedAreaUnderROC() + "\n");
            p.close();

            //release semaphore
            FlexDM.writeFile.release();
        } catch (InterruptedException e) { //bad things happened
            System.err.println("FATAL ERROR OCCURRED: Classifier: " + cNum + " - " + classifier.getName()
                    + " on dataset " + dataset.getName());
        }

        //output we have successfully finished processing classifier
        if (temp.equals("no_parameters")) { //no parameters
            try {
                System.out.println("FINISHED CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset "
                        + dataset.getName().substring(dataset.getName().lastIndexOf("\\") + 1)
                        + " with no parameters");
            } catch (Exception e) {
                System.out.println("FINISHED CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset "
                        + dataset.getName() + " with no parameters");
            }
        } else { //with parameters
            try {
                System.out.println("FINISHED CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset "
                        + dataset.getName().substring(dataset.getName().lastIndexOf("\\") + 1)
                        + " with parameters " + temp);
            } catch (Exception e) {
                System.out.println("FINISHED CLASSIFIER " + cNum + " - " + classifier.getName() + " on dataset "
                        + dataset.getName() + " with parameters " + temp);
            }
        }

        try { //get a permit
              //grab the log file, write the classifiers details to it
            FlexDM.writeLog.acquire();
            PrintWriter p = new PrintWriter(new FileWriter(log, true));

            Date date = new Date();
            Format formatter = new SimpleDateFormat("dd/MM/YYYY HH:mm:ss");
            //formatter.format(date)

            if (temp.equals("results_no_parameters")) { //change output based on parameters
                temp = temp.substring(8);
            }

            //write details to log file
            p.write(dataset.getName() + ", " + dataset.getTest() + ", \"" + dataset.getResult_string() + "\", "
                    + classifier.getName() + ", " + temp + ", " + formatter.format(date) + "\n");
            p.close();

            //release semaphore
            FlexDM.writeLog.release();
        } catch (InterruptedException e) { //bad things happened
            System.err.println("FATAL ERROR OCCURRED: Classifier: " + cNum + " - " + classifier.getName()
                    + " on dataset " + dataset.getName());
        }

        s.release();

    } catch (Exception e) {
        //an error occurred
        System.err.println("FATAL ERROR OCCURRED: " + e.toString() + "\nClassifier: " + cNum + " - "
                + classifier.getName() + " on dataset " + dataset.getName());
        s.release();
    }

}

From source file:ClassificationClass.java

public Evaluation cls_naivebayes(Instances data) {
    Evaluation eval = null;
    try {//from  w  ww  .  jav a 2 s .co  m
        Classifier classifier;
        PreparingSteps preparingSteps = new PreparingSteps();
        data.setClassIndex(data.numAttributes() - 1);
        classifier = new NaiveBayes();
        classifier.buildClassifier(data);
        eval = new Evaluation(data);
        eval.evaluateModel(classifier, data);

        System.out.println(eval.toSummaryString());
    } catch (Exception ex) {
        Logger.getLogger(ClassificationClass.class.getName()).log(Level.SEVERE, null, ex);
    }
    return eval;
}

From source file:ClassificationClass.java

public Evaluation cls_c4_5(Instances data) {
    Evaluation eval = null;
    try {//from w  w w . j  a v  a 2  s.  c  o m
        Classifier classifier;
        PreparingSteps preparingSteps = new PreparingSteps();
        data.setClassIndex(data.numAttributes() - 1);
        classifier = new J48();
        classifier.buildClassifier(data);
        eval = new Evaluation(data);
        eval.evaluateModel(classifier, data);

        System.out.println(eval.toSummaryString());
    } catch (Exception ex) {
        Logger.getLogger(ClassificationClass.class.getName()).log(Level.SEVERE, null, ex);
    }
    return eval;
}

From source file:CopiaSeg3.java

public static Evaluation simpleClassify(Classifier model, Instances trainingSet, Instances testingSet)
        throws Exception {
    Evaluation validation = new Evaluation(trainingSet);

    model.buildClassifier(trainingSet);/*  w w  w.  j a v  a2  s .c o  m*/
    validation.evaluateModel(model, testingSet);

    // Imprime el resultado de Weka explorer:
    String strSummary = validation.toSummaryString();
    System.out.println(strSummary);

    return validation;
}

From source file:MainFrame.java

private void svm_buttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_svm_buttonMouseClicked
    ClassificationClass classificationClass = new ClassificationClass();
    PreparingSteps pr = new PreparingSteps();
    Instances data = pr.getReadFileData(path);
    data = pr.deleteattributefromData(data, this.getfsresults());
    Evaluation ev = null;
    ev = classificationClass.cls_svm(data);
    classification_result_ta.setText("SVM RESULTS:\n" + ev.toSummaryString());
}

From source file:MainFrame.java

private void knn_buttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_knn_buttonMouseClicked
    ClassificationClass classificationClass = new ClassificationClass();
    PreparingSteps pr = new PreparingSteps();
    Instances data = pr.getReadFileData(path);
    data = pr.deleteattributefromData(data, this.getfsresults());
    Evaluation ev = null;
    ev = classificationClass.cls_knn(data);
    classification_result_ta.setText("KNN RESULTS:\n" + ev.toSummaryString());
}

From source file:MainFrame.java

private void naivebayesbuttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_naivebayesbuttonMouseClicked
    ClassificationClass classificationClass = new ClassificationClass();
    PreparingSteps pr = new PreparingSteps();
    Instances data = pr.getReadFileData(path);
    data = pr.deleteattributefromData(data, this.getfsresults());
    Evaluation ev = null;
    ev = classificationClass.cls_naivebayes(data);
    classification_result_ta.setText("NAIVE BAYES RESULTS:\n" + ev.toSummaryString());
}

From source file:MainFrame.java

private void c45buttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_c45buttonMouseClicked
    ClassificationClass classificationClass = new ClassificationClass();
    PreparingSteps pr = new PreparingSteps();
    Instances data = pr.getReadFileData(path);
    data = pr.deleteattributefromData(data, this.getfsresults());
    Evaluation ev = null;
    ev = classificationClass.cls_c4_5(data);
    classification_result_ta.setText("C4.5 RESULTS\n" + ev.toSummaryString());
}

From source file:activeSegmentation.learning.WekaClassifier.java

License:Open Source License

/**
 * Evaluates the classifier using the test dataset and stores the evaluation.
 *
 * @param instances The instances to test
 * @return The evaluation//from   w ww. j a  va2s  . c  o  m
 */
@Override
public double[] testModel(IDataSet instances) {

    try {

        // test the current classifier with the test set
        Evaluation evaluator = new Evaluation(new Instances(instances.getDataset(), 0));

        double[] predict = evaluator.evaluateModel(classifier, instances.getDataset());

        System.out.println(evaluator.toSummaryString());
        return predict;

    } catch (Exception e) {
        Logger.getLogger(WekaClassifier.class.getName()).log(Level.SEVERE, null, e);
    }

    return null;
}

From source file:ANN.MultilayerPerceptron.java

public static void main(String[] args) throws Exception {
    ConverterUtils.DataSource source = new ConverterUtils.DataSource(
            ("D:\\Program Files\\Weka-3-8\\data\\iris.arff"));
    Instances train = source.getDataSet();
    Normalize nm = new Normalize();
    nm.setInputFormat(train);// ww w  . j a  v  a  2 s.  c o m
    train = Filter.useFilter(train, nm);
    train.setClassIndex(train.numAttributes() - 1);
    System.out.println();
    //                System.out.println(i + " "+0.8);
    MultilayerPerceptron slp = new MultilayerPerceptron(train, 0.1, 5000, 14);
    slp.buildClassifier(train);
    Evaluation eval = new Evaluation(train);
    eval.evaluateModel(slp, train);
    System.out.println(eval.toSummaryString());
    System.out.print(eval.toMatrixString());
}