Search in sources :

Example 26 with KiePMMLNameValue

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));
}
Also used : CommonTestingUtility.getProcessingDTO(org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) PMML4Result(org.kie.api.pmml.PMML4Result) KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) KiePMMLTestingModel(org.kie.pmml.commons.testingutility.KiePMMLTestingModel) Test(org.junit.Test)

Example 27 with KiePMMLNameValue

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));
        }
    }
}
Also used : KiePMMLOutputField(org.kie.pmml.commons.model.KiePMMLOutputField) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue)

Example 28 with KiePMMLNameValue

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);
}
Also used : KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue)

Example 29 with KiePMMLNameValue

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));
}
Also used : PMML4Result(org.kie.api.pmml.PMML4Result) KiePMMLMiningModel(org.kie.pmml.models.mining.model.KiePMMLMiningModel) ArrayList(java.util.ArrayList) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) LinkedHashMap(java.util.LinkedHashMap) PMMLContextTest(org.kie.pmml.commons.testingutility.PMMLContextTest) KiePMMLSegmentation(org.kie.pmml.models.mining.model.segmentation.KiePMMLSegmentation) PMMLContextTest(org.kie.pmml.commons.testingutility.PMMLContextTest) Test(org.junit.Test)

Example 30 with KiePMMLNameValue

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) AtomicReference(java.util.concurrent.atomic.AtomicReference) LinkedHashMap(java.util.LinkedHashMap) KiePMMLValueWeight(org.kie.pmml.commons.model.tuples.KiePMMLValueWeight)

Aggregations

KiePMMLNameValue (org.kie.pmml.commons.model.tuples.KiePMMLNameValue)44 Test (org.junit.Test)37 CommonTestingUtility.getProcessingDTO (org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO)19 ProcessingDTO (org.kie.pmml.commons.model.ProcessingDTO)19 LinkedHashMap (java.util.LinkedHashMap)16 ArrayList (java.util.ArrayList)8 List (java.util.List)8 HashMap (java.util.HashMap)7 PMML4Result (org.kie.api.pmml.PMML4Result)7 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 IntStream (java.util.stream.IntStream)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 KiePMMLFieldRef (org.kie.pmml.commons.model.expressions.KiePMMLFieldRef)6 KiePMMLDerivedField (org.kie.pmml.commons.transformations.KiePMMLDerivedField)6 Collections (java.util.Collections)5 Assert.assertNotNull (org.junit.Assert.assertNotNull)5 Arrays (java.util.Arrays)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 PMMLContextTest (org.kie.pmml.commons.testingutility.PMMLContextTest)4