List of usage examples for weka.classifiers Evaluation getHeader
public Instances getHeader()
From source file:adams.flow.transformer.WekaEvaluationSummary.java
License:Open Source License
/** * Executes the flow item./*ww w . j av a 2s . c o m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Evaluation eval; StringBuilder buffer; boolean prolog; String[] comment; result = null; if (m_InputToken.getPayload() instanceof WekaEvaluationContainer) eval = (Evaluation) ((WekaEvaluationContainer) m_InputToken.getPayload()) .getValue(WekaEvaluationContainer.VALUE_EVALUATION); else eval = (Evaluation) m_InputToken.getPayload(); buffer = new StringBuilder(); prolog = false; // comments if (m_Comment.getValue().length() > 0) { comment = m_Comment.getValue().split("\n"); if (comment.length == 1) { buffer.append("Comment: " + m_Comment + "\n"); } else { buffer.append("Comment:\n"); for (String line : comment) buffer.append(line + "\n"); } prolog = true; } // relation name if (m_OutputRelationName) { buffer.append("Relation: " + eval.getHeader().relationName() + "\n"); prolog = true; } // separator if (prolog) buffer.append("\n"); // summary if (m_TitleSummary.isEmpty()) buffer.append(eval.toSummaryString(m_ComplexityStatistics)); else buffer.append(eval.toSummaryString(Utils.unbackQuoteChars(m_TitleSummary), m_ComplexityStatistics)); // confusion matrix if (m_ConfusionMatrix) { try { buffer.append("\n\n"); if (m_TitleMatrix.isEmpty()) buffer.append(eval.toMatrixString()); else buffer.append(eval.toMatrixString(Utils.unbackQuoteChars(m_TitleMatrix))); } catch (Exception e) { result = handleException("Failed to generate confusion matrix: ", e); } } // class details if (m_ClassDetails) { try { buffer.append("\n\n"); if (m_TitleClassDetails.isEmpty()) buffer.append(eval.toClassDetailsString()); else buffer.append(eval.toClassDetailsString(Utils.unbackQuoteChars(m_TitleClassDetails))); } catch (Exception e) { result = handleException("Failed to generate class details: ", e); } } m_OutputToken = new Token(buffer.toString()); return result; }
From source file:adams.flow.transformer.WekaEvaluationValuePicker.java
License:Open Source License
/** * Executes the flow item.// w w w . j a v a2 s . c o m * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; double value; Evaluation eval; result = null; value = Double.NaN; if (m_InputToken.getPayload() instanceof WekaEvaluationContainer) eval = (Evaluation) ((WekaEvaluationContainer) m_InputToken.getPayload()) .getValue(WekaEvaluationContainer.VALUE_EVALUATION); else eval = (Evaluation) m_InputToken.getPayload(); try { m_ClassIndex.setData(eval.getHeader().classAttribute()); value = EvaluationHelper.getValue(eval, m_StatisticValue, m_ClassIndex.getIntIndex()); m_OutputToken = new Token(value); } catch (Exception e) { result = handleException("Error retrieving value for '" + m_StatisticValue + "':\n", e); } return result; }
From source file:adams.flow.transformer.WekaEvaluationValues.java
License:Open Source License
/** * Adds the specified statistic to the spreadsheet. * /* ww w. jav a 2s . co m*/ * @param eval the {@link Evaluation} object to get the statist from * @param sheet the sheet to add the data to * @param statistic the statistic to add * @param classIndex the class index to use (for class-specific stats) * @param useIndex whether to use the index in the "Statistic" column * @return null if successfully added, otherwise error message */ protected String addStatistic(Evaluation eval, SpreadSheet sheet, EvaluationStatistic statistic, int classIndex, boolean useIndex) { String result; Row row; double value; String name; result = null; try { value = EvaluationHelper.getValue(eval, statistic, classIndex); row = sheet.addRow(); name = statistic.toDisplayShort(); if (useIndex && statistic.isPerClass()) name += " (" + eval.getHeader().classAttribute().value(classIndex) + ")"; row.addCell(0).setContent(name); row.addCell(1).setContent(Double.toString(value)); } catch (Exception e) { result = handleException("Error retrieving value for '" + statistic + "':\n", e); } return result; }
From source file:adams.flow.transformer.WekaEvaluationValues.java
License:Open Source License
/** * Executes the flow item./* ww w. j av a 2 s . co m*/ * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; Evaluation eval; SpreadSheet sheet; int[] indices; String msg; result = null; // fill spreadsheet if (m_InputToken.getPayload() instanceof WekaEvaluationContainer) eval = (Evaluation) ((WekaEvaluationContainer) m_InputToken.getPayload()) .getValue(WekaEvaluationContainer.VALUE_EVALUATION); else eval = (Evaluation) m_InputToken.getPayload(); m_ClassIndex.setData(eval.getHeader().classAttribute()); indices = m_ClassIndex.getIntIndices(); sheet = new DefaultSpreadSheet(); sheet.getHeaderRow().addCell("0").setContent("Statistic"); sheet.getHeaderRow().addCell("1").setContent("Value"); if (indices.length <= 1) { for (EvaluationStatistic statistic : m_StatisticValues) { msg = addStatistic(eval, sheet, statistic, (indices.length == 0 ? 0 : indices[0]), true); if (msg != null) { if (result == null) result = ""; else result += "\n"; result += msg; } } } else if (indices.length > 1) { // not class-specific stats for (EvaluationStatistic statistic : m_StatisticValues) { if (statistic.isPerClass()) continue; msg = addStatistic(eval, sheet, statistic, 0, false); // class index is irrelevant if (msg != null) { if (result == null) result = ""; else result += "\n"; result += msg; } } // class-specific stats for (int index : indices) { for (EvaluationStatistic statistic : m_StatisticValues) { if (!statistic.isPerClass()) continue; msg = addStatistic(eval, sheet, statistic, index, true); if (msg != null) { if (result == null) result = ""; else result += "\n"; result += msg; } } } } // generate output token m_OutputToken = new Token(sheet); return result; }
From source file:adams.flow.transformer.WekaPredictionsToInstances.java
License:Open Source License
/** * Executes the flow item./*ww w. j a v a 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<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 ww.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:adams.gui.visualization.debug.inspectionhandler.WekaEvaluation.java
License:Open Source License
/** * Returns further inspection values.//from ww w . j ava2s. c om * * @param obj the object to further inspect * @return the named inspected values */ @Override public Hashtable<String, Object> inspect(Object obj) { Hashtable<String, Object> result; Evaluation eval; WekaEvaluationValues values; boolean numeric; boolean nominal; List<EvaluationStatistic> stats; String msg; Token token; result = new Hashtable<>(); eval = (Evaluation) obj; result.put("header", eval.getHeader()); result.put("predictions", eval.predictions()); nominal = eval.getHeader().classAttribute().isNominal(); numeric = eval.getHeader().classAttribute().isNumeric(); stats = new ArrayList<>(); for (EvaluationStatistic stat : EvaluationStatistic.values()) { if (nominal && stat.isOnlyNominal()) stats.add(stat); else if (numeric && stat.isOnlyNumeric()) stats.add(stat); else if (!stat.isOnlyNumeric() && !stat.isOnlyNominal()) stats.add(stat); } values = new WekaEvaluationValues(); values.setStatisticValues(stats.toArray(new EvaluationStatistic[stats.size()])); values.input(new Token(eval)); msg = values.execute(); if (msg == null) { token = values.output(); if (token != null) result.put("statistics", token.getPayload()); } else { System.err.println(getClass().getName() + ": Failed to extract statistics:\n" + msg); } return result; }
From source file:de.fub.maps.project.detector.model.inference.ui.EvaluationPanel.java
License:Apache License
public void updatePanel(Evaluation evaluation) { DefaultCategoryDataset dataset = getBarChartPanel().getDataset(); dataset.clear();/*from w w w.j a v a 2 s . c o m*/ this.evaluation = evaluation; double correct = evaluation.pctCorrect(); double incorrect = evaluation.pctIncorrect(); getCorrectClassifiedInstances().setText(MessageFormat.format(NUMBER_PATTERN, correct)); getIncorrectClassifiedInstances().setText(MessageFormat.format(NUMBER_PATTERN, incorrect)); int numClasses = evaluation.getHeader().numClasses(); for (int classIndex = 0; classIndex < numClasses; classIndex++) { double precision = evaluation.precision(classIndex) * 100; double recall = evaluation.recall(classIndex) * 100; dataset.addValue(precision, NbBundle.getMessage(EvaluationPanel.class, "EvaluationPanel.CLT_Precision_Text"), evaluation.getHeader().classAttribute().value(classIndex)); dataset.addValue(recall, NbBundle.getMessage(EvaluationPanel.class, "EvaluationPanel.CLT_Recall_Text"), evaluation.getHeader().classAttribute().value(classIndex)); } getExplorerManager() .setRootContext(new AbstractNode(Children.create(new EvaluationNodeFactory(evaluation), true))); repaint(); }
From source file:de.unidue.langtech.grading.report.LearningCurveReport.java
License:Open Source License
@Override public void execute() throws Exception { File storage = getContext().getStorageLocation(TestTask.TEST_TASK_OUTPUT_KEY, AccessMode.READONLY); for (Integer numberOfInstances : LearningCurveTask.NUMBER_OF_TRAINING_INSTANCES) { Properties props = new Properties(); List<Double> kappas = new ArrayList<Double>(); for (int iteration = 0; iteration < LearningCurveTask.ITERATIONS; iteration++) { File evaluationFile = new File(storage.getAbsolutePath() + "/" + TestTask.EVALUATION_DATA_FILENAME + "_" + numberOfInstances + "_" + iteration); // we need to check non-existing files as we might skip some training sizes if (!evaluationFile.exists()) { continue; }//w w w . j a va 2 s.c om weka.classifiers.Evaluation eval = (weka.classifiers.Evaluation) SerializationHelper .read(evaluationFile.getAbsolutePath()); // System.out.println(eval.toMatrixString()); List<String> classLabels = TaskUtils.getClassLabels(eval.getHeader(), false); List<Integer> classLabelsInteger = new ArrayList<Integer>(); for (String classLabel : classLabels) { classLabelsInteger.add(Integer.parseInt(classLabel)); } double[][] confusionMatrix = eval.confusionMatrix(); List<Integer> goldLabelsList = new ArrayList<Integer>(); List<Integer> predictedLabelsList = new ArrayList<Integer>(); // fill rating lists from weka confusion matrix for (int c = 0; c < confusionMatrix.length; c++) { for (int r = 0; r < confusionMatrix.length; r++) { for (int i = 0; i < (int) confusionMatrix[c][r]; i++) { goldLabelsList.add(classLabelsInteger.get(c)); predictedLabelsList.add(classLabelsInteger.get(r)); } } } double kappa = QuadraticWeightedKappa.getKappa(goldLabelsList, predictedLabelsList, classLabelsInteger.toArray(new Integer[0])); kappas.add(kappa); } double min = -1.0; double max = -1.0; if (kappas.size() > 0) { min = Collections.min(kappas); max = Collections.max(kappas); } double meanKappa = QuadraticWeightedKappa.getMeanKappa(kappas); results.put(KappaReport.KAPPA, meanKappa); System.out.println(numberOfInstances + "\t" + meanKappa + "\t" + min + "\t" + max); for (String s : results.keySet()) { props.setProperty(s, results.get(s).toString()); } // Write out properties getContext().storeBinary(TestTask.RESULTS_FILENAME + "_" + numberOfInstances, new PropertiesAdapter(props)); } }