List of usage examples for weka.classifiers Evaluation evaluateModelOnceAndRecordPrediction
public double evaluateModelOnceAndRecordPrediction(double[] dist, Instance instance) throws Exception
From source file:DocClassifier.java
public Evaluation classify(Classifier classifier) throws Exception { docPredList.clear();/*from www.ja v a 2s .com*/ Instances testInstances = createInstances(testFiles); Instances trainInstances = createInstances(trainFiles); classifier.buildClassifier(trainInstances); Evaluation ev = new Evaluation(trainInstances); for (int i = 0; i < testInstances.numInstances(); ++i) { Instance inst = testInstances.instance(i); double pred = ev.evaluateModelOnceAndRecordPrediction(classifier, inst); docPredList.add(testFiles[i].getName() + "\t=>\t" + inst.classAttribute().value((int) pred)); } return ev; }
From source file:adams.flow.transformer.WekaBootstrapping.java
License:Open Source License
/** * Executes the flow item.// w ww . j a va 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:boostingPL.boosting.AdaBoost.java
License:Open Source License
public static void main(String[] args) throws Exception { java.io.File inputFile = new java.io.File( "/home/aax/xpShareSpace/dataset/single-class/+winered/winequality-red.datatrain1.arff"); ArffLoader atf = new ArffLoader(); atf.setFile(inputFile);// w w w .j a v a 2 s. c o m Instances training = atf.getDataSet(); training.setClassIndex(training.numAttributes() - 1); AdaBoost adaBoost = new AdaBoost(training, 100); for (int t = 0; t < 100; t++) { adaBoost.run(t); } java.io.File inputFilet = new java.io.File( "/home/aax/xpShareSpace/dataset/single-class/+winered/winequality-red.datatest1.arff"); ArffLoader atft = new ArffLoader(); atft.setFile(inputFilet); Instances testing = atft.getDataSet(); testing.setClassIndex(testing.numAttributes() - 1); Evaluation eval = new Evaluation(testing); for (Instance inst : testing) { eval.evaluateModelOnceAndRecordPrediction(adaBoost, inst); } System.out.println(eval.toSummaryString()); System.out.println(eval.toClassDetailsString()); System.out.println(eval.toMatrixString()); /* int right = 0; for (int i = 0; i < testing.numInstances(); i++) { Instance inst = testing.instance(i); if (adaBoost.classifyInstance(inst) == inst.classValue()) { right++; } } System.out.println(right); System.out.println((double)right/training.numInstances()); */ }
From source file:boostingPL.boosting.SAMME.java
License:Open Source License
public static void main(String[] args) throws Exception { java.io.File inputFile = new java.io.File(args[0]); ArffLoader atf = new ArffLoader(); atf.setFile(inputFile);//w w w . j a va2s.c o m Instances training = atf.getDataSet(); training.setClassIndex(training.numAttributes() - 1); //Instances testing = new Instances(training); int iterationNum = 100; SAMME samme = new SAMME(training, iterationNum); for (int t = 0; t < iterationNum; t++) { samme.run(t); } java.io.File inputFilet = new java.io.File(args[1]); ArffLoader atft = new ArffLoader(); atft.setFile(inputFilet); Instances testing = atft.getDataSet(); testing.setClassIndex(testing.numAttributes() - 1); Evaluation eval = new Evaluation(testing); for (Instance inst : testing) { eval.evaluateModelOnceAndRecordPrediction(samme, inst); } System.out.println(eval.toSummaryString()); System.out.println(eval.toClassDetailsString()); System.out.println(eval.toMatrixString()); }
From source file:org.openml.webapplication.evaluate.EvaluateStreamPredictions.java
License:Open Source License
private void doEvaluation() throws Exception { // set global evaluation Evaluation globalEvaluator = new Evaluation(datasetStructure); // we go through all the instances in one loop. Instance currentInstance;/*from ww w.ja v a 2 s. c o m*/ for (int iInstanceNr = 0; ((currentInstance = datasetLoader .getNextInstance(datasetStructure)) != null); ++iInstanceNr) { Instance currentPrediction = predictionsLoader.getNextInstance(predictionsStructure); if (currentPrediction == null) throw new Exception("Could not find prediction for instance #" + iInstanceNr); int rowid = (int) currentPrediction.value(ATT_PREDICTION_ROWID); if (rowid != iInstanceNr) { throw new Exception("Predictions need to be done in the same order as the dataset. " + "Could not find prediction for instance #" + iInstanceNr + ". Found prediction for instance #" + rowid + " instead."); } double[] confidences = InstancesHelper.predictionToConfidences(datasetStructure, currentPrediction, ATT_PREDICTION_CONFIDENCE, ATT_PREDICTION_PREDICTION); // TODO: we might want to throw an error if the sum of confidences is not 1.0. Not now though. confidences = InstancesHelper.toProbDist(confidences); // TODO: security, we might be more picky later on and requiring real prob distributions. try { globalEvaluator.evaluateModelOnceAndRecordPrediction(confidences, currentInstance); } catch (ArrayIndexOutOfBoundsException aiobe) { throw new Exception( "ArrayIndexOutOfBoundsException: This is an error that occurs when the classifier returns negative values. "); } } globalMeasures = Output.evaluatorToMap(globalEvaluator, nrOfClasses, TaskType.TESTTHENTRAIN); }