Search in sources :

Example 16 with TreeModel

use of org.dmg.pmml.tree.TreeModel in project jpmml-r by jpmml.

the class IForestConverter method encodeTreeModel.

private TreeModel encodeTreeModel(int index, RGenericVector trees, Schema schema) {
    RIntegerVector nrnodes = trees.getIntegerElement("nrnodes");
    RIntegerVector ntree = trees.getIntegerElement("ntree");
    RIntegerVector nodeStatus = trees.getIntegerElement("nodeStatus");
    RIntegerVector leftDaughter = trees.getIntegerElement("lDaughter");
    RIntegerVector rightDaughter = trees.getIntegerElement("rDaughter");
    RIntegerVector splitAtt = trees.getIntegerElement("splitAtt");
    RDoubleVector splitPoint = trees.getDoubleElement("splitPoint");
    RIntegerVector nSam = trees.getIntegerElement("nSam");
    int rows = nrnodes.asScalar();
    int columns = ntree.asScalar();
    Node root = encodeNode(0, True.INSTANCE, 0, FortranMatrixUtil.getColumn(nodeStatus.getValues(), rows, columns, index), FortranMatrixUtil.getColumn(nSam.getValues(), rows, columns, index), FortranMatrixUtil.getColumn(leftDaughter.getValues(), rows, columns, index), FortranMatrixUtil.getColumn(rightDaughter.getValues(), rows, columns, index), FortranMatrixUtil.getColumn(splitAtt.getValues(), rows, columns, index), FortranMatrixUtil.getColumn(splitPoint.getValues(), rows, columns, index), schema);
    TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), root).setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_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)

Example 17 with TreeModel

use of org.dmg.pmml.tree.TreeModel in project jpmml-r by jpmml.

the class PartyConverter method encodeModel.

@Override
public Model encodeModel(Schema schema) {
    RGenericVector party = getObject();
    RGenericVector partyNode = party.getGenericElement("node");
    RGenericVector predicted = DecorationUtil.getGenericElement(party, "predicted");
    RVector<?> response = predicted.getVectorElement("(response)");
    RDoubleVector prob = predicted.getDoubleElement("(prob)", false);
    Node root = encodeNode(partyNode, True.INSTANCE, response, prob, schema);
    TreeModel treeModel;
    if (response instanceof RFactorVector) {
        CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
        treeModel = new TreeModel(MiningFunction.CLASSIFICATION, ModelUtil.createMiningSchema(categoricalLabel), root).setOutput(ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel));
    } else {
        treeModel = new TreeModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), root);
    }
    return treeModel;
}
Also used : TreeModel(org.dmg.pmml.tree.TreeModel) CategoricalLabel(org.jpmml.converter.CategoricalLabel) BranchNode(org.dmg.pmml.tree.BranchNode) LeafNode(org.dmg.pmml.tree.LeafNode) Node(org.dmg.pmml.tree.Node) ClassifierNode(org.dmg.pmml.tree.ClassifierNode)

Example 18 with TreeModel

use of org.dmg.pmml.tree.TreeModel in project jpmml-r by jpmml.

the class BinaryTreeConverter method encodeModel.

@Override
public TreeModel encodeModel(Schema schema) {
    S4Object binaryTree = getObject();
    RGenericVector tree = binaryTree.getGenericAttribute("tree");
    Output output;
    switch(this.miningFunction) {
        case REGRESSION:
            output = new Output();
            break;
        case CLASSIFICATION:
            CategoricalLabel categoricalLabel = (CategoricalLabel) schema.getLabel();
            output = ModelUtil.createProbabilityOutput(DataType.DOUBLE, categoricalLabel);
            break;
        default:
            throw new IllegalArgumentException();
    }
    output.addOutputFields(ModelUtil.createEntityIdField("nodeId", DataType.STRING));
    TreeModel treeModel = encodeTreeModel(tree, schema).setOutput(output);
    return treeModel;
}
Also used : TreeModel(org.dmg.pmml.tree.TreeModel) CategoricalLabel(org.jpmml.converter.CategoricalLabel) Output(org.dmg.pmml.Output)

Example 19 with TreeModel

use of org.dmg.pmml.tree.TreeModel in project jpmml-r by jpmml.

the class RPartEnsembleConverter method encodeTreeModels.

public List<TreeModel> encodeTreeModels(RGenericVector trees) {
    List<TreeModel> result = new ArrayList<>();
    if (trees.size() != this.schemas.size()) {
        throw new IllegalArgumentException();
    }
    for (int i = 0; i < trees.size(); i++) {
        RGenericVector tree = trees.getGenericValue(i);
        Schema schema = this.schemas.get(i);
        RPartConverter converter = this.converters.get(tree);
        if (converter == null) {
            throw new IllegalArgumentException();
        }
        Schema segmentSchema = schema.toAnonymousSchema();
        TreeModel treeModel = (TreeModel) converter.encode(segmentSchema);
        result.add(treeModel);
    }
    return result;
}
Also used : TreeModel(org.dmg.pmml.tree.TreeModel) Schema(org.jpmml.converter.Schema) ArrayList(java.util.ArrayList)

Example 20 with TreeModel

use of org.dmg.pmml.tree.TreeModel in project jpmml-r by jpmml.

the class RangerConverter method encodeTreeModel.

private TreeModel encodeTreeModel(MiningFunction miningFunction, ScoreEncoder scoreEncoder, RGenericVector childNodeIDs, RNumberVector<?> splitVarIDs, RNumberVector<?> splitValues, RGenericVector terminalClassCounts, Schema schema) {
    RNumberVector<?> leftChildIDs = childNodeIDs.getNumericValue(0);
    RNumberVector<?> rightChildIDs = childNodeIDs.getNumericValue(1);
    Node root = encodeNode(True.INSTANCE, 0, scoreEncoder, leftChildIDs, rightChildIDs, splitVarIDs, splitValues, terminalClassCounts, new CategoryManager(), schema);
    TreeModel treeModel = new TreeModel(miningFunction, ModelUtil.createMiningSchema(schema.getLabel()), root).setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT);
    return treeModel;
}
Also used : TreeModel(org.dmg.pmml.tree.TreeModel) Node(org.dmg.pmml.tree.Node) ClassifierNode(org.dmg.pmml.tree.ClassifierNode) BranchNode(org.dmg.pmml.tree.BranchNode) LeafNode(org.dmg.pmml.tree.LeafNode) CategoryManager(org.jpmml.converter.CategoryManager)

Aggregations

TreeModel (org.dmg.pmml.tree.TreeModel)48 MiningModel (org.dmg.pmml.mining.MiningModel)17 Node (org.dmg.pmml.tree.Node)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)11 BranchNode (org.dmg.pmml.tree.BranchNode)9 LeafNode (org.dmg.pmml.tree.LeafNode)9 Schema (org.jpmml.converter.Schema)9 ClassifierNode (org.dmg.pmml.tree.ClassifierNode)8 CategoricalLabel (org.jpmml.converter.CategoricalLabel)8 KiePMMLTreeModel (org.kie.pmml.models.drools.tree.model.KiePMMLTreeModel)8 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 HasClassLoaderMock (org.kie.pmml.compiler.commons.mocks.HasClassLoaderMock)6 PMML (org.dmg.pmml.PMML)5 HasKnowledgeBuilderMock (org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock)5 KiePMMLTreeModel (org.kie.pmml.models.tree.model.KiePMMLTreeModel)5 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)4 Expression (com.github.javaparser.ast.expr.Expression)4 NameExpr (com.github.javaparser.ast.expr.NameExpr)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)4