Data implements Cloneable { public int majorityLabel(Random rng) { int[] counts = new int[dataset.nblabels()]; for (int index = 0; index < size(); index++) { counts[(int) dataset.getLabel(get(index))]++; } return DataUtils.maxindex(rng, counts); } Data(Dataset dataset);  Data(Dataset dataset, List<Instance> instances); int size(); boolean isEmpty(); boolean contains(Instance v); Instance get(int index); Data subset(Condition condition); Data bagging(Random rng); Data bagging(Random rng, boolean[] sampled); Data rsplit(Random rng, int subsize); boolean isIdentical(); boolean identicalLabel(); double[] values(int attr); @Override Data clone(); @Override boolean equals(Object obj); @Override int hashCode(); double[] extractLabels(); int majorityLabel(Random rng); void countLabels(int[] counts); Dataset getDataset();  }
@Test public void testMajorityLabel() throws Exception { String descriptor = Utils.randomDescriptor(rng, ATTRIBUTE_COUNT); int label = Utils.findLabel(descriptor); int label1 = rng.nextInt(); double[][] source = Utils.randomDoublesWithSameLabel(rng, descriptor, false, 100, label1); String[] sData = Utils.double2String(source); Dataset dataset = DataLoader.generateDataset(descriptor, false, sData); Data data = DataLoader.loadData(dataset, sData); int code1 = dataset.labelCode(Double.toString(label1)); assertEquals(code1, data.majorityLabel(rng)); int label2 = label1 + 1; int nblabel2 = 51; while (nblabel2 > 0) { double[] vector = source[rng.nextInt(100)]; if (vector[label] != label2) { vector[label] = label2; nblabel2--; } } sData = Utils.double2String(source); dataset = DataLoader.generateDataset(descriptor, false, sData); data = DataLoader.loadData(dataset, sData); int code2 = dataset.labelCode(Double.toString(label2)); assertEquals(code2, data.majorityLabel(rng)); do { double[] vector = source[rng.nextInt(100)]; if (vector[label] == label2) { vector[label] = label1; break; } } while (true); sData = Utils.double2String(source); data = DataLoader.loadData(dataset, sData); code1 = dataset.labelCode(Double.toString(label1)); code2 = dataset.labelCode(Double.toString(label2)); boolean found1 = false; boolean found2 = false; for (int index = 0; index < 10 && (!found1 || !found2); index++) { int major = data.majorityLabel(rng); if (major == code1) { found1 = true; } if (major == code2) { found2 = true; } } assertTrue(found1 && found2); }