Search in sources :

Example 1 with SimplePredicate

use of org.dmg.pmml.SimplePredicate in project pyramid by cheng-li.

the class RegressionTree method encodePredicate.

private static Predicate encodePredicate(Feature feature, Node node, boolean left) {
    FieldName name = feature.getName();
    SimplePredicate.Operator operator;
    String value;
    if (feature instanceof BinaryFeature) {
        BinaryFeature binaryFeature = (BinaryFeature) feature;
        operator = (left ? SimplePredicate.Operator.NOT_EQUAL : SimplePredicate.Operator.EQUAL);
        value = binaryFeature.getValue();
    } else {
        ContinuousFeature continuousFeature = feature.toContinuousFeature();
        Number splitValue = node.getThreshold();
        DataType dataType = continuousFeature.getDataType();
        switch(dataType) {
            case INTEGER:
                splitValue = (int) (splitValue.floatValue() + 1f);
                break;
            case FLOAT:
                break;
            default:
                throw new IllegalArgumentException();
        }
        operator = (left ? SimplePredicate.Operator.LESS_OR_EQUAL : SimplePredicate.Operator.GREATER_THAN);
        value = ValueUtil.formatValue(splitValue);
    }
    SimplePredicate simplePredicate = new SimplePredicate(name, operator).setValue(value);
    return simplePredicate;
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) DataType(org.dmg.pmml.DataType) BinaryFeature(org.jpmml.converter.BinaryFeature) FieldName(org.dmg.pmml.FieldName) SimplePredicate(org.dmg.pmml.SimplePredicate)

Example 2 with SimplePredicate

use of org.dmg.pmml.SimplePredicate in project shifu by ShifuML.

the class TreeNodePmmlElementCreator method convert.

public org.dmg.pmml.tree.Node convert(Node node, boolean isLeft, Split split) {
    org.dmg.pmml.tree.Node pmmlNode = new org.dmg.pmml.tree.Node();
    pmmlNode.setId(String.valueOf(node.getId()));
    if (node.getPredict() != null) {
        pmmlNode.setScore(String.valueOf(treeModel.isClassification() ? node.getPredict().getClassValue() : node.getPredict().getPredict()));
    }
    pmmlNode.setDefaultChild(null);
    Predicate predicate = null;
    ColumnConfig columnConfig = this.columnConfigList.get(split.getColumnNum());
    if (columnConfig.isNumerical()) {
        SimplePredicate p = new SimplePredicate();
        p.setValue(String.valueOf(split.getThreshold()));
        // TODO, how to support segment variable in tree model, here should be changed
        p.setField(new FieldName(NormalUtils.getSimpleColumnName(columnConfig.getColumnName())));
        if (isLeft) {
            p.setOperator(SimplePredicate.Operator.fromValue("lessThan"));
        } else {
            p.setOperator(SimplePredicate.Operator.fromValue("greaterOrEqual"));
        }
        predicate = p;
    } else if (columnConfig.isCategorical()) {
        SimpleSetPredicate p = new SimpleSetPredicate();
        Set<Short> childCategories = split.getLeftOrRightCategories();
        // TODO, how to support segment variable in tree model, here should be changed
        p.setField(new FieldName(NormalUtils.getSimpleColumnName(columnConfig.getColumnName())));
        StringBuilder arrayStr = new StringBuilder();
        List<String> valueList = treeModel.getCategoricalColumnNameNames().get(columnConfig.getColumnNum());
        for (Short sh : childCategories) {
            if (sh >= valueList.size()) {
                arrayStr.append(" \"\"");
                continue;
            }
            String s = valueList.get(sh);
            arrayStr.append(" ");
            if (s.contains("\"")) {
                String tmp = s.replaceAll("\"", "\\\\\\\"");
                if (s.contains(" ")) {
                    arrayStr.append("\"");
                    arrayStr.append(tmp);
                    arrayStr.append("\"");
                } else {
                    arrayStr.append(tmp);
                }
            } else {
                if (s.contains(" ")) {
                    arrayStr.append("\"");
                    arrayStr.append(s);
                    arrayStr.append("\"");
                } else {
                    arrayStr.append(s);
                }
            }
        }
        Array array = new Array(Array.Type.fromValue("string"), arrayStr.toString().trim());
        p.setArray(array);
        if (isLeft) {
            if (split.isLeft()) {
                p.setBooleanOperator(SimpleSetPredicate.BooleanOperator.fromValue("isIn"));
            } else {
                p.setBooleanOperator(SimpleSetPredicate.BooleanOperator.fromValue("isNotIn"));
            }
        } else {
            if (split.isLeft()) {
                p.setBooleanOperator(SimpleSetPredicate.BooleanOperator.fromValue("isNotIn"));
            } else {
                p.setBooleanOperator(SimpleSetPredicate.BooleanOperator.fromValue("isIn"));
            }
        }
        predicate = p;
    }
    pmmlNode.setPredicate(predicate);
    if (node.getSplit() == null || node.isRealLeaf()) {
        return pmmlNode;
    }
    List<org.dmg.pmml.tree.Node> childList = pmmlNode.getNodes();
    org.dmg.pmml.tree.Node left = convert(node.getLeft(), true, node.getSplit());
    org.dmg.pmml.tree.Node right = convert(node.getRight(), false, node.getSplit());
    childList.add(left);
    childList.add(right);
    return pmmlNode;
}
Also used : Set(java.util.Set) ColumnConfig(ml.shifu.shifu.container.obj.ColumnConfig) Node(ml.shifu.shifu.core.dtrain.dt.Node) SimplePredicate(org.dmg.pmml.SimplePredicate) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) Array(org.dmg.pmml.Array) List(java.util.List) FieldName(org.dmg.pmml.FieldName)

Example 3 with SimplePredicate

use of org.dmg.pmml.SimplePredicate in project drools by kiegroup.

the class PMMLModelTestUtils method getRandomSimplePredicate.

public static SimplePredicate getRandomSimplePredicate(DataField dataField) {
    SimplePredicate toReturn = getRandomSimplePredicate();
    toReturn.setField(dataField.getName());
    toReturn.setValue(getRandomObject(dataField.getDataType()));
    return toReturn;
}
Also used : SimplePredicate(org.dmg.pmml.SimplePredicate)

Example 4 with SimplePredicate

use of org.dmg.pmml.SimplePredicate in project drools by kiegroup.

the class PMMLModelTestUtils method getRandomSimplePredicate.

public static SimplePredicate getRandomSimplePredicate() {
    FieldName fieldName = FieldName.create(RandomStringUtils.random(6, true, false));
    SimplePredicate toReturn = new SimplePredicate();
    toReturn.setField(fieldName);
    toReturn.setOperator(getRandomSimplePredicateOperator());
    toReturn.setValue(getRandomObject(getRandomDataType()));
    return toReturn;
}
Also used : FieldName(org.dmg.pmml.FieldName) SimplePredicate(org.dmg.pmml.SimplePredicate)

Example 5 with SimplePredicate

use of org.dmg.pmml.SimplePredicate in project drools by kiegroup.

the class KiePMMLSimplePredicateInstanceFactoryTest method getKiePMMLSimplePredicate.

@Test
public void getKiePMMLSimplePredicate() {
    List<Field<?>> fields = IntStream.range(0, 3).mapToObj(i -> getRandomDataField()).collect(Collectors.toList());
    final SimplePredicate toConvert = getRandomSimplePredicate((DataField) fields.get(0));
    final KiePMMLSimplePredicate retrieved = KiePMMLSimplePredicateInstanceFactory.getKiePMMLSimplePredicate(toConvert, fields);
    commonVerifyKiePMMLSimplePredicate(retrieved, toConvert, (DataField) fields.get(0));
}
Also used : IntStream(java.util.stream.IntStream) DataField(org.dmg.pmml.DataField) PMMLModelTestUtils.getRandomSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimplePredicate) List(java.util.List) SimplePredicate(org.dmg.pmml.SimplePredicate) Field(org.dmg.pmml.Field) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate) Test(org.junit.Test) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) Collectors(java.util.stream.Collectors) DataField(org.dmg.pmml.DataField) Field(org.dmg.pmml.Field) PMMLModelTestUtils.getRandomDataField(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDataField) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate) PMMLModelTestUtils.getRandomSimplePredicate(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicate) KiePMMLSimplePredicate(org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate) InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLSimplePredicate) Test(org.junit.Test)

Aggregations

SimplePredicate (org.dmg.pmml.SimplePredicate)30 Test (org.junit.Test)17 CompoundPredicate (org.dmg.pmml.CompoundPredicate)15 ArrayList (java.util.ArrayList)11 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)11 HashMap (java.util.HashMap)10 List (java.util.List)10 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)10 Collectors (java.util.stream.Collectors)9 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicate)9 Predicate (org.dmg.pmml.Predicate)8 KiePMMLSimplePredicate (org.kie.pmml.commons.model.predicates.KiePMMLSimplePredicate)7 KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)7 DataField (org.dmg.pmml.DataField)6 DataType (org.dmg.pmml.DataType)6 KiePMMLASTTestUtils.getPredicateASTFactoryData (org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData)6 Map (java.util.Map)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 KiePMMLCompoundPredicate (org.kie.pmml.commons.model.predicates.KiePMMLCompoundPredicate)5 KiePMMLSimpleSetPredicate (org.kie.pmml.commons.model.predicates.KiePMMLSimpleSetPredicate)5