Search in sources :

Example 1 with FlagManager

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;
}
Also used : Node(org.dmg.pmml.tree.Node) BranchNode(org.dmg.pmml.tree.BranchNode) LeafNode(org.dmg.pmml.tree.LeafNode) FlagManager(org.jpmml.converter.FlagManager) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Feature(org.jpmml.converter.Feature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate) BranchNode(org.dmg.pmml.tree.BranchNode) ContinuousFeature(org.jpmml.converter.ContinuousFeature) LeafNode(org.dmg.pmml.tree.LeafNode) CategoryManager(org.jpmml.converter.CategoryManager)

Example 2 with FlagManager

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;
}
Also used : TreeModel(org.dmg.pmml.tree.TreeModel) Node(org.dmg.pmml.tree.Node) BranchNode(org.dmg.pmml.tree.BranchNode) LeafNode(org.dmg.pmml.tree.LeafNode) FlagManager(org.jpmml.converter.FlagManager) CategoryManager(org.jpmml.converter.CategoryManager)

Aggregations

BranchNode (org.dmg.pmml.tree.BranchNode)2 LeafNode (org.dmg.pmml.tree.LeafNode)2 Node (org.dmg.pmml.tree.Node)2 CategoryManager (org.jpmml.converter.CategoryManager)2 FlagManager (org.jpmml.converter.FlagManager)2 Predicate (org.dmg.pmml.Predicate)1 SimplePredicate (org.dmg.pmml.SimplePredicate)1 TreeModel (org.dmg.pmml.tree.TreeModel)1 CategoricalFeature (org.jpmml.converter.CategoricalFeature)1 ContinuousFeature (org.jpmml.converter.ContinuousFeature)1 Feature (org.jpmml.converter.Feature)1