ClassificationEvaluationUtil implements Serializable { public static Tuple2<Map<Object, Integer>, Object[]> buildLabelIndexLabelArray(HashSet<Object> set, boolean binary, String positiveValue, TypeInformation labelType) { Object[] labels = set.toArray(); Arrays.sort(labels, Collections.reverseOrder()); Preconditions.checkArgument(labels.length >= 2, "The distinct label number less than 2!"); Preconditions.checkArgument(!binary || labels.length == BINARY_LABEL_NUMBER, "The number of labels must be equal to 2!"); Map<Object, Integer> map = new HashMap<>(labels.length); if (binary && null != positiveValue) { if (EvaluationUtil.labelCompare(labels[1], positiveValue, labelType)) { Object tmp = labels[1]; labels[1] = labels[0]; labels[0] = tmp; } else if (!EvaluationUtil.labelCompare(labels[0], positiveValue, labelType)) { throw new IllegalArgumentException("Not contain positiveValue"); } map.put(labels[0], 0); map.put(labels[1], 1); } else { for (int i = 0; i < labels.length; i++) { map.put(labels[i], i); } } return Tuple2.of(map, labels); }  static Tuple3<Double, Boolean, Double> getBinaryDetailStatistics(Row row,
                                                                            Object[] tuple,
                                                                            TypeInformation labelType); static Tuple2<Map<Object, Integer>, Object[]> buildLabelIndexLabelArray(HashSet<Object> set,
                                                                                   boolean binary,
                                                                                   String positiveValue,
                                                                                   TypeInformation labelType); static Tuple2<Boolean, long[]> reduceBinaryPartitionSummary(List<BinaryPartitionSummary> values, int taskId); static boolean isMiddlePoint(Tuple3<Double, Boolean, Double> t); static void updateBinaryPartitionSummary(BinaryPartitionSummary statistics,
                                                    Tuple3<Double, Boolean, Double> t); static void updateAccurateBinaryMetricsSummary(Tuple3<Double, Boolean, Double> cur,
                                                          AccurateBinaryMetricsSummary binaryMetricsSummary,
                                                          long[] countValues,
                                                          double[] recordValues,
                                                          boolean first); static int DETAIL_BIN_NUMBER; static int TOTAL_TRUE; static int TOTAL_FALSE; static int CUR_TRUE; static int CUR_FALSE; static int RECORD_LEN; static int BINARY_LABEL_NUMBER; static Tuple3<Double, Boolean, Double> middlePoint; }
@Test public void predResultLabelMapException() { HashSet<Object> set = new HashSet <>(); set.add("0"); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("The distinct label number less than 2!"); ClassificationEvaluationUtil.buildLabelIndexLabelArray(set, true, null, Types.INT); }
@Test public void predResultLabelMapTest() { HashSet <Object> set = new HashSet <>(); set.add("1.0"); set.add("0.0"); Tuple2<Map<Object, Integer>, Object[]> tuple2 = ClassificationEvaluationUtil.buildLabelIndexLabelArray(set, true, null, Types.DOUBLE); Map<Object, Integer> map = tuple2.f0; Assert.assertEquals(map.size(), 2); Assert.assertEquals((int) map.get("0.0"), 1); Assert.assertEquals((int) map.get("1.0"), 0); tuple2 = ClassificationEvaluationUtil.buildLabelIndexLabelArray(set, true, "0", Types.DOUBLE); map = tuple2.f0; Assert.assertEquals(map.size(), 2); Assert.assertEquals((int) map.get("0.0"), 0); Assert.assertEquals((int) map.get("1.0"), 1); set.add("2.0"); tuple2 = ClassificationEvaluationUtil.buildLabelIndexLabelArray(set, false, null, Types.DOUBLE); map = tuple2.f0; Assert.assertEquals(map.size(), 3); Assert.assertEquals((int) map.get("0.0"), 2); Assert.assertEquals((int) map.get("1.0"), 1); Assert.assertEquals((int) map.get("2.0"), 0); Assert.assertArrayEquals(new String[]{"2.0", "1.0", "0.0"}, tuple2.f1); }