Example usage for weka.classifiers.evaluation Prediction actual

List of usage examples for weka.classifiers.evaluation Prediction actual

Introduction

In this page you can find the example usage for weka.classifiers.evaluation Prediction actual.

Prototype

double actual();

Source Link

Document

Gets the actual class value.

Usage

From source file:adams.flow.transformer.WekaAccumulatedError.java

License:Open Source License

/**
 * Returns the generated token.//from  w ww  .ja  va  2 s  . co m
 *
 * @return      the generated token
 */
@Override
public Token output() {
    Token result;
    Prediction pred;
    double error;

    pred = m_Predictions.get(0).getPrediction();
    error = Math.sqrt(Math.pow((pred.actual() - pred.predicted()), 2)) / Math.sqrt(m_NumPredictions);
    m_AccumulatdError += error;

    m_Predictions.remove(0);
    m_InputToken = null;
    result = new Token(new SequencePlotterContainer(m_PlotName,
            (double) (m_NumPredictions - m_Predictions.size()), m_AccumulatdError));

    return result;
}

From source file:adams.flow.transformer.WekaPredictionsToInstances.java

License:Open Source License

/**
 * Executes the flow item./*from  w w w  .j a v a 2 s  .  com*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Evaluation eval;
    int i;
    int n;
    int indexErr;
    int indexProb;
    int indexDist;
    int indexWeight;
    boolean nominal;
    Instances header;
    ArrayList<Attribute> atts;
    ArrayList<String> values;
    ArrayList<Prediction> predictions;
    Prediction pred;
    double[] vals;
    Instances data;
    Instances testData;
    int[] indices;

    result = null;

    if (m_InputToken.getPayload() instanceof WekaEvaluationContainer) {
        eval = (Evaluation) ((WekaEvaluationContainer) m_InputToken.getPayload())
                .getValue(WekaEvaluationContainer.VALUE_EVALUATION);
        indices = (int[]) ((WekaEvaluationContainer) m_InputToken.getPayload())
                .getValue(WekaEvaluationContainer.VALUE_ORIGINALINDICES);
        testData = (Instances) ((WekaEvaluationContainer) m_InputToken.getPayload())
                .getValue(WekaEvaluationContainer.VALUE_TESTDATA);
    } else {
        eval = (Evaluation) m_InputToken.getPayload();
        indices = null;
        testData = null;
    }
    header = eval.getHeader();
    nominal = header.classAttribute().isNominal();
    predictions = eval.predictions();

    if (predictions != null) {
        // create header
        atts = new ArrayList<>();
        // actual
        if (nominal && m_AddLabelIndex) {
            values = new ArrayList<>();
            for (i = 0; i < header.classAttribute().numValues(); i++)
                values.add((i + 1) + ":" + header.classAttribute().value(i));
            atts.add(new Attribute(m_MeasuresPrefix + "Actual", values));
        } else {
            atts.add(header.classAttribute().copy(m_MeasuresPrefix + "Actual"));
        }
        // predicted
        if (nominal && m_AddLabelIndex) {
            values = new ArrayList<>();
            for (i = 0; i < header.classAttribute().numValues(); i++)
                values.add((i + 1) + ":" + header.classAttribute().value(i));
            atts.add(new Attribute(m_MeasuresPrefix + "Predicted", values));
        } else {
            atts.add(header.classAttribute().copy(m_MeasuresPrefix + "Predicted"));
        }
        // error
        indexErr = -1;
        if (m_ShowError) {
            indexErr = atts.size();
            if (nominal) {
                values = new ArrayList<>();
                values.add("n");
                values.add("y");
                atts.add(new Attribute(m_MeasuresPrefix + "Error", values));
            } else {
                atts.add(new Attribute(m_MeasuresPrefix + "Error"));
            }
        }
        // probability
        indexProb = -1;
        if (m_ShowProbability && nominal) {
            indexProb = atts.size();
            atts.add(new Attribute(m_MeasuresPrefix + "Probability"));
        }
        // distribution
        indexDist = -1;
        if (m_ShowDistribution && nominal) {
            indexDist = atts.size();
            for (n = 0; n < header.classAttribute().numValues(); n++)
                atts.add(new Attribute(
                        m_MeasuresPrefix + "Distribution (" + header.classAttribute().value(n) + ")"));
        }
        // weight
        indexWeight = -1;
        if (m_ShowWeight) {
            indexWeight = atts.size();
            atts.add(new Attribute(m_MeasuresPrefix + "Weight"));
        }

        data = new Instances("Predictions", atts, predictions.size());
        data.setClassIndex(1); // predicted

        // add data
        if ((indices != null) && m_UseOriginalIndices)
            predictions = CrossValidationHelper.alignPredictions(predictions, indices);
        for (i = 0; i < predictions.size(); i++) {
            pred = predictions.get(i);
            vals = new double[data.numAttributes()];
            // actual
            vals[0] = pred.actual();
            // predicted
            vals[1] = pred.predicted();
            // error
            if (m_ShowError) {
                if (nominal) {
                    vals[indexErr] = ((pred.actual() != pred.predicted()) ? 1.0 : 0.0);
                } else {
                    if (m_UseAbsoluteError)
                        vals[indexErr] = Math.abs(pred.actual() - pred.predicted());
                    else
                        vals[indexErr] = pred.actual() - pred.predicted();
                }
            }
            // probability
            if (m_ShowProbability && nominal) {
                vals[indexProb] = StatUtils.max(((NominalPrediction) pred).distribution());
            }
            // distribution
            if (m_ShowDistribution && nominal) {
                for (n = 0; n < header.classAttribute().numValues(); n++)
                    vals[indexDist + n] = ((NominalPrediction) pred).distribution()[n];
            }
            // weight
            if (m_ShowWeight) {
                vals[indexWeight] = pred.weight();
            }
            // add row
            data.add(new DenseInstance(1.0, vals));
        }

        // add test data?
        if ((testData != null) && !m_TestAttributes.isEmpty()) {
            testData = filterTestData(testData);
            if (testData != null)
                data = Instances.mergeInstances(data, testData);
        }

        // generate output token
        m_OutputToken = new Token(data);
    } else {
        getLogger().severe("No predictions available from Evaluation object!");
    }

    return result;
}

From source file:adams.flow.transformer.WekaPredictionsToSpreadSheet.java

License:Open Source License

/**
 * Executes the flow item./* ww  w . ja  v a 2  s.com*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    Evaluation eval;
    int i;
    int n;
    int indexErr;
    int indexProb;
    int indexDist;
    int indexWeight;
    boolean nominal;
    Instances header;
    ArrayList<Prediction> predictions;
    Prediction pred;
    SpreadSheet data;
    Instances testData;
    InstancesView testView;
    Row row;
    int[] indices;

    result = null;

    if (m_InputToken.getPayload() instanceof WekaEvaluationContainer) {
        eval = (Evaluation) ((WekaEvaluationContainer) m_InputToken.getPayload())
                .getValue(WekaEvaluationContainer.VALUE_EVALUATION);
        indices = (int[]) ((WekaEvaluationContainer) m_InputToken.getPayload())
                .getValue(WekaEvaluationContainer.VALUE_ORIGINALINDICES);
        testData = (Instances) ((WekaEvaluationContainer) m_InputToken.getPayload())
                .getValue(WekaEvaluationContainer.VALUE_TESTDATA);
    } else {
        eval = (Evaluation) m_InputToken.getPayload();
        indices = null;
        testData = null;
    }
    header = eval.getHeader();
    nominal = header.classAttribute().isNominal();
    predictions = eval.predictions();

    if (predictions != null) {
        data = new DefaultSpreadSheet();
        data.setName("Predictions");

        // create header
        row = data.getHeaderRow();
        row.addCell("A").setContent(m_MeasuresPrefix + "Actual");
        row.addCell("P").setContent(m_MeasuresPrefix + "Predicted");
        indexErr = -1;
        if (m_ShowError) {
            indexErr = row.getCellCount();
            row.addCell("E").setContent(m_MeasuresPrefix + "Error");
        }
        // probability
        indexProb = -1;
        if (m_ShowProbability && nominal) {
            indexProb = row.getCellCount();
            row.addCell("Pr").setContent(m_MeasuresPrefix + "Probability");
        }
        // distribution
        indexDist = -1;
        if (m_ShowDistribution && nominal) {
            indexDist = row.getCellCount();
            for (n = 0; n < header.classAttribute().numValues(); n++)
                row.addCell("D" + n).setContent(
                        m_MeasuresPrefix + "Distribution (" + header.classAttribute().value(n) + ")");
        }
        // weight
        indexWeight = -1;
        if (m_ShowWeight) {
            indexWeight = row.getCellCount();
            row.addCell("W").setContent(m_MeasuresPrefix + "Weight");
        }

        // add data
        if ((indices != null) && m_UseOriginalIndices)
            predictions = CrossValidationHelper.alignPredictions(predictions, indices);
        for (i = 0; i < predictions.size(); i++) {
            pred = predictions.get(i);
            row = data.addRow();
            // actual
            if (Double.isNaN(pred.actual()))
                row.addCell(0).setMissing();
            else if (nominal)
                row.addCell(0).setContentAsString(header.classAttribute().value((int) pred.actual()));
            else
                row.addCell(0).setContent(pred.actual());
            // predicted
            if (Double.isNaN(pred.predicted()))
                row.addCell(1).setMissing();
            else if (nominal)
                row.addCell(1).setContentAsString(header.classAttribute().value((int) pred.predicted()));
            else
                row.addCell(1).setContent(pred.predicted());
            // error
            if (m_ShowError) {
                if (nominal) {
                    row.addCell(indexErr).setContent((pred.actual() != pred.predicted() ? "true" : "false"));
                } else {
                    if (m_UseAbsoluteError)
                        row.addCell(indexErr).setContent(Math.abs(pred.actual() - pred.predicted()));
                    else
                        row.addCell(indexErr).setContent(pred.actual() - pred.predicted());
                }
            }
            // probability
            if (m_ShowProbability && nominal) {
                row.addCell(indexProb).setContent(StatUtils.max(((NominalPrediction) pred).distribution()));
            }
            // distribution
            if (m_ShowDistribution && nominal) {
                for (n = 0; n < header.classAttribute().numValues(); n++)
                    row.addCell(indexDist + n).setContent(((NominalPrediction) pred).distribution()[n]);
            }
            // weight
            if (m_ShowWeight) {
                row.addCell(indexWeight).setContent(pred.weight());
            }
        }

        // add test data?
        if ((testData != null) && !m_TestAttributes.isEmpty()) {
            testData = filterTestData(testData);
            if (testData != null) {
                testView = new InstancesView(testData);
                data.mergeWith(testView);
            }
        }

        // generate output token
        m_OutputToken = new Token(data);
    } else {
        getLogger().severe("No predictions available from Evaluation object!");
    }

    return result;
}

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;//from   w  w w. j  a  v a 2 s. com
    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 ww .j  a  v a 2 s  . co 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 .jav  a  2s  .c  o 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++;//from   www  . j av a2  s  . c o m
        }
        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++;//www  . j ava2  s. co m
        }
        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.  j a  v a2s  .c  o m*/
        }
        total++;
    }
    double acc = ((double) correct / total);

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