Example usage for weka.classifiers Evaluation getHeader

List of usage examples for weka.classifiers Evaluation getHeader

Introduction

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

Prototype

public Instances getHeader() 

Source Link

Document

Returns the header of the underlying dataset.

Usage

From source file:adams.data.conversion.WekaEvaluationToCostCurve.java

License:Open Source License

/**
 * Performs the actual conversion.//from   ww  w  . ja v a2s . c  om
 *
 * @return      the converted data
 * @throws Exception   if something goes wrong with the conversion
 */
@Override
protected Object doConvert() throws Exception {
    Evaluation eval;
    CostCurve curve;
    Instances cost;

    eval = (Evaluation) m_Input;
    m_ClassLabelIndex.setMax(eval.getHeader().classAttribute().numValues());
    curve = new CostCurve();
    cost = curve.getCurve(eval.predictions(), m_ClassLabelIndex.getIntIndex());

    return cost;
}

From source file:adams.data.conversion.WekaEvaluationToThresholdCurve.java

License:Open Source License

/**
 * Performs the actual conversion.//from   w  w w .jav a  2 s.co  m
 *
 * @return      the converted data
 * @throws Exception   if something goes wrong with the conversion
 */
@Override
protected Object doConvert() throws Exception {
    Evaluation eval;
    ThresholdCurve curve;
    Instances cost;

    eval = (Evaluation) m_Input;
    m_ClassLabelIndex.setMax(eval.getHeader().classAttribute().numValues());
    curve = new ThresholdCurve();
    cost = curve.getCurve(eval.predictions(), m_ClassLabelIndex.getIntIndex());

    return cost;
}

From source file:adams.flow.sink.WekaCostBenefitAnalysis.java

License:Open Source License

/**
 * Plots the token (the panel and dialog have already been created at
 * this stage)./*from   w w w  .jav  a  2  s .  com*/
 *
 * @param token   the token to display
 */
@Override
protected void display(Token token) {
    Evaluation eval;
    Attribute classAtt;
    Attribute classAttToUse;
    int classValue;
    ThresholdCurve tc;
    Instances result;
    ArrayList<String> newNames;
    CostBenefitAnalysis cbAnalysis;
    PlotData2D tempd;
    boolean[] cp;
    int n;

    try {
        if (token.getPayload() instanceof WekaEvaluationContainer)
            eval = (Evaluation) ((WekaEvaluationContainer) token.getPayload())
                    .getValue(WekaEvaluationContainer.VALUE_EVALUATION);
        else
            eval = (Evaluation) token.getPayload();
        if (eval.predictions() == null) {
            getLogger().severe("No predictions available from Evaluation object!");
            return;
        }
        classAtt = eval.getHeader().classAttribute();
        m_ClassIndex.setData(classAtt);
        classValue = m_ClassIndex.getIntIndex();
        tc = new ThresholdCurve();
        result = tc.getCurve(eval.predictions(), classValue);

        // Create a dummy class attribute with the chosen
        // class value as index 0 (if necessary).
        classAttToUse = eval.getHeader().classAttribute();
        if (classValue != 0) {
            newNames = new ArrayList<>();
            newNames.add(classAtt.value(classValue));
            for (int k = 0; k < classAtt.numValues(); k++) {
                if (k != classValue)
                    newNames.add(classAtt.value(k));
            }
            classAttToUse = new Attribute(classAtt.name(), newNames);
        }
        // assemble plot data
        tempd = new PlotData2D(result);
        tempd.setPlotName(result.relationName());
        tempd.m_alwaysDisplayPointsOfThisSize = 10;
        // specify which points are connected
        cp = new boolean[result.numInstances()];
        for (n = 1; n < cp.length; n++)
            cp[n] = true;
        tempd.setConnectPoints(cp);
        // add plot
        m_CostBenefitPanel.setCurveData(tempd, classAttToUse);
    } catch (Exception e) {
        handleException("Failed to display token: " + token, e);
    }
}

