Precision extends ClassMetric<L> { @Override public double score(Iterator<LabelPair<L>> it) { if (clsLb != null) { long tp = 0; long fp = 0; while (it.hasNext()) { LabelPair<L> e = it.next(); L prediction = e.getPrediction(); L truth = e.getTruth(); if (clsLb.equals(prediction)) { if (prediction.equals(truth)) tp++; else fp++; } } long denominator = tp + fp; if (denominator == 0) return 1; return (double)tp / denominator; } else return Double.NaN; } Precision(L clsLb); @Override double score(Iterator<LabelPair<L>> it); @Override String name();  }
@Test public void testScore() { Precision<Integer> scoreCalculator = new Precision<>(0); 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); }