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:org.dkpro.similarity.algorithms.ml.ClassifierSimilarityMeasure.java

License:Open Source License

public ClassifierSimilarityMeasure(WekaClassifier classifier, File trainArff, File testArff) throws Exception {
    CLASSIFIER = getClassifier(classifier);

    // Get all instances
    Instances train = getTrainInstances(trainArff);
    test = getTestInstances(testArff);//from   w  w w  .  ja v  a 2  s .  com

    // Apply log filter
    Filter logFilter = new LogFilter();
    logFilter.setInputFormat(train);
    train = Filter.useFilter(train, logFilter);
    logFilter.setInputFormat(test);
    test = Filter.useFilter(test, logFilter);

    Classifier clsCopy;
    try {
        // Copy the classifier
        clsCopy = AbstractClassifier.makeCopy(CLASSIFIER);

        // Build the classifier
        filteredClassifier = clsCopy;
        filteredClassifier.buildClassifier(train);

        Evaluation eval = new Evaluation(train);
        eval.evaluateModel(filteredClassifier, test);

        System.out.println(eval.toSummaryString());
        System.out.println(eval.toMatrixString());
    } catch (Exception e) {
        throw new SimilarityException(e);
    }
}

From source file:org.processmining.analysis.clusteranalysis.DecisionAnalyzer.java

License:Open Source License

/**
 * Creates an evaluation overview of the built classifier.
 * /* w  w  w  .  j av  a  2s  .  com*/
 * @return the panel to be displayed as result evaluation view for the
 *         current decision point
 */
protected JPanel createEvaluationVisualization(Instances data) {
    // build text field to display evaluation statistics
    JTextPane statistic = new JTextPane();

    try {
        // build evaluation statistics
        Evaluation evaluation = new Evaluation(data);
        evaluation.evaluateModel(myClassifier, data);
        statistic.setText(evaluation.toSummaryString() + "\n\n" + evaluation.toClassDetailsString() + "\n\n"
                + evaluation.toMatrixString());

    } catch (Exception ex) {
        ex.printStackTrace();
        return createMessagePanel("Error while creating the decision tree evaluation view");
    }

    statistic.setFont(new Font("Courier", Font.PLAIN, 14));
    statistic.setEditable(false);
    statistic.setCaretPosition(0);

    JPanel resultViewPanel = new JPanel();
    resultViewPanel.setLayout(new BoxLayout(resultViewPanel, BoxLayout.PAGE_AXIS));
    resultViewPanel.add(new JScrollPane(statistic));

    return resultViewPanel;
}

From source file:PEBL.TwoStep.java

public static void main(String[] args) throws Exception {

    ConverterUtils.DataSource source = new ConverterUtils.DataSource(
            "Z:\\\\shared from vm\\\\fourthset\\\\mixed.csv");

    Instances data = source.getDataSet();

    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }//from w  ww . j av  a2  s .c  o m

    NumericToNominal nmf = new NumericToNominal();
    nmf.setInputFormat(data);
    data = Filter.useFilter(data, nmf);

    // build a c4.5 classifier
    String[] options = new String[1];
    // options[0] = "-C 0.25 -M 2";            // unpruned tree
    options[0] = "-K";
    NaiveBayes c = new NaiveBayes(); // new instance of tree
    c.setOptions(options); // set the options
    c.buildClassifier(data); // build classifier

    // eval
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(c, data, 10, new Random(1));
    System.out.println(eval.toSummaryString());
    System.out.println(eval.toMatrixString());
    System.out.println(eval.toClassDetailsString());
    System.out.println("--- model learned on mixed set ---");

    // load unlabeled data
    ConverterUtils.DataSource s = new ConverterUtils.DataSource(
            "Z:\\\\shared from vm\\\\fourthset\\\\unlabelled.csv");
    Instances unlabeled = s.getDataSet();
    // set class attribute
    unlabeled.setClassIndex(unlabeled.numAttributes() - 1);

    nmf = new NumericToNominal();
    nmf.setInputFormat(unlabeled);
    unlabeled = Filter.useFilter(unlabeled, nmf);

    // label instances
    for (int i = 0; i < unlabeled.numInstances(); i++) {
        double classZero = c.distributionForInstance(unlabeled.instance(i))[0];
        double classOne = c.distributionForInstance(unlabeled.instance(i))[1];
        System.out.print(
                "classifying: " + unlabeled.instance(i) + " : " + classZero + " - " + classOne + " == class: ");
        if (classZero > classOne) {
            System.out.print("0");
            unlabeled.instance(i).setClassValue("0");
        } else {
            System.out.print("1");
            unlabeled.instance(i).setClassValue("1");
        }
        System.out.println("");
    }

    // save labeled data
    // BufferedWriter writer = new BufferedWriter(
    //         new FileWriter("Z:\\\\shared from vm\\\\thirdset\\\\relabelled.arff"));
    // writer.write(labeled.toString());
    // writer.newLine();
    // writer.flush();
    // writer.close();
    ArffSaver saver = new ArffSaver();
    saver.setInstances(unlabeled);
    saver.setFile(new File("Z:\\shared from vm\\thirdset\\relabelled.arff"));
    //        saver.setDestination(new File("Z:\\shared from vm\\thirdset\\relabelled.arff"));   // **not** necessary in 3.5.4 and later
    saver.writeBatch();

}

