EvaluationUtil implements Serializable { public static MultiMetricsSummary getMultiClassMetrics(Iterable<Row> rows, Tuple2<Map<Object, Integer>, Object[]> labelIndexLabelArray) { final Object[] recordLabel = labelIndexLabelArray.f1; final Map<Object, Integer> labelIndexMap = labelIndexLabelArray.f0; final int n = recordLabel.length; MultiMetricsSummary multiMetricsSummary = new MultiMetricsSummary(new long[n][n], recordLabel, -1, 0L); for (Row r : rows) { if (checkRowFieldNotNull(r)) { multiMetricsSummary.total++; int label = labelIndexMap.get(r.getField(0)); int prediction = labelIndexMap.get(r.getField(1)); multiMetricsSummary.matrix.setValue(prediction, label, multiMetricsSummary.matrix.getValue(prediction, label) + 1); } } return multiMetricsSummary.total == 0 ? null : multiMetricsSummary; }  static boolean labelCompare(Object label1, Object label2, TypeInformation type); static int compare(Object o1, Object o2); static Object castTo(Object x, TypeInformation t); static BaseMetricsSummary getDetailStatistics(Iterable<Row> rows, String positiveValue, boolean binary, TypeInformation labelType); static BaseMetricsSummary getDetailStatistics(Iterable<Row> rows,
                                                         boolean binary,
                                                         Tuple2<Map<Object, Integer>, Object[]> labels,
                                                         TypeInformation labelType); static boolean checkRowFieldNotNull(Row row); static double extractLogloss(TreeMap<Object, Double> labelProbMap, Object label); static TreeMap<Object, Double> extractLabelProbMap(Row row, TypeInformation labelType); static void updateBinaryMetricsSummary(TreeMap<Object, Double> labelProbMap,
                                                  Object label,
                                                  BinaryMetricsSummary binaryMetricsSummary); static void updateMultiMetricsSummary(TreeMap<Object, Double> labelProbMap,
                                                 Object label,
                                                 Map<Object, Integer> labels,
                                                 MultiMetricsSummary multiMetricsSummary); static RegressionMetricsSummary getRegressionStatistics(Iterable<Row> rows); static MultiMetricsSummary getMultiClassMetrics(Iterable<Row> rows,
                                                           Tuple2<Map<Object, Integer>, Object[]>
                                                               labelIndexLabelArray);  }
@Test public void predResultMatrixTest(){ Row[] rows = new Row[] { Row.of("prefix0", "prefix2"), Row.of("prefix0", "prefix0"), Row.of("prefix1", "prefix1"), Row.of("prefix2", "prefix0"), Row.of("prefix2", "prefix0"), Row.of("prefix2", "prefix2"), Row.of("prefix2", "prefix0"), Row.of("prefix1", "prefix2"), Row.of("prefix1", "prefix1"), Row.of("prefix1", "prefix1") }; Map<Object, Integer> map = new HashMap<>(); map.put("prefix0", 2); map.put("prefix1", 1); map.put("prefix2", 0); MultiMetricsSummary metrics = EvaluationUtil.getMultiClassMetrics(Arrays.asList(rows), Tuple2.of(map, new Object[]{"prefix2", "prefix1", "prefix0"})); Assert.assertEquals(10, metrics.total); Assert.assertArrayEquals(new Object[] {"prefix2", "prefix1", "prefix0"}, metrics.labels); Assert.assertArrayEquals(new long[] {1, 1, 1}, metrics.matrix.getMatrix()[0]); Assert.assertArrayEquals(new long[] {0, 3, 0}, metrics.matrix.getMatrix()[1]); Assert.assertArrayEquals(new long[] {3, 0, 1}, metrics.matrix.getMatrix()[2]); }
@Test public void testZeroEffitiveData(){ Row[] rows = new Row[] { Row.of("prefix0", null) }; Map<Object, Integer> map = new HashMap<>(); map.put("prefix0", 2); map.put("prefix1", 1); map.put("prefix2", 0); MultiMetricsSummary metrics = EvaluationUtil.getMultiClassMetrics(Arrays.asList(rows), Tuple2.of(map, new Object[]{"prefix2", "prefix1", "prefix0"})); Assert.assertNull(metrics); }