Example usage for weka.classifiers.evaluation EvaluationUtils getCVPredictions

List of usage examples for weka.classifiers.evaluation EvaluationUtils getCVPredictions

Introduction

In this page you can find the example usage for weka.classifiers.evaluation EvaluationUtils getCVPredictions.

Prototype

public ArrayList<Prediction> getCVPredictions(Classifier classifier, Instances data, int numFolds)
        throws Exception 

Source Link

Document

Generate a bunch of predictions ready for processing, by performing a cross-validation on the supplied dataset.

Usage

From source file:machinelearningcw.MachineLearningCw.java

public static void compareAlgorithms(Instances data) throws Exception {
    System.out.println("Compare Algorithms");
    EnhancedLinearPerceptron ep = new EnhancedLinearPerceptron();
    ep.onlineoroffline = false;// set to choice offline
    ep.numberofiterations = 100;//w w  w  . j av a  2  s.  c om
    ep.setCrossvalidate = false;
    ep.setStandardiseAttributes = false;

    int numFolds = 10;
    EvaluationUtils eval = new EvaluationUtils();
    ArrayList<Prediction> preds = eval.getCVPredictions(ep, data, numFolds);
    int correct = 0;
    int total = 0;
    for (Prediction pred : preds) {
        if (pred.predicted() == pred.actual()) {
            correct++;
        }
        total++;
    }
    double acc = ((double) correct / total);

    System.out.println("Offline Accuracy: " + acc);
    writer.append(acc + ",");

    perceptronClassifier p = new perceptronClassifier();
    numFolds = 10;
    eval = new EvaluationUtils();
    ArrayList<Prediction> preds2 = eval.getCVPredictions(p, data, numFolds);
    correct = 0;
    total = 0;
    for (Prediction pred : preds2) {
        if (pred.predicted() == pred.actual()) {
            correct++;
        }
        total++;
    }
    acc = ((double) correct / total);

    System.out.println("Online Accury: " + acc);
    writer.append(acc + ",");
}

From source file:machinelearningcw.MachineLearningCw.java

public static void standardiseData(Instances data) throws Exception {
    System.out.println("\n" + "Compare Algorithms with standiasation");
    EnhancedLinearPerceptron ep = new EnhancedLinearPerceptron();
    ep.onlineoroffline = false;// set to choice offline
    ep.numberofiterations = 100;/*from w w  w  .  j  a  v  a2 s. c  o m*/
    ep.setCrossvalidate = false;
    ep.setStandardiseAttributes = true;//std attributes
    //standardise the test instances 

    int numFolds = 10;
    EvaluationUtils eval = new EvaluationUtils();
    ArrayList<Prediction> preds = eval.getCVPredictions(ep, data, numFolds);
    int correct = 0;
    int total = 0;
    for (Prediction pred : preds) {
        if (pred.predicted() == pred.actual()) {
            correct++;
        }
        total++;
    }
    double acc = ((double) correct / total);

    System.out.println("Offline Accuracy: " + acc);
    writer.append(acc + ",");
    ep.onlineoroffline = true;

    numFolds = 10;
    eval = new EvaluationUtils();
    ArrayList<Prediction> preds2 = eval.getCVPredictions(ep, data, numFolds);
    correct = 0;
    total = 0;
    for (Prediction pred : preds2) {
        if (pred.predicted() == pred.actual()) {
            correct++;
        }
        total++;
    }
    acc = ((double) correct / total);

    System.out.println("Online Accury: " + acc);
    writer.append(acc + ",");
}

From source file:machinelearningcw.MachineLearningCw.java

public static void crossValidation(Instances data) throws Exception {
    System.out.println("\n" + "CrossValidation");
    EnhancedLinearPerceptron ep = new EnhancedLinearPerceptron();
    ep.numberofiterations = 100;/*from w w w.ja  v a2s .  co  m*/
    ep.setCrossvalidate = true;
    ep.setStandardiseAttributes = false;//std attributes

    int numFolds = 10;
    EvaluationUtils eval = new EvaluationUtils();
    ArrayList<Prediction> preds = eval.getCVPredictions(ep, data, numFolds);
    int correct = 0;
    int total = 0;
    for (Prediction pred : preds) {
        if (pred.predicted() == pred.actual()) {
            correct++;
        }
        total++;
    }
    double acc = ((double) correct / total);
    if (ep.onlineoroffline) {
        System.out.println("picked: online " + "Accuracy " + acc);
        writer.append(acc + ",");
    } else {
        System.out.println("picked: offline " + "Accuracy " + acc);
        writer.append(acc + ",");
    }
}

From source file:machinelearningcw.MachineLearningCw.java

public static void ensemble(Instances data) throws Exception {
    System.out.println("====ensemble method=====");
    RandomLinearPerceptron rlp = new RandomLinearPerceptron();
    int numFolds = 10;
    EvaluationUtils eval = new EvaluationUtils();
    ArrayList<Prediction> preds = eval.getCVPredictions(rlp, data, numFolds);
    int correct = 0;
    int total = 0;
    for (Prediction pred : preds) {
        if (pred.predicted() == pred.actual()) {
            correct++;/* ww w  .j a v  a 2  s . com*/
        }
        total++;
    }
    double acc = ((double) correct / total);
    writer.append(acc + ",");

    System.out.println("Random Accuracy: " + acc);

}

From source file:machinelearningcw.MachineLearningCw.java

public static void perceptron(Instances data) throws Exception {
    perceptronClassifier p = new perceptronClassifier();
    data.setClassIndex(data.numAttributes() - 1);

    int numFolds = 10;
    EvaluationUtils eval = new EvaluationUtils();
    ArrayList<Prediction> preds = eval.getCVPredictions(p, data, numFolds);
    int correct = 0;
    int total = 0;
    for (Prediction pred : preds) {
        if (pred.predicted() == pred.actual()) {
            correct++;/*from  w  w  w  .  ja v  a 2 s. c  om*/
        }
        total++;
    }
    double acc = ((double) correct / total);

    System.out.println(acc);

}

From source file:machinelearningcw.MachineLearningCw.java

public static void RandomLinearPerceptron(Instances data) throws Exception {
    System.out.println("\n ========RandomLinearPerceptron==========");
    RandomLinearPerceptron rlp = new RandomLinearPerceptron();
    int numFolds = 10;
    EvaluationUtils eval = new EvaluationUtils();
    ArrayList<Prediction> preds = eval.getCVPredictions(rlp, data, numFolds);
    int correct = 0;
    int total = 0;
    for (Prediction pred : preds) {
        if (pred.predicted() == pred.actual()) {
            correct++;//from  w w  w . java 2 s .  c  om
        }
        total++;
    }
    double acc = ((double) correct / total);

    System.out.println("Random Accuracy: " + acc);
}