use of org.kie.pmml.commons.model.tuples.KiePMMLValueWeight in project drools by kiegroup.
the class PMMLMiningModelEvaluatorTest method getEventuallyWeightedResultValueWeightNumber.
@Test
public void getEventuallyWeightedResultValueWeightNumber() {
final Integer rawObject = 24;
final double weight = 2.23;
VALUE_WEIGHT_METHODS.forEach(multipleModelMethod -> {
Object retrieved = evaluator.getEventuallyWeightedResult(rawObject, multipleModelMethod, weight);
assertNotNull(retrieved);
assertTrue(retrieved instanceof KiePMMLValueWeight);
KiePMMLValueWeight kiePMMLValueWeight = (KiePMMLValueWeight) retrieved;
assertEquals(rawObject.doubleValue(), kiePMMLValueWeight.getValue(), 0.0);
assertEquals(weight, kiePMMLValueWeight.getWeight(), 0.0);
});
}
use of org.kie.pmml.commons.model.tuples.KiePMMLValueWeight in project drools by kiegroup.
the class PMMLMiningModelEvaluatorTest method getKiePMMLNameValueValueWeightNumber.
@Test
public void getKiePMMLNameValueValueWeightNumber() {
final Integer rawObject = 24;
final PMML4Result pmml4Result = getPMML4Result(rawObject);
final double weight = 2.23;
double expected = rawObject.doubleValue();
VALUE_WEIGHT_METHODS.forEach(multipleModelMethod -> {
KiePMMLNameValue retrieved = evaluator.getKiePMMLNameValue(pmml4Result, multipleModelMethod, weight);
assertNotNull(retrieved);
assertEquals(pmml4Result.getResultObjectName(), retrieved.getName());
assertNotNull(retrieved.getValue());
assertTrue(retrieved.getValue() instanceof KiePMMLValueWeight);
KiePMMLValueWeight kiePMMLValueWeight = (KiePMMLValueWeight) retrieved.getValue();
assertEquals(expected, kiePMMLValueWeight.getValue(), 0.0);
assertEquals(weight, kiePMMLValueWeight.getWeight(), 0.0);
});
}
use of org.kie.pmml.commons.model.tuples.KiePMMLValueWeight in project drools by kiegroup.
the class MULTIPLE_MODEL_METHODTest method applySELECT_ALLNumbers.
@Test
public void applySELECT_ALLNumbers() {
Map<String, Object> expectedKiePMMLValueWeightMap = getExpectedKiePMMLValueWeightMap(true);
LinkedHashMap<String, KiePMMLNameValue> inputData = (LinkedHashMap<String, KiePMMLNameValue>) expectedKiePMMLValueWeightMap.get("inputData");
List<Double> expected = inputData.values().stream().map(kiePMMLNameValue -> ((KiePMMLValueWeight) kiePMMLNameValue.getValue()).getValue()).collect(Collectors.toList());
List retrieved = (List) SELECT_ALL.applyPrediction(inputData);
assertEquals(expected.size(), retrieved.size());
for (Double expDouble : expected) {
assertTrue(retrieved.contains(expDouble));
}
expectedKiePMMLValueWeightMap = getExpectedKiePMMLValueWeightMap(false);
inputData = (LinkedHashMap<String, KiePMMLNameValue>) expectedKiePMMLValueWeightMap.get("inputData");
expected = inputData.values().stream().map(kiePMMLNameValue -> ((KiePMMLValueWeight) kiePMMLNameValue.getValue()).getValue()).collect(Collectors.toList());
retrieved = (List) SELECT_ALL.applyPrediction(inputData);
assertEquals(expected.size(), retrieved.size());
for (Double expDouble : expected) {
assertTrue(retrieved.contains(expDouble));
}
}
use of org.kie.pmml.commons.model.tuples.KiePMMLValueWeight in project drools by kiegroup.
the class MULTIPLE_MODEL_METHODTest method getExpectedKiePMMLValueWeightMap.
private Map<String, Object> getExpectedKiePMMLValueWeightMap(boolean evenNumberOfData) {
LinkedHashMap<String, KiePMMLNameValue> inputData = new LinkedHashMap<>();
int numberOfData = evenNumberOfData ? 8 : 9;
final List<KiePMMLValueWeight> valueWeightList = new ArrayList<>();
double seed = 0.35;
AtomicReference<Double> WEIGHT_SEED = new AtomicReference<>(seed);
IntStream.range(0, numberOfData).forEach(i -> {
double weight;
if (i < numberOfData - 1) {
weight = ThreadLocalRandom.current().nextDouble(0.0, (1 - WEIGHT_SEED.get()));
WEIGHT_SEED.accumulateAndGet(weight, Double::sum);
} else {
weight = 1 - WEIGHT_SEED.get() + seed;
}
double value = ThreadLocalRandom.current().nextDouble(3.0, 10.0);
valueWeightList.add(new KiePMMLValueWeight(value, weight));
});
double sum = 0;
double weightedSum = 0;
double weightsSum = 0;
int i = 0;
for (KiePMMLValueWeight valueWeight : valueWeightList) {
inputData.put("Value" + i, new KiePMMLNameValue("val-" + i, valueWeight));
sum += valueWeight.getValue();
weightedSum += valueWeight.weightedValue();
weightsSum += valueWeight.getWeight();
i++;
}
final Map<String, Object> toReturn = new HashMap<>();
toReturn.put("inputData", inputData);
toReturn.put("sum", sum);
toReturn.put("weightedSum", weightedSum);
double average = sum / inputData.values().size();
toReturn.put("average", average);
double weightedAverage = weightedSum / weightsSum;
toReturn.put("weightedAverage", weightedAverage);
// ordering by values
valueWeightList.sort((o1, o2) -> {
int toReturn1 = 0;
if (o1.getValue() > o2.getValue()) {
toReturn1 = 1;
} else if (o1.getValue() < o2.getValue()) {
toReturn1 = -1;
}
return toReturn1;
});
double median = 0;
if (evenNumberOfData) {
median = (valueWeightList.get(3).getValue() + valueWeightList.get(4).getValue()) / 2;
} else {
median = valueWeightList.get(4).getValue();
}
toReturn.put("median", median);
double halfWeight = weightsSum / 2;
double weightedMedianSum = 0;
double weightedMedian = 0;
for (KiePMMLValueWeight tuple : valueWeightList) {
weightedMedianSum += tuple.getWeight();
if (weightedMedianSum >= halfWeight) {
weightedMedian = tuple.getValue();
break;
}
}
toReturn.put("weightedMedian", weightedMedian);
double max = valueWeightList.get(valueWeightList.size() - 1).getValue();
toReturn.put("max", max);
return toReturn;
}
Aggregations