Example usage for weka.classifiers.evaluation Prediction weight

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

Introduction

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

Prototype

double weight();

Source Link

Document

Gets the weight assigned to this prediction.

Usage

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

License:Open Source License

/**
 * Executes the flow item./* w  ww  .j a  va 2 s .  c o m*/
 *
 * @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./*from  w w w  . j ava  2s.c  o m*/
 *
 * @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;
}