From source file:PointAnalyser.Main.java

public static void trainC45Classifier() throws Exception {

    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }//from w  ww. j  a  va 2 s. co m

    NumericToNominal nmf = new NumericToNominal();
    nmf.setInputFormat(data);
    data = Filter.useFilter(data, nmf);

    // build a c4.5 classifier
    String[] options = new String[1];
    options[0] = "-C 0.25 -M 2 -U"; // unpruned tree
    tree = new J48(); // new instance of tree
    tree.setOptions(options); // set the options
    tree.buildClassifier(data); // build classifier
    /*
             RemoveMisclassified rm = new RemoveMisclassified();
             rm.setInputFormat(data);
             rm.setClassifier(tree);
             rm.setNumFolds(10);
             rm.setThreshold(0.1);
             rm.setMaxIterations(0);
             data = Filter.useFilter(data, rm);
            
             tree = new J48();         // new instance of tree
             tree.setOptions(options);     // set the options
             tree.buildClassifier(data);   // build classifier
             */
    // eval
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(tree, data, 10, new Random(1));

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

}

From source file:PointAnalyser.Main.java

public static void trainNNClassifier() throws Exception {

    // setting class attribute if the data format does not provide this information
    // For example, the XRFF format saves the class attribute information as well
    if (data.classIndex() == -1) {
        data.setClassIndex(data.numAttributes() - 1);
    }/*  w  ww  .j av a  2s. co  m*/

    NumericToNominal nmf = new NumericToNominal();
    nmf.setInputFormat(data);
    data = Filter.useFilter(data, nmf);

    // build a c4.5 classifier
    String[] options = new String[1];
    // options[0] = "-K 1";            // unpruned tree
    nn = new IBk(); // new instance of tree
    //  nn.setCrossValidate(true);
    nn.setKNN(7);
    nn.setNearestNeighbourSearchAlgorithm(new weka.core.neighboursearch.KDTree(data));

    nn.setWindowSize(0);

    // nn.setOptions(options);     // set the options
    nn.buildClassifier(data); // build classifier
    // eval
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(nn, data, 10, new Random(1));

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

}

From source file:predictor.Predictor.java

public static void multilayerPerceptron() throws Exception {

    DataSource train = new DataSource(configuration.getWorkspace() + "train_common.arff");
    DataSource test = new DataSource(configuration.getWorkspace() + "test_common.arff");

    Instances trainInstances = train.getDataSet();
    Instances testInstances = test.getDataSet();

    //last attribute classify
    trainInstances.setClassIndex(trainInstances.numAttributes() - 1);
    testInstances.setClassIndex(testInstances.numAttributes() - 1);
    //        /*from ww w . j ava 2 s . c  o m*/
    //        Classifier cModel = (Classifier)new MultilayerPerceptron();  
    //        cModel.buildClassifier(trainInstances);  
    //
    //        weka.core.SerializationHelper.write("/some/where/nBayes.model", cModel);
    //
    //        Classifier cls = (Classifier) weka.core.SerializationHelper.read("/some/where/nBayes.model");
    //
    //        // Test the model
    //        Evaluation eTest = new Evaluation(trainInstances);
    //        eTest.evaluateModel(cls, testInstances);

    MultilayerPerceptron mlp = new MultilayerPerceptron();
    mlp.buildClassifier(trainInstances);
    mlp.setHiddenLayers(configuration.getHiddenLayers());
    mlp.setLearningRate(configuration.getLearningRate());
    mlp.setTrainingTime(configuration.getEpocs());
    mlp.setMomentum(configuration.getMomentum());

    // train classifier
    Classifier cls = new MultilayerPerceptron();
    cls.buildClassifier(trainInstances);

    // evaluate classifier and print some statistics
    Evaluation eval = new Evaluation(trainInstances);
    eval.evaluateModel(cls, testInstances);

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

From source file:sentinets.Prediction.java

License:Open Source License

public String updateModel(String inputFile, ArrayList<Double[]> metrics) {
    String output = "";
    this.setInstances(inputFile);
    FilteredClassifier fcls = (FilteredClassifier) this.cls;
    SGD cls = (SGD) fcls.getClassifier();
    Filter filter = fcls.getFilter();
    Instances insAll;/*  ww w  . j a v a  2 s.  c o m*/
    try {
        insAll = Filter.useFilter(this.unlabled, filter);
        if (insAll.size() > 0) {
            Random rand = new Random(10);
            int folds = 10 > insAll.size() ? 2 : 10;
            Instances randData = new Instances(insAll);
            randData.randomize(rand);
            if (randData.classAttribute().isNominal()) {
                randData.stratify(folds);
            }
            Evaluation eval = new Evaluation(randData);
            eval.evaluateModel(cls, insAll);
            System.out.println("Initial Evaluation");
            System.out.println(eval.toSummaryString());
            System.out.println(eval.toClassDetailsString());
            metrics.add(new Double[] { eval.fMeasure(0), eval.fMeasure(1), eval.weightedFMeasure() });
            output += "\n====" + "Initial Evaluation" + "====\n";
            output += "\n" + eval.toSummaryString();
            output += "\n" + eval.toClassDetailsString();
            System.out.println("Cross Validated Evaluation");
            output += "\n====" + "Cross Validated Evaluation" + "====\n";
            for (int n = 0; n < folds; n++) {
                Instances train = randData.trainCV(folds, n);
                Instances test = randData.testCV(folds, n);

                for (int i = 0; i < train.numInstances(); i++) {
                    cls.updateClassifier(train.instance(i));
                }

                eval.evaluateModel(cls, test);
                System.out.println("Cross Validated Evaluation fold: " + n);
                output += "\n====" + "Cross Validated Evaluation fold (" + n + ")====\n";
                System.out.println(eval.toSummaryString());
                System.out.println(eval.toClassDetailsString());
                output += "\n" + eval.toSummaryString();
                output += "\n" + eval.toClassDetailsString();
                metrics.add(new Double[] { eval.fMeasure(0), eval.fMeasure(1), eval.weightedFMeasure() });
            }
            for (int i = 0; i < insAll.numInstances(); i++) {
                cls.updateClassifier(insAll.instance(i));
            }
            eval.evaluateModel(cls, insAll);
            System.out.println("Final Evaluation");
            System.out.println(eval.toSummaryString());
            System.out.println(eval.toClassDetailsString());
            output += "\n====" + "Final Evaluation" + "====\n";
            output += "\n" + eval.toSummaryString();
            output += "\n" + eval.toClassDetailsString();
            metrics.add(new Double[] { eval.fMeasure(0), eval.fMeasure(1), eval.weightedFMeasure() });
            fcls.setClassifier(cls);
            String modelFilePath = outputDir + "/" + Utils.getOutDir(Utils.OutDirIndex.MODELS)
                    + "/updatedClassifier.model";
            weka.core.SerializationHelper.write(modelFilePath, fcls);
            output += "\n" + "Updated Model saved at: " + modelFilePath;
        } else {
            output += "No new instances for training the model.";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return output;
}

From source file:sentinets.TrainModel.java

License:Open Source License

public void trainModel(Classifier c, String name) {
    Evaluation e;
    try {//from  w ww .  j a va  2s . c  o m
        e = new Evaluation(ins);
        e.crossValidateModel(c, ins, 10, new Random(1));
        System.out.println("****Results of " + name + "****");
        System.out.println(e.toSummaryString());
        System.out.println(e.toClassDetailsString());
        System.out.println(e.toCumulativeMarginDistributionString());
        System.out.println(e.toMatrixString());
        System.out.println("*********************");
        TrainModel.saveModel(c, name);
    } catch (Exception e1) {
        e1.printStackTrace();
    }

}

From source file:soccer.core.SimpleClassifier.java

public void evaluate() throws IOException, Exception {
    Instances data = loader.buildInstances();
    NumericToNominal toNominal = new NumericToNominal();
    toNominal.setOptions(new String[] { "-R", "5,6,8,9" });
    toNominal.setInputFormat(data);/*from   ww  w .j a  va 2  s.  co m*/
    data = Filter.useFilter(data, toNominal);
    data.setClassIndex(6);

    //        DataSink.write(ARFF_STRING, data);

    EnsembleLibrary ensembleLib = new EnsembleLibrary();
    ensembleLib.addModel("weka.classifiers.trees.J48");
    ensembleLib.addModel("weka.classifiers.bayes.NaiveBayes");
    ensembleLib.addModel("weka.classifiers.functions.SMO");
    ensembleLib.addModel("weka.classifiers.meta.AdaBoostM1");
    ensembleLib.addModel("weka.classifiers.meta.LogitBoost");
    ensembleLib.addModel("classifiers.trees.DecisionStump");
    ensembleLib.addModel("classifiers.trees.DecisionStump");
    EnsembleLibrary.saveLibrary(new File("./ensembleLib.model.xml"), ensembleLib, null);
    EnsembleSelection model = new EnsembleSelection();
    model.setOptions(new String[] { "-L", "./ensembleLib.model.xml", // </path/to/modelLibrary>"-W", path+"esTmp", // </path/to/working/directory> - 
            "-B", "10", // <numModelBags> 
            "-E", "1.0", // <modelRatio>.
            "-V", "0.25", // <validationRatio>
            "-H", "100", // <hillClimbIterations> 
            "-I", "1.0", // <sortInitialization> 
            "-X", "2", // <numFolds>
            "-P", "roc", // <hillclimbMettric>
            "-A", "forward", // <algorithm> 
            "-R", "true", // - Flag to be selected more than once
            "-G", "true", // - stops adding models when performance degrades
            "-O", "true", // - verbose output.
            "-S", "1", // <num> - Random number seed.
            "-D", "true" // - run in debug mode 
    });
    //        double resES[] = evaluate(ensambleSel);
    //        System.out.println("Ensemble Selection\n"
    //                + "\tchurn:     " + resES[0] + "\n"
    //                + "\tappetency: " + resES[1] + "\n"
    //                + "\tup-sell:   " + resES[2] + "\n"
    //                + "\toverall:   " + resES[3] + "\n");
    //        models.add(new J48());
    //        models.add(new RandomForest());
    //        models.add(new NaiveBayes());
    //        models.add(new AdaBoostM1());
    //        models.add(new Logistic());
    //        models.add(new MultilayerPerceptron());

    int FOLDS = 5;
    Evaluation eval = new Evaluation(data);
    //
    //        for (Classifier model : models) {
    eval.crossValidateModel(model, data, FOLDS, new Random(1), new Object[] {});
    System.out.println(model.getClass().getName() + "\n" + "\tRecall:    " + eval.recall(1) + "\n"
            + "\tPrecision: " + eval.precision(1) + "\n" + "\tF-measure: " + eval.fMeasure(1));
    System.out.println(eval.toSummaryString());
    //        }
    //        LogitBoost cl = new LogitBoost();
    //        cl.setOptions(new String[] {
    //            "-Q", "-I", "100", "-Z", "4", "-O", "4", "-E", "4"
    //        });
    //        cl.buildClassifier(data);
    //        Evaluation eval = new Evaluation(data);
    //        eval.crossValidateModel(cl, data, 6, new Random(1), new Object[]{});
    //        System.out.println(eval.weightedFMeasure());
    //        System.out.println(cl.graph());
    //        System.out.println(cl.globalInfo());

}

From source file:statistics.BinaryStatisticsEvaluator.java

@Override
public double[][] getConfusionMatrix(Instances Training_Instances, Instances Testing_Instances,
        String classifier) {//from   w  w w.jav  a2 s  . co 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;
}