/** Test of learn method, of class CategoryBalancedBaggingLearner. */ @Test public void testLearn() { CategoryBalancedBaggingLearner<Vector, Boolean> instance = new CategoryBalancedBaggingLearner<Vector, Boolean>(); instance.setLearner(new Perceptron()); instance.setRandom(random); instance.setMaxIterations(5); instance.setPercentToSample(0.5); assertNull(instance.getResult()); ArrayList<InputOutputPair<Vector, Boolean>> data = new ArrayList<InputOutputPair<Vector, Boolean>>(); VectorFactory<?> vectorFactory = VectorFactory.getDefault(); for (int i = 0; i < 100; i++) { data.add( new DefaultInputOutputPair<Vector, Boolean>( vectorFactory.createUniformRandom(14, 0.0, 1.0, random), true)); } for (int i = 0; i < 2; i++) { data.add( new DefaultInputOutputPair<Vector, Boolean>( vectorFactory.createUniformRandom(14, -1.0, 0.0, random), false)); } WeightedVotingCategorizerEnsemble<Vector, Boolean, ?> result = instance.learn(data); assertSame(result, instance.getResult()); assertEquals(5, result.getMembers().size()); for (WeightedValue<?> member : result.getMembers()) { assertEquals(1.0, member.getWeight(), 0.0); assertNotNull(member.getValue()); assertTrue(member.getValue() instanceof LinearBinaryCategorizer); } }
/** * Creates a new instance of LaplaceDistribution using a weighted Maximum Likelihood estimate * based on the given data * * @param data Weighed pairs of data (first is data, second is weight) that was generated by * some unknown LaplaceDistribution distribution * @return Maximum Likelihood UnivariateGaussian that generated the data */ @Override public LaplaceDistribution learn( final Collection<? extends WeightedValue<? extends Double>> data) { double mean = 0.0; double weightSum = 0.0; for (WeightedValue<? extends Double> weightedValue : data) { double weight = weightedValue.getWeight(); if (weight != 0.0) { double value = weightedValue.getValue().doubleValue(); mean += weight * value; weightSum += weight; } } if (weightSum != 0.0) { mean /= weightSum; } // Now compute the shape factor double shape = 0.0; for (WeightedValue<? extends Number> weightedValue : data) { double weight = weightedValue.getWeight(); if (weight != 0.0) { double value = weightedValue.getValue().doubleValue(); double delta = value - mean; shape += weight * Math.abs(delta); } } if (weightSum != 0.0) { shape /= weightSum; } return new LaplaceDistribution(mean, shape); }