use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.
the class PostProcessTest method populatePredictedOutputField2.
@Test
public void populatePredictedOutputField2() {
KiePMMLNameValue kiePMMLNameValue = new KiePMMLNameValue("targetField", 54346.32454);
KiePMMLOutputField outputField = KiePMMLOutputField.builder(OUTPUT_NAME, Collections.emptyList()).withResultFeature(RESULT_FEATURE.PREDICTED_VALUE).withTargetField(kiePMMLNameValue.getName()).build();
KiePMMLTestingModel kiePMMLModel = testingModelBuilder(outputField).build();
ProcessingDTO processingDTO = buildProcessingDTOWithNameValues(kiePMMLModel, kiePMMLNameValue);
PMML4Result toUpdate = new PMML4Result();
PostProcess.populateOutputFields(toUpdate, processingDTO);
assertFalse(toUpdate.getResultVariables().isEmpty());
assertTrue(toUpdate.getResultVariables().containsKey(OUTPUT_NAME));
assertEquals(kiePMMLNameValue.getValue(), toUpdate.getResultVariables().get(OUTPUT_NAME));
}
use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.
the class PostProcess method populateOutputFields.
/**
* Populated the <code>PMML4Result</code> with <code>OutputField</code> results
* @param toUpdate
* @param processingDTO
*/
static void populateOutputFields(final PMML4Result toUpdate, final ProcessingDTO processingDTO) {
logger.debug("populateOutputFields {} {}", toUpdate, processingDTO);
for (KiePMMLOutputField outputField : processingDTO.getOutputFields()) {
Object variableValue = outputField.evaluate(processingDTO);
if (variableValue != null) {
String variableName = outputField.getName();
toUpdate.addResultVariable(variableName, variableValue);
processingDTO.addKiePMMLNameValue(new KiePMMLNameValue(variableName, variableValue));
}
}
}
use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.
the class PMMLMiningModelEvaluator method populateInputDataWithSegmentResult.
void populateInputDataWithSegmentResult(final PMML4Result pmml4Result, final PMMLContext pmmlContext, final MULTIPLE_MODEL_METHOD multipleModelMethod, final KiePMMLSegment segment, final LinkedHashMap<String, KiePMMLNameValueProbabilityMapTuple> toPopulate) {
pmml4Result.getResultVariables().forEach((s, o) -> pmmlContext.getRequestData().addRequestParam(s, o));
PMML4ResultProbabilityMapTuple pmml4ResultTuple = new PMML4ResultProbabilityMapTuple(pmml4Result, pmmlContext.getProbabilityMap());
KiePMMLNameValue predictionValue = getKiePMMLNameValue(pmml4ResultTuple.pmml4Result, multipleModelMethod, segment.getWeight());
List<KiePMMLNameValue> probabilityValues = getKiePMMLNameValues(pmml4ResultTuple.probabilityResultMap, multipleModelMethod, segment.getWeight());
toPopulate.put(segment.getId(), new KiePMMLNameValueProbabilityMapTuple(predictionValue, probabilityValues));
addStep(() -> getStep(segment, pmml4Result), pmmlContext);
}
use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.
the class PMMLMiningModelEvaluatorTest method getPMML4ResultOK.
@Test
public void getPMML4ResultOK() {
String name = "NAME";
String targetField = "TARGET";
String prediction = "FIRST_VALUE";
KiePMMLSegmentation kiePMMLSegmentation = KiePMMLSegmentation.builder("SEGM_1", Collections.emptyList(), SELECT_FIRST).build();
KiePMMLMiningModel kiePMMLMiningModel = KiePMMLMiningModel.builder(name, Collections.emptyList(), MINING_FUNCTION.ASSOCIATION_RULES).withTargetField(targetField).withSegmentation(kiePMMLSegmentation).build();
final LinkedHashMap<String, PMMLMiningModelEvaluator.KiePMMLNameValueProbabilityMapTuple> inputData = new LinkedHashMap<>();
inputData.put("FIRST_KEY", new PMMLMiningModelEvaluator.KiePMMLNameValueProbabilityMapTuple(new KiePMMLNameValue("FIRST_NAME", prediction), new ArrayList<>()));
inputData.put("SECOND_KEY", new PMMLMiningModelEvaluator.KiePMMLNameValueProbabilityMapTuple(new KiePMMLNameValue("SECOND_NAME", "SECOND_VALUE"), new ArrayList<>()));
PMML4Result retrieved = evaluator.getPMML4Result(kiePMMLMiningModel, inputData, new PMMLContextTest());
assertNotNull(retrieved);
assertEquals(OK.getName(), retrieved.getResultCode());
assertEquals(targetField, retrieved.getResultObjectName());
final Map<String, Object> resultVariables = retrieved.getResultVariables();
assertTrue(resultVariables.containsKey(targetField));
assertEquals(prediction, resultVariables.get(targetField));
}
use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue 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