DecisionForest implements Writable { public void classify(Data data, double[][] predictions) { Preconditions.checkArgument(data.size() == predictions.length, "predictions.length must be equal to data.size()"); if (data.isEmpty()) { return; } int treeId = 0; for (Node tree : trees) { for (int index = 0; index < data.size(); index++) { if (predictions[index] == null) { predictions[index] = new double[trees.size()]; } predictions[index][treeId] = tree.classify(data.get(index)); } treeId++; } } private  DecisionForest();  DecisionForest(List<Node> trees); void classify(Data data, double[][] predictions); double classify(Dataset dataset, Random rng, Instance instance); long meanNbNodes(); long nbNodes(); long meanMaxDepth(); @Override boolean equals(Object obj); @Override int hashCode(); @Override void write(DataOutput dataOutput); @Override void readFields(DataInput dataInput); static DecisionForest read(DataInput dataInput); static DecisionForest load(Configuration conf, Path forestPath);  }
@Test public void testClassify() throws DescriptorException { Data[] datas = generateTrainingDataA(); DecisionForest forest = buildForest(datas); Data testData = DataLoader.loadData(datas[0].getDataset(), TEST_DATA); assertEquals(1.0, forest.classify(testData.getDataset(), rng, testData.get(0)), EPSILON); assertEquals(0.0, forest.classify(testData.getDataset(), rng, testData.get(1)), EPSILON); assertEquals(1.0, forest.classify(testData.getDataset(), rng, testData.get(2)), EPSILON); }
@Test public void testClassifyData() throws DescriptorException { Data[] datas = generateTrainingDataA(); DecisionForest forest = buildForest(datas); Data testData = DataLoader.loadData(datas[0].getDataset(), TEST_DATA); double[][] predictions = new double[testData.size()][]; forest.classify(testData, predictions); assertArrayEquals(new double[][]{{1.0, Double.NaN, Double.NaN}, {1.0, 0.0, Double.NaN}, {1.0, 1.0, Double.NaN}}, predictions); }
@Test public void testRegression() throws DescriptorException { Data[] datas = generateTrainingDataB(); DecisionForest[] forests = new DecisionForest[datas.length]; for (int i = 0; i < datas.length; i++) { Data[] subDatas = new Data[datas.length - 1]; int k = 0; for (int j = 0; j < datas.length; j++) { if (j != i) { subDatas[k] = datas[j]; k++; } } forests[i] = buildForest(subDatas); } double[][] predictions = new double[datas[0].size()][]; forests[0].classify(datas[0], predictions); assertArrayEquals(new double[]{20.0, 20.0}, predictions[0], EPSILON); assertArrayEquals(new double[]{39.0, 29.0}, predictions[1], EPSILON); assertArrayEquals(new double[]{Double.NaN, 29.0}, predictions[2], EPSILON); assertArrayEquals(new double[]{Double.NaN, 23.0}, predictions[17], EPSILON); predictions = new double[datas[1].size()][]; forests[1].classify(datas[1], predictions); assertArrayEquals(new double[]{30.0, 29.0}, predictions[19], EPSILON); predictions = new double[datas[2].size()][]; forests[2].classify(datas[2], predictions); assertArrayEquals(new double[]{29.0, 28.0}, predictions[9], EPSILON); assertEquals(20.0, forests[0].classify(datas[0].getDataset(), rng, datas[0].get(0)), EPSILON); assertEquals(34.0, forests[0].classify(datas[0].getDataset(), rng, datas[0].get(1)), EPSILON); assertEquals(29.0, forests[0].classify(datas[0].getDataset(), rng, datas[0].get(2)), EPSILON); }
@Test public void testClassify() throws DescriptorException { Data[] datas = generateTrainingDataA(); DecisionForest forest = buildForest(datas); Dataset dataset = datas[0].getDataset(); Data testData = DataLoader.loadData(dataset, TEST_DATA); double noValue = dataset.valueOf(4, "no"); double yesValue = dataset.valueOf(4, "yes"); assertEquals(noValue, forest.classify(testData.getDataset(), rng, testData.get(0)), EPSILON); assertEquals(noValue, forest.classify(testData.getDataset(), rng, testData.get(2)), EPSILON); }
@Test public void testClassifyData() throws DescriptorException { Data[] datas = generateTrainingDataA(); DecisionForest forest = buildForest(datas); Dataset dataset = datas[0].getDataset(); Data testData = DataLoader.loadData(dataset, TEST_DATA); double[][] predictions = new double[testData.size()][]; forest.classify(testData, predictions); double noValue = dataset.valueOf(4, "no"); double yesValue = dataset.valueOf(4, "yes"); assertArrayEquals(new double[][]{{noValue, Double.NaN, Double.NaN}, {noValue, yesValue, Double.NaN}, {noValue, noValue, Double.NaN}}, predictions); }