Fmeasure extends ClassMetric<L> { @Override public double score(Iterator<LabelPair<L>> it) { if (clsLb != null) { long tp = 0; long fn = 0; long fp = 0; double recall; double precision; while (it.hasNext()) { LabelPair<L> e = it.next(); L prediction = e.getPrediction(); L truth = e.getTruth(); if (clsLb.equals(truth)) { if (prediction.equals(truth)) tp++; else fn++; } if (clsLb.equals(prediction) && !prediction.equals(truth)) fp++; } long denominatorRecall = tp + fn; long denominatorPrecision = tp + fp; recall = denominatorRecall == 0 ? 1 : (double)tp / denominatorRecall; precision = denominatorPrecision == 0 ? 1 : (double)tp / denominatorPrecision; return 2 * (recall * precision) / (recall + precision); } else return Double.NaN; } Fmeasure(L clsLb); @Override double score(Iterator<LabelPair<L>> it); @Override String name();  }
@Test public void testScore() { Fmeasure<Integer> scoreCalculator = new Fmeasure<>(1); LabelPairCursor<Integer> cursor = new TestLabelPairCursor<>( Arrays.asList(1, 0, 1, 0, 1, 0), Arrays.asList(1, 0, 0, 1, 1, 0) ); double score = scoreCalculator.score(cursor.iterator()); assertEquals((double)2 / 3, score, 1e-12); }