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