private static Prediction voteOnNumericFeature(List<NumericPrediction> predictions, double[] weights) { DoubleWeightedMean mean = new DoubleWeightedMean(); for (int i = 0; i < predictions.size(); i++) { mean.increment(predictions.get(i).getPrediction(), weights[i]); } return new NumericPrediction(mean.getResult(), (int) mean.getN()); }
@Override public String predict(String[] example) { Prediction prediction = makePrediction(example); if (inputSchema.isClassification()) { int targetIndex = inputSchema.getTargetFeatureIndex(); Map<Integer,String> targetEncodingName = encodings.getEncodingValueMap(targetIndex); int mostProbable = ((CategoricalPrediction) prediction).getMostProbableCategoryEncoding(); return targetEncodingName.get(mostProbable); } else { double score = ((NumericPrediction) prediction).getPrediction(); return Double.toString(score); } }
static double rmse(DecisionForest forest, JavaRDD<Example> examples) { double mse = examples.mapToDouble(example -> { NumericPrediction prediction = (NumericPrediction) forest.predict(example); NumericFeature target = (NumericFeature) example.getTarget(); double diff = prediction.getPrediction() - target.getValue(); return diff * diff; }).mean(); return Math.sqrt(mse); }
@Test public void testUpdate2() { NumericPrediction prediction = new NumericPrediction(1.5, 1); prediction.update(3.5, 3); assertEquals(3.0, prediction.getPrediction()); }
@Test public void testFindByID() { DecisionTree tree = buildTestTree(); TerminalNode node = (TerminalNode) tree.findByID("r-+"); assertEquals(1.0, ((NumericPrediction) node.getPrediction()).getPrediction()); }
@Test public void testConstruct() { NumericPrediction prediction = new NumericPrediction(1.5, 1); assertEquals(FeatureType.NUMERIC, prediction.getFeatureType()); assertEquals(1.5, prediction.getPrediction()); }
@Test public void testNumericVoteWeighted() { List<NumericPrediction> predictions = Arrays.asList( new NumericPrediction(1.0, 1), new NumericPrediction(3.0, 2), new NumericPrediction(6.0, 3) ); double[] weights = {3.0, 2.0, 1.0}; NumericPrediction vote = (NumericPrediction) WeightedPrediction.voteOnFeature(predictions, weights); assertEquals(FeatureType.NUMERIC, vote.getFeatureType()); assertEquals(15.0 / 6.0, vote.getPrediction()); }
@Test public void testPredict() { DecisionForest forest = buildTestForest(); NumericPrediction prediction = (NumericPrediction) forest.predict(new Example(null, NumericFeature.forValue(0.5))); assertEquals(1.0, prediction.getPrediction()); }
encoding.getValueEncodingMap(3).get(f3 == 1 ? "A" : "B")); Example toPredict = new Example(null, null, feature1, feature2, feature3); double prediction = ((NumericPrediction) forest.predict(toPredict)).getPrediction(); assertEquals("Incorrect prediction " + prediction + " for " + toPredict, f1 + f2 + f3, Math.round(prediction));
@Test public void testPredict() { DecisionTree tree = buildTestTree(); NumericPrediction prediction = (NumericPrediction) tree.predict(new Example(null, NumericFeature.forValue(0.5))); assertEquals(1.0, prediction.getPrediction()); }
@Test public void testUpdate() { NumericPrediction prediction = new NumericPrediction(1.5, 1); Example example = new Example(NumericFeature.forValue(2.5)); prediction.update(example); assertEquals(2.0, prediction.getPrediction()); }
@Test public void testNumericVote() { List<NumericPrediction> predictions = Arrays.asList( new NumericPrediction(1.0, 1), new NumericPrediction(3.0, 2), new NumericPrediction(6.0, 3) ); double[] weights = {1.0, 1.0, 1.0}; NumericPrediction vote = (NumericPrediction) WeightedPrediction.voteOnFeature(predictions, weights); assertEquals(FeatureType.NUMERIC, vote.getFeatureType()); assertEquals(10.0 /3.0, vote.getPrediction()); }
@Test public void testFindTerminal() { DecisionTree tree = buildTestTree(); TerminalNode node = tree.findTerminal(new Example(null, NumericFeature.forValue(0.5))); NumericPrediction prediction = (NumericPrediction) node.getPrediction(); assertEquals(1.0, prediction.getPrediction()); }
private static Prediction voteOnNumericFeature(List<NumericPrediction> predictions, double[] weights) { DoubleWeightedMean mean = new DoubleWeightedMean(); for (int i = 0; i < predictions.size(); i++) { mean.increment(predictions.get(i).getPrediction(), weights[i]); } return new NumericPrediction(mean.getResult(), (int) mean.getN()); }