### Question:
InformationGainFeatureRanker extends AbstractFeatureRanker { @Override public FeatureRanking rankFeatures(Dataset dataset, ProgressReporter progress) { Validate.notNull(dataset, "dataset must not be null"); Map<String, Double> informationGainValues = new HashMap<>(); progress.startTask("Information Gain", -1); LOGGER.debug("Calculating discretization"); Discretization discretization = new Discretization(dataset, progress.createSubProgress(0.5)); Dataset preparedData = dataset.transform(discretization); NominalValueStatistics categoryStatistics = new DatasetStatistics(dataset).getCategoryStatistics(); CategoryEntries categoryCounts = new CategoryEntriesBuilder(categoryStatistics.getMap()).create(); double entropy = categoryCounts.getEntropy(); Set<String> featureNames = dataset.getFeatureInformation().getFeatureNames(); ProgressReporter informationGainProgress = progress.createSubProgress(0.5); LOGGER.debug("Calculating gain"); informationGainProgress.startTask("Calculating gain", featureNames.size()); for (String featureName : featureNames) { double gain = entropy - conditionalEntropy(featureName, preparedData); informationGainValues.put(featureName, gain); informationGainProgress.increment(); } informationGainProgress.finishTask(); return new FeatureRanking(informationGainValues); }  @Override FeatureRanking rankFeatures(Dataset dataset, ProgressReporter progress);  }### Answer:
@Test public void testInformationGain_playData() throws FileNotFoundException { File testFile = getResourceFile("/classifier/playData.txt"); List<Instance> dataset = new CsvDatasetReader(testFile, true).readAll(); FeatureRanking result = new InformationGainFeatureRanker().rankFeatures(dataset, new ProgressMonitor()); assertEquals(0.2467, result.getFeature("Outlook").getScore(), DELTA); assertEquals(0.1518, result.getFeature("Humidity").getScore(), DELTA); assertEquals(0.0481, result.getFeature("Windy").getScore(), DELTA); assertEquals(0.0292, result.getFeature("Temperature").getScore(), DELTA); }
@Test public void testInformationGain_wineData() throws FileNotFoundException { File testFile = getResourceFile("/classifier/wineData.csv"); List<Instance> dataset = new CsvDatasetReader(testFile).readAll(); FeatureRanking result = new InformationGainFeatureRanker().rankFeatures(dataset, new ProgressMonitor()); assertEquals(1.0151, result.getFeature("flavanoids").getScore(), DELTA); assertEquals(0.8278, result.getFeature("proline").getScore(), DELTA); assertEquals(0.7438, result.getFeature("colorIntensity").getScore(), DELTA); assertEquals(0.7221, result.getFeature("od280/od315ofDilutedWines").getScore(), DELTA); assertEquals(0.6324, result.getFeature("hue").getScore(), DELTA); assertEquals(0.6034, result.getFeature("alcohol").getScore(), DELTA); assertEquals(0.5795, result.getFeature("totalPhenols").getScore(), DELTA); assertEquals(0.4306, result.getFeature("malicAcid").getScore(), DELTA); assertEquals(0.2772, result.getFeature("alcalinityOfAsh").getScore(), DELTA); assertEquals(0.2653, result.getFeature("proanthocyanins").getScore(), DELTA); assertEquals(0.2614, result.getFeature("magnesium").getScore(), DELTA); assertEquals(0.2198, result.getFeature("nonflavanoidPhenols").getScore(), DELTA); assertEquals(0.1649, result.getFeature("ash").getScore(), DELTA); }