DefaultIgSplit extends IgSplit { protected double entropy(Data data) { double invDataSize = 1.0 / data.size(); if (counts == null) { counts = new int[data.getDataset().nblabels()]; } Arrays.fill(counts, 0); data.countLabels(counts); double entropy = 0.0; for (int label = 0; label < data.getDataset().nblabels(); label++) { int count = counts[label]; if (count == 0) { continue; } double p = count * invDataSize; entropy += -p * Math.log(p) / LOG2; } return entropy; }  @Override Split computeSplit(Data data, int attr);  }
@Test public void testEntropy() throws Exception { Random rng = RandomUtils.getRandom(); String descriptor = Utils.randomDescriptor(rng, NUM_ATTRIBUTES); int label = Utils.findLabel(descriptor); double[][] temp = Utils.randomDoublesWithSameLabel(rng, descriptor, false, 100, 0); String[] sData = Utils.double2String(temp); Dataset dataset = DataLoader.generateDataset(descriptor, false, sData); Data data = DataLoader.loadData(dataset, sData); DefaultIgSplit iG = new DefaultIgSplit(); double expected = 0.0 - 1.0 * Math.log(1.0) / Math.log(2.0); assertEquals(expected, iG.entropy(data), EPSILON); for (int index = 0; index < 50; index++) { temp[index][label] = 1.0; } sData = Utils.double2String(temp); dataset = DataLoader.generateDataset(descriptor, false, sData); data = DataLoader.loadData(dataset, sData); iG = new DefaultIgSplit(); expected = 2.0 * -0.5 * Math.log(0.5) / Math.log(2.0); assertEquals(expected, iG.entropy(data), EPSILON); for (int index = 0; index < 15; index++) { temp[index][label] = 2.0; } sData = Utils.double2String(temp); dataset = DataLoader.generateDataset(descriptor, false, sData); data = DataLoader.loadData(dataset, sData); iG = new DefaultIgSplit(); expected = -0.15 * Math.log(0.15) / Math.log(2.0) - 0.35 * Math.log(0.35) / Math.log(2.0) - 0.5 * Math.log(0.5) / Math.log(2.0); assertEquals(expected, iG.entropy(data), EPSILON); }