use of org.jpmml.converter.FlagManager in project jpmml-r by jpmml.
the class GBMConverter method encodeNode.
private Node encodeNode(int i, Predicate predicate, RGenericVector tree, RGenericVector c_splits, FlagManager flagManager, CategoryManager categoryManager, Schema schema) {
Integer id = Integer.valueOf(i + 1);
RIntegerVector splitVar = tree.getIntegerValue(0);
RDoubleVector splitCodePred = tree.getDoubleValue(1);
RIntegerVector leftNode = tree.getIntegerValue(2);
RIntegerVector rightNode = tree.getIntegerValue(3);
RIntegerVector missingNode = tree.getIntegerValue(4);
RDoubleVector prediction = tree.getDoubleValue(7);
Integer var = splitVar.getValue(i);
if (var == -1) {
Double value = prediction.getValue(i);
Node result = new LeafNode(value, predicate).setId(id);
return result;
}
Boolean isMissing;
FlagManager missingFlagManager = flagManager;
FlagManager nonMissingFlagManager = flagManager;
Predicate missingPredicate;
Feature feature = schema.getFeature(var);
{
String name = feature.getName();
isMissing = flagManager.getValue(name);
if (isMissing == null) {
missingFlagManager = missingFlagManager.fork(name, Boolean.TRUE);
nonMissingFlagManager = nonMissingFlagManager.fork(name, Boolean.FALSE);
}
missingPredicate = createSimplePredicate(feature, SimplePredicate.Operator.IS_MISSING, null);
}
CategoryManager leftCategoryManager = categoryManager;
CategoryManager rightCategoryManager = categoryManager;
Predicate leftPredicate;
Predicate rightPredicate;
Double split = splitCodePred.getValue(i);
if (feature instanceof CategoricalFeature) {
CategoricalFeature categoricalFeature = (CategoricalFeature) feature;
String name = categoricalFeature.getName();
List<?> values = categoricalFeature.getValues();
int index = ValueUtil.asInt(split);
RIntegerVector c_split = c_splits.getIntegerValue(index);
List<Integer> splitValues = c_split.getValues();
java.util.function.Predicate<Object> valueFilter = categoryManager.getValueFilter(name);
List<Object> leftValues = selectValues(values, valueFilter, splitValues, true);
List<Object> rightValues = selectValues(values, valueFilter, splitValues, false);
leftCategoryManager = leftCategoryManager.fork(name, leftValues);
rightCategoryManager = rightCategoryManager.fork(name, rightValues);
leftPredicate = createPredicate(categoricalFeature, leftValues);
rightPredicate = createPredicate(categoricalFeature, rightValues);
} else {
ContinuousFeature continuousFeature = feature.toContinuousFeature();
leftPredicate = createSimplePredicate(continuousFeature, SimplePredicate.Operator.LESS_THAN, split);
rightPredicate = createSimplePredicate(continuousFeature, SimplePredicate.Operator.GREATER_OR_EQUAL, split);
}
Node result = new BranchNode(null, predicate).setId(id);
List<Node> nodes = result.getNodes();
Integer missing = missingNode.getValue(i);
if (missing != -1 && (isMissing == null || isMissing)) {
Node missingChild = encodeNode(missing, missingPredicate, tree, c_splits, missingFlagManager, categoryManager, schema);
nodes.add(missingChild);
}
Integer left = leftNode.getValue(i);
if (left != -1 && (isMissing == null || !isMissing)) {
Node leftChild = encodeNode(left, leftPredicate, tree, c_splits, nonMissingFlagManager, leftCategoryManager, schema);
nodes.add(leftChild);
}
Integer right = rightNode.getValue(i);
if (right != -1 && (isMissing == null || !isMissing)) {
Node rightChild = encodeNode(right, rightPredicate, tree, c_splits, nonMissingFlagManager, rightCategoryManager, schema);
nodes.add(rightChild);
}
return result;
}
use of org.jpmml.converter.FlagManager in project jpmml-r by jpmml.
the class GBMConverter method encodeTreeModel.
private TreeModel encodeTreeModel(MiningFunction miningFunction, RGenericVector tree, RGenericVector c_splits, Schema schema) {
Node root = encodeNode(0, True.INSTANCE, tree, c_splits, new FlagManager(), new CategoryManager(), schema);
TreeModel treeModel = new TreeModel(miningFunction, ModelUtil.createMiningSchema(schema.getLabel()), root).setSplitCharacteristic(TreeModel.SplitCharacteristic.MULTI_SPLIT);
return treeModel;
}
Aggregations