Search in sources :

Example 1 with KiePMMLValueWeight

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);
    });
}
Also used : KiePMMLValueWeight(org.kie.pmml.commons.model.tuples.KiePMMLValueWeight) PMMLContextTest(org.kie.pmml.commons.testingutility.PMMLContextTest) Test(org.junit.Test)

Example 2 with KiePMMLValueWeight

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

Example 3 with KiePMMLValueWeight

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));
    }
}
Also used : IntStream(java.util.stream.IntStream) MAX(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.MAX) Arrays(java.util.Arrays) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) WEIGHTED_AVERAGE(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.WEIGHTED_AVERAGE) ArrayList(java.util.ArrayList) SELECT_ALL(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.SELECT_ALL) LinkedHashMap(java.util.LinkedHashMap) MEDIAN(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.MEDIAN) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) WEIGHTED_MAJORITY_VOTE(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.WEIGHTED_MAJORITY_VOTE) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) WEIGHTED_SUM(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.WEIGHTED_SUM) KiePMMLValueWeight(org.kie.pmml.commons.model.tuples.KiePMMLValueWeight) Assert.assertNotNull(org.junit.Assert.assertNotNull) MAJORITY_VOTE(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.MAJORITY_VOTE) SUM(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.SUM) KieEnumException(org.kie.pmml.api.exceptions.KieEnumException) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SELECT_FIRST(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.SELECT_FIRST) AVERAGE(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.AVERAGE) AbstractMap(java.util.AbstractMap) List(java.util.List) MODEL_CHAIN(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.MODEL_CHAIN) Stream(java.util.stream.Stream) Assert.assertEquals(org.junit.Assert.assertEquals) WEIGHTED_MEDIAN(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD.WEIGHTED_MEDIAN) KiePMMLValueWeight(org.kie.pmml.commons.model.tuples.KiePMMLValueWeight) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 4 with KiePMMLValueWeight

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;
}
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

KiePMMLValueWeight (org.kie.pmml.commons.model.tuples.KiePMMLValueWeight)4 Test (org.junit.Test)3 KiePMMLNameValue (org.kie.pmml.commons.model.tuples.KiePMMLNameValue)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 PMMLContextTest (org.kie.pmml.commons.testingutility.PMMLContextTest)2 AbstractMap (java.util.AbstractMap)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 PMML4Result (org.kie.api.pmml.PMML4Result)1