use of ml.shifu.shifu.core.dtrain.dt.Node 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 ml.shifu.shifu.core.dtrain.dt.Node in project shifu by ShifuML.
the class TreeNodePmmlElementCreator method convert.
public org.dmg.pmml.tree.Node convert(Node node) {
org.dmg.pmml.tree.Node pmmlNode = new org.dmg.pmml.tree.Node();
pmmlNode.setId(String.valueOf(node.getId()));
pmmlNode.setDefaultChild(null);
pmmlNode.setPredicate(new True());
pmmlNode.setEmbeddedModel(null);
List<org.dmg.pmml.tree.Node> childList = pmmlNode.getNodes();
org.dmg.pmml.tree.Node left = convert(node.getLeft(), true, node.getSplit());
childList.add(left);
org.dmg.pmml.tree.Node right = convert(node.getRight(), false, node.getSplit());
childList.add(right);
return pmmlNode;
}
Aggregations