From source file:adams.flow.sink.WekaCostBenefitAnalysis.java

License:Open Source License

/**
 * Creates a new panel for the token.//from w w w .  ja  va2s . c  om
 *
 * @param token   the token to display in a new panel, can be null
 * @return      the generated panel
 */
public AbstractDisplayPanel createDisplayPanel(Token token) {
    AbstractDisplayPanel result;
    String name;

    if (token != null)
        name = "Cost curve (" + getEvaluation(token).getHeader().relationName() + ")";
    else
        name = "Cost curve";

    result = new AbstractComponentDisplayPanel(name) {
        private static final long serialVersionUID = -3513994354297811163L;
        protected CostBenefitAnalysis m_VisualizePanel;

        @Override
        protected void initGUI() {
            super.initGUI();
            setLayout(new BorderLayout());
            m_VisualizePanel = new CostBenefitAnalysis();
            add(m_VisualizePanel, BorderLayout.CENTER);
        }

        @Override
        public void display(Token token) {
            try {
                Evaluation eval = getEvaluation(token);
                Attribute classAtt = eval.getHeader().classAttribute();
                m_ClassIndex.setData(classAtt);
                int classValue = m_ClassIndex.getIntIndex();
                ThresholdCurve tc = new ThresholdCurve();
                Instances result = tc.getCurve(eval.predictions(), classValue);

                // Create a dummy class attribute with the chosen
                // class value as index 0 (if necessary).
                Attribute classAttToUse = eval.getHeader().classAttribute();
                if (classValue != 0) {
                    ArrayList<String> newNames = new ArrayList<>();
                    newNames.add(classAtt.value(classValue));
                    for (int k = 0; k < classAtt.numValues(); k++) {
                        if (k != classValue)
                            newNames.add(classAtt.value(k));
                    }
                    classAttToUse = new Attribute(classAtt.name(), newNames);
                }
                // assemble plot data
                PlotData2D tempd = new PlotData2D(result);
                tempd.setPlotName(result.relationName());
                tempd.m_alwaysDisplayPointsOfThisSize = 10;
                // specify which points are connected
                boolean[] cp = new boolean[result.numInstances()];
                for (int n = 1; n < cp.length; n++)
                    cp[n] = true;
                tempd.setConnectPoints(cp);
                // add plot
                m_VisualizePanel.setCurveData(tempd, classAttToUse);
            } catch (Exception e) {
                getLogger().log(Level.SEVERE, "Failed to display token: " + token, e);
            }
        }

        @Override
        public JComponent supplyComponent() {
            return m_VisualizePanel;
        }

        @Override
        public void clearPanel() {
        }

        public void cleanUp() {
        }
    };

    if (token != null)
        result.display(token);

    return result;
}

From source file:adams.flow.sink.WekaCostCurve.java

License:Open Source License

/**
 * Plots the token (the panel and dialog have already been created at
 * this stage)./*from  w ww.j av a2s. c om*/
 *
 * @param token   the token to display
 */
@Override
protected void display(Token token) {
    weka.classifiers.evaluation.CostCurve curve;
    Evaluation eval;
    PlotData2D plot;
    boolean[] connectPoints;
    int cp;
    Instances data;
    int[] indices;

    try {
        if (token.getPayload() instanceof WekaEvaluationContainer)
            eval = (Evaluation) ((WekaEvaluationContainer) token.getPayload())
                    .getValue(WekaEvaluationContainer.VALUE_EVALUATION);
        else
            eval = (Evaluation) token.getPayload();
        if (eval.predictions() == null) {
            getLogger().severe("No predictions available from Evaluation object!");
            return;
        }
        m_ClassLabelRange.setData(eval.getHeader().classAttribute());
        indices = m_ClassLabelRange.getIntIndices();
        for (int index : indices) {
            curve = new weka.classifiers.evaluation.CostCurve();
            data = curve.getCurve(eval.predictions(), index);
            plot = new PlotData2D(data);
            plot.setPlotName(eval.getHeader().classAttribute().value(index));
            plot.m_displayAllPoints = true;
            connectPoints = new boolean[data.numInstances()];
            for (cp = 1; cp < connectPoints.length; cp++)
                connectPoints[cp] = true;
            plot.setConnectPoints(connectPoints);
            m_VisualizePanel.addPlot(plot);
        }
    } catch (Exception e) {
        handleException("Failed to display token: " + token, e);
    }
}

From source file:adams.flow.sink.WekaCostCurve.java

License:Open Source License

/**
 * Creates a new panel for the token.//from  w  ww  .j av a 2s .  c  om
 *
 * @param token   the token to display in a new panel, can be null
 * @return      the generated panel
 */
public AbstractDisplayPanel createDisplayPanel(Token token) {
    AbstractDisplayPanel result;
    String name;

    if (token != null)
        name = "Cost curve (" + getEvaluation(token).getHeader().relationName() + ")";
    else
        name = "Cost curve";

    result = new AbstractComponentDisplayPanel(name) {
        private static final long serialVersionUID = -3513994354297811163L;
        protected VisualizePanel m_VisualizePanel;

        @Override
        protected void initGUI() {
            super.initGUI();
            setLayout(new BorderLayout());
            m_VisualizePanel = new VisualizePanel();
            add(m_VisualizePanel, BorderLayout.CENTER);
        }

        @Override
        public void display(Token token) {
            try {
                Evaluation eval = getEvaluation(token);
                m_ClassLabelRange.setMax(eval.getHeader().classAttribute().numValues());
                int[] indices = m_ClassLabelRange.getIntIndices();
                for (int index : indices) {
                    weka.classifiers.evaluation.CostCurve curve = new weka.classifiers.evaluation.CostCurve();
                    Instances data = curve.getCurve(eval.predictions(), index);
                    PlotData2D plot = new PlotData2D(data);
                    plot.setPlotName(eval.getHeader().classAttribute().value(index));
                    plot.m_displayAllPoints = true;
                    boolean[] connectPoints = new boolean[data.numInstances()];
                    for (int cp = 1; cp < connectPoints.length; cp++)
                        connectPoints[cp] = true;
                    plot.setConnectPoints(connectPoints);
                    m_VisualizePanel.addPlot(plot);
                }
            } catch (Exception e) {
                getLogger().log(Level.SEVERE, "Failed to display token: " + token, e);
            }
        }

        @Override
        public JComponent supplyComponent() {
            return m_VisualizePanel;
        }

        @Override
        public void clearPanel() {
            m_VisualizePanel.removeAllPlots();
        }

        public void cleanUp() {
            m_VisualizePanel.removeAllPlots();
        }
    };

    if (token != null)
        result.display(token);

    return result;
}

From source file:adams.flow.sink.WekaThresholdCurve.java

License:Open Source License

/**
 * Plots the token (the panel and dialog have already been created at
 * this stage).//from www  . ja v a2 s .c  om
 *
 * @param token   the token to display
 */
@Override
protected void display(Token token) {
    ThresholdCurve curve;
    Evaluation eval;
    PlotData2D plot;
    boolean[] connectPoints;
    int cp;
    Instances data;
    int[] indices;

    try {
        if (token.getPayload() instanceof WekaEvaluationContainer)
            eval = (Evaluation) ((WekaEvaluationContainer) token.getPayload())
                    .getValue(WekaEvaluationContainer.VALUE_EVALUATION);
        else
            eval = (Evaluation) token.getPayload();
        if (eval.predictions() == null) {
            getLogger().severe("No predictions available from Evaluation object!");
            return;
        }
        m_ClassLabelRange.setData(eval.getHeader().classAttribute());
        indices = m_ClassLabelRange.getIntIndices();
        for (int index : indices) {
            curve = new ThresholdCurve();
            data = curve.getCurve(eval.predictions(), index);
            plot = new PlotData2D(data);
            plot.setPlotName(eval.getHeader().classAttribute().value(index));
            plot.m_displayAllPoints = true;
            connectPoints = new boolean[data.numInstances()];
            for (cp = 1; cp < connectPoints.length; cp++)
                connectPoints[cp] = true;
            plot.setConnectPoints(connectPoints);
            m_VisualizePanel.addPlot(plot);
            if (data.attribute(m_AttributeX.toDisplay()) != null)
                m_VisualizePanel.setXIndex(data.attribute(m_AttributeX.toDisplay()).index());
            if (data.attribute(m_AttributeY.toDisplay()) != null)
                m_VisualizePanel.setYIndex(data.attribute(m_AttributeY.toDisplay()).index());
        }
    } catch (Exception e) {
        handleException("Failed to display token: " + token, e);
    }
}

From source file:adams.flow.sink.WekaThresholdCurve.java

License:Open Source License

/**
 * Creates a new panel for the token./*w w w.j  ava 2 s . c o m*/
 *
 * @param token   the token to display in a new panel, can be null
 * @return      the generated panel
 */
public AbstractDisplayPanel createDisplayPanel(Token token) {
    AbstractDisplayPanel result;
    String name;

    if (token != null)
        name = "Threshold curve (" + getEvaluation(token).getHeader().relationName() + ")";
    else
        name = "Threshold curve";

    result = new AbstractComponentDisplayPanel(name) {
        private static final long serialVersionUID = -7362768698548152899L;
        protected ThresholdVisualizePanel m_VisualizePanel;

        @Override
        protected void initGUI() {
            super.initGUI();
            setLayout(new BorderLayout());
            m_VisualizePanel = new ThresholdVisualizePanel();
            add(m_VisualizePanel, BorderLayout.CENTER);
        }

        @Override
        public void display(Token token) {
            try {
                Evaluation eval = getEvaluation(token);
                m_ClassLabelRange.setMax(eval.getHeader().classAttribute().numValues());
                int[] indices = m_ClassLabelRange.getIntIndices();
                for (int index : indices) {
                    ThresholdCurve curve = new ThresholdCurve();
                    Instances data = curve.getCurve(eval.predictions(), index);
                    PlotData2D plot = new PlotData2D(data);
                    plot.setPlotName(eval.getHeader().classAttribute().value(index));
                    plot.m_displayAllPoints = true;
                    boolean[] connectPoints = new boolean[data.numInstances()];
                    for (int cp = 1; cp < connectPoints.length; cp++)
                        connectPoints[cp] = true;
                    plot.setConnectPoints(connectPoints);
                    m_VisualizePanel.addPlot(plot);
                    if (data.attribute(m_AttributeX.toDisplay()) != null)
                        m_VisualizePanel.setXIndex(data.attribute(m_AttributeX.toDisplay()).index());
                    if (data.attribute(m_AttributeY.toDisplay()) != null)
                        m_VisualizePanel.setYIndex(data.attribute(m_AttributeY.toDisplay()).index());
                }
            } catch (Exception e) {
                getLogger().log(Level.SEVERE, "Failed to display token: " + token, e);
            }
        }

        @Override
        public JComponent supplyComponent() {
            return m_VisualizePanel;
        }

        @Override
        public void clearPanel() {
            m_VisualizePanel.removeAllPlots();
        }

        public void cleanUp() {
            m_VisualizePanel.removeAllPlots();
        }
    };

    if (token != null)
        result.display(token);

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item.//from  w w  w .java  2  s  . com
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    SpreadSheet sheet;
    Row row;
    Evaluation evalAll;
    Evaluation eval;
    WekaEvaluationContainer cont;
    TIntList indices;
    Random random;
    int i;
    int iteration;
    int size;
    List<Prediction> preds;
    Instances header;
    Instances data;
    ArrayList<Attribute> atts;
    Instance inst;
    boolean numeric;
    int classIndex;
    Double[] errors;
    Double[] errorsRev;
    Percentile<Double> perc;
    Percentile<Double> percRev;
    TIntList subset;

    result = null;

    if (m_InputToken.getPayload() instanceof Evaluation) {
        evalAll = (Evaluation) m_InputToken.getPayload();
    } else {
        cont = (WekaEvaluationContainer) m_InputToken.getPayload();
        evalAll = (Evaluation) cont.getValue(WekaEvaluationContainer.VALUE_EVALUATION);
    }

    if ((evalAll.predictions() == null) || (evalAll.predictions().size() == 0))
        result = "No predictions available!";

    if (result == null) {
        // init spreadsheet
        sheet = new DefaultSpreadSheet();
        row = sheet.getHeaderRow();
        row.addCell("S").setContentAsString("Subsample");
        for (EvaluationStatistic s : m_StatisticValues)
            row.addCell(s.toString()).setContentAsString(s.toString());
        for (i = 0; i < m_Percentiles.length; i++) {
            switch (m_ErrorCalculation) {
            case ACTUAL_MINUS_PREDICTED:
                row.addCell("perc-AmP-" + i).setContentAsString("Percentile-AmP-" + m_Percentiles[i]);
                break;
            case PREDICTED_MINUS_ACTUAL:
                row.addCell("perc-PmA-" + i).setContentAsString("Percentile-PmA-" + m_Percentiles[i]);
                break;
            case ABSOLUTE:
                row.addCell("perc-Abs-" + i).setContentAsString("Percentile-Abs-" + m_Percentiles[i]);
                break;
            case BOTH:
                row.addCell("perc-AmP-" + i).setContentAsString("Percentile-AmP-" + m_Percentiles[i]);
                row.addCell("perc-PmA-" + i).setContentAsString("Percentile-PmA-" + m_Percentiles[i]);
                break;
            default:
                throw new IllegalStateException("Unhandled error calculation: " + m_ErrorCalculation);
            }
        }

        // set up bootstrapping
        preds = evalAll.predictions();
        random = new Random(m_Seed);
        indices = new TIntArrayList();
        size = (int) Math.round(preds.size() * m_Percentage);
        header = evalAll.getHeader();
        numeric = header.classAttribute().isNumeric();
        m_ClassIndex.setData(header.classAttribute());
        if (numeric)
            classIndex = -1;
        else
            classIndex = m_ClassIndex.getIntIndex();
        for (i = 0; i < preds.size(); i++)
            indices.add(i);

        // create fake evalutions
        subset = new TIntArrayList();
        for (iteration = 0; iteration < m_NumSubSamples; iteration++) {
            if (isStopped()) {
                sheet = null;
                break;
            }

            // determine
            subset.clear();
            if (m_WithReplacement) {
                for (i = 0; i < size; i++)
                    subset.add(indices.get(random.nextInt(preds.size())));
            } else {
                indices.shuffle(random);
                for (i = 0; i < size; i++)
                    subset.add(indices.get(i));
            }

            // create dataset from predictions
            errors = new Double[size];
            errorsRev = new Double[size];
            atts = new ArrayList<>();
            atts.add(header.classAttribute().copy("Actual"));
            data = new Instances(header.relationName() + "-" + (iteration + 1), atts, size);
            data.setClassIndex(0);
            for (i = 0; i < subset.size(); i++) {
                inst = new DenseInstance(preds.get(subset.get(i)).weight(),
                        new double[] { preds.get(subset.get(i)).actual() });
                data.add(inst);
                switch (m_ErrorCalculation) {
                case ACTUAL_MINUS_PREDICTED:
                    errors[i] = preds.get(subset.get(i)).actual() - preds.get(subset.get(i)).predicted();
                    break;
                case PREDICTED_MINUS_ACTUAL:
                    errorsRev[i] = preds.get(subset.get(i)).predicted() - preds.get(subset.get(i)).actual();
                    break;
                case ABSOLUTE:
                    errors[i] = Math
                            .abs(preds.get(subset.get(i)).actual() - preds.get(subset.get(i)).predicted());
                    break;
                case BOTH:
                    errors[i] = preds.get(subset.get(i)).actual() - preds.get(subset.get(i)).predicted();
                    errorsRev[i] = preds.get(subset.get(i)).predicted() - preds.get(subset.get(i)).actual();
                    break;
                default:
                    throw new IllegalStateException("Unhandled error calculation: " + m_ErrorCalculation);
                }
            }

            // perform "fake" evaluation
            try {
                eval = new Evaluation(data);
                for (i = 0; i < subset.size(); i++) {
                    if (numeric)
                        eval.evaluateModelOnceAndRecordPrediction(
                                new double[] { preds.get(subset.get(i)).predicted() }, data.instance(i));
                    else
                        eval.evaluateModelOnceAndRecordPrediction(
                                ((NominalPrediction) preds.get(subset.get(i))).distribution().clone(),
                                data.instance(i));
                }
            } catch (Exception e) {
                result = handleException(
                        "Failed to create 'fake' Evaluation object (iteration: " + (iteration + 1) + ")!", e);
                break;
            }

            // add row
            row = sheet.addRow();
            row.addCell("S").setContent(iteration + 1);
            for (EvaluationStatistic s : m_StatisticValues) {
                try {
                    row.addCell(s.toString()).setContent(EvaluationHelper.getValue(eval, s, classIndex));
                } catch (Exception e) {
                    getLogger().log(Level.SEVERE,
                            "Failed to calculate statistic in iteration #" + (iteration + 1) + ": " + s, e);
                    row.addCell(s.toString()).setMissing();
                }
            }
            for (i = 0; i < m_Percentiles.length; i++) {
                perc = new Percentile<>();
                perc.addAll(errors);
                percRev = new Percentile<>();
                percRev.addAll(errorsRev);
                switch (m_ErrorCalculation) {
                case ACTUAL_MINUS_PREDICTED:
                    row.addCell("perc-AmP-" + i).setContent(perc.getPercentile(m_Percentiles[i].doubleValue()));
                    break;
                case PREDICTED_MINUS_ACTUAL:
                    row.addCell("perc-PmA-" + i)
                            .setContent(percRev.getPercentile(m_Percentiles[i].doubleValue()));
                    break;
                case ABSOLUTE:
                    row.addCell("perc-Abs-" + i).setContent(perc.getPercentile(m_Percentiles[i].doubleValue()));
                    break;
                case BOTH:
                    row.addCell("perc-AmP-" + i).setContent(perc.getPercentile(m_Percentiles[i].doubleValue()));
                    row.addCell("perc-PmA-" + i)
                            .setContent(percRev.getPercentile(m_Percentiles[i].doubleValue()));
                    break;
                default:
                    throw new IllegalStateException("Unhandled error calculation: " + m_ErrorCalculation);
                }
            }
        }

        if ((result == null) && (sheet != null))
            m_OutputToken = new Token(sheet);
    }

    return result;
}

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

License:Open Source License

/**
 * Executes the flow item.//from  w  ww .  j  a v a2s . com
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    Evaluation eval;

    if (m_InputToken.getPayload() instanceof Evaluation)
        eval = (Evaluation) m_InputToken.getPayload();
    else
        eval = (Evaluation) ((WekaEvaluationContainer) m_InputToken.getPayload())
                .getValue(WekaEvaluationContainer.VALUE_EVALUATION);

    switch (m_Type) {
    case RELATION_NAME:
        m_OutputToken = new Token(eval.getHeader().relationName());
        break;
    case CLASS_ATTRIBUTE_NAME:
        m_OutputToken = new Token(eval.getHeader().classAttribute().name());
        break;
    case HEADER:
        m_OutputToken = new Token(eval.getHeader());
        break;
    case PREDICTIONS_RECORDED:
        m_OutputToken = new Token(eval.predictions() != null);
        break;
    case NUM_PREDICTIONS:
        if (eval.predictions() == null)
            m_OutputToken = new Token(-1);
        else
            m_OutputToken = new Token(eval.predictions().size());
        break;
    default:
        throw new IllegalStateException("Unhandled info type: " + m_Type);
    }

    return null;
}