Example usage for weka.classifiers Evaluation Evaluation

List of usage examples for weka.classifiers Evaluation Evaluation

Introduction

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

Prototype

public Evaluation(Instances data) throws Exception 

Source Link

Usage

From source file:wekimini.learning.ModelEvaluator.java

public void evaluateAll(final List<Path> paths, final boolean isTraining, final int numFolds,
        PropertyChangeListener listener) {
    final List<Instances> data = new LinkedList<>();
    for (Path p : paths) {
        Instances i = w.getSupervisedLearningManager().getTrainingDataForPath(p, false);
        data.add(i);//w  w w  .j  a v  a2s .  c  o  m
    }

    setResults(new String[paths.size()]);
    if (evalWorker != null && evalWorker.getState() != SwingWorker.StateValue.DONE) {
        return;
    }

    evalWorker = new SwingWorker<Integer, Void>() {

        //trainingWorker.
        @Override
        public Integer doInBackground() {
            // train(); //TODO: Add status updates
            int progress = 0;
            //setProgress(progress);
            int numToEvaluate = 0;
            for (Path p : paths) {
                if (p.canBuild()) {
                    numToEvaluate++;
                }
            }

            int numEvaluated = 0;
            int numErr = 0;
            setEvalStatus(new EvaluationStatus(numToEvaluate, numEvaluated, numErr, false));

            for (int i = 0; i < paths.size(); i++) {
                Path p = paths.get(i);
                if (p.canBuild()) {
                    try {
                        System.out.println("Evaluating with " + numFolds);
                        //EVALUATE HERE: TODO 
                        Instances instances = w.getSupervisedLearningManager().getTrainingDataForPath(p, false);
                        Evaluation eval = new Evaluation(instances);
                        Classifier c = ((LearningModelBuilder) p.getModelBuilder()).getClassifier();
                        if (!isTraining) {
                            Random r = new Random();
                            eval.crossValidateModel(c, instances, numFolds, r);
                        } else {
                            Classifier c2 = Classifier.makeCopy(c);
                            c2.buildClassifier(instances);
                            eval.evaluateModel(c2, instances);
                        }
                        String result;
                        if (p.getModelBuilder() instanceof ClassificationModelBuilder) {
                            result = dFormat.format(eval.pctCorrect()) + "%"; //WON"T WORK FOR NN
                        } else {
                            result = dFormat.format(eval.errorRate()) + " (RMS)";
                        }
                        if (!isTraining) {
                            KadenzeLogging.getLogger().crossValidationComputed(w, i, numFolds, result);
                        } else {
                            KadenzeLogging.getLogger().trainingAccuracyComputed(w, i, result);
                        }
                        setResults(i, result);
                        finishedModel(i, result);
                        numEvaluated++;

                        if (isCancelled()) {
                            cancelMe(p);
                            setResults(i, "Cancelled");
                            return 0;
                        }

                    } catch (InterruptedException ex) {
                        cancelMe(p);
                        setResults(i, "Cancelled");
                        return 0; //Not sure this will be called...
                    } catch (Exception ex) {
                        numErr++;
                        Util.showPrettyErrorPane(null, "Error encountered during evaluation "
                                + p.getCurrentModelName() + ": " + ex.getMessage());
                        logger.log(Level.SEVERE, ex.getMessage());
                    }
                    setEvalStatus(new EvaluationStatus(numToEvaluate, numEvaluated, numErr, false));
                } else {
                    logger.log(Level.WARNING, "Could not evaluate path");
                }

            }
            wasCancelled = false;
            hadError = evaluationStatus.numErrorsEncountered > 0;
            return 0;
        }

        @Override
        public void done() {
            if (isCancelled()) {
                EvaluationStatus t = new EvaluationStatus(evaluationStatus.numToEvaluate,
                        evaluationStatus.numEvaluated, evaluationStatus.numErrorsEncountered, true);
                setEvalStatus(t);
            }
            finished();
        }
    };
    evalWorker.addPropertyChangeListener(listener);
    evalWorker.execute();
}