Search in sources :

Example 1 with ContinuousFeature

use of org.jpmml.converter.ContinuousFeature in project jpmml-r by jpmml.

the class IForestConverter method encodeNode.

private void encodeNode(Node node, int index, int depth, List<Integer> nodeStatus, List<Integer> nodeSize, List<Integer> leftDaughter, List<Integer> rightDaughter, List<Integer> splitAtt, List<Double> splitValue, Schema schema) {
    int status = nodeStatus.get(index);
    int size = nodeSize.get(index);
    node.setId(String.valueOf(index + 1));
    // Interior node
    if (status == -3) {
        int att = splitAtt.get(index);
        ContinuousFeature feature = (ContinuousFeature) schema.getFeature(att - 1);
        String value = ValueUtil.formatValue(splitValue.get(index));
        Predicate leftPredicate = createSimplePredicate(feature, SimplePredicate.Operator.LESS_THAN, value);
        Node leftChild = new Node().setPredicate(leftPredicate);
        int leftIndex = (leftDaughter.get(index) - 1);
        encodeNode(leftChild, leftIndex, depth + 1, nodeStatus, nodeSize, leftDaughter, rightDaughter, splitAtt, splitValue, schema);
        Predicate rightPredicate = createSimplePredicate(feature, SimplePredicate.Operator.GREATER_OR_EQUAL, value);
        Node rightChild = new Node().setPredicate(rightPredicate);
        int rightIndex = (rightDaughter.get(index) - 1);
        encodeNode(rightChild, rightIndex, depth + 1, nodeStatus, nodeSize, leftDaughter, rightDaughter, splitAtt, splitValue, schema);
        node.addNodes(leftChild, rightChild);
    } else // Terminal node
    if (status == -1) {
        node.setScore(ValueUtil.formatValue(depth + avgPathLength(size)));
    } else {
        throw new IllegalArgumentException();
    }
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) Node(org.dmg.pmml.tree.Node) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate)

Example 2 with ContinuousFeature

use of org.jpmml.converter.ContinuousFeature in project jpmml-r by jpmml.

the class MVRConverter method encodeSchema.

@Override
public void encodeSchema(RExpEncoder encoder) {
    RGenericVector mvr = getObject();
    RDoubleVector coefficients = (RDoubleVector) mvr.getValue("coefficients");
    RDoubleVector scale = (RDoubleVector) mvr.getValue("scale", true);
    RExp terms = mvr.getValue("terms");
    final RGenericVector model = (RGenericVector) mvr.getValue("model");
    RStringVector rowNames = coefficients.dimnames(0);
    RStringVector columnNames = coefficients.dimnames(1);
    FormulaContext context = new ModelFrameFormulaContext(model);
    Formula formula = FormulaUtil.createFormula(terms, context, encoder);
    // Dependent variable
    {
        FieldName name = FieldName.create(columnNames.asScalar());
        DataField dataField = (DataField) encoder.getField(name);
        encoder.setLabel(dataField);
    }
    // Independent variables
    for (int i = 0; i < rowNames.size(); i++) {
        String rowName = rowNames.getValue(i);
        Feature feature = formula.resolveFeature(rowName);
        if (scale != null) {
            feature = feature.toContinuousFeature();
            Apply apply = PMMLUtil.createApply("/", feature.ref(), PMMLUtil.createConstant(scale.getValue(i)));
            DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("scale", feature), OpType.CONTINUOUS, DataType.DOUBLE, apply);
            feature = new ContinuousFeature(encoder, derivedField);
        }
        encoder.addFeature(feature);
    }
}
Also used : Apply(org.dmg.pmml.Apply) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DataField(org.dmg.pmml.DataField) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField)

Example 3 with ContinuousFeature

use of org.jpmml.converter.ContinuousFeature in project jpmml-r by jpmml.

the class RandomForestConverter method encodeNode.

private <P extends Number> void encodeNode(Node node, int i, ScoreEncoder<P> scoreEncoder, List<? extends Number> leftDaughter, List<? extends Number> rightDaughter, List<? extends Number> bestvar, List<Double> xbestsplit, List<P> nodepred, Schema schema) {
    Predicate leftPredicate;
    Predicate rightPredicate;
    int var = ValueUtil.asInt(bestvar.get(i));
    if (var != 0) {
        Feature feature = schema.getFeature(var - 1);
        Double split = xbestsplit.get(i);
        if (feature instanceof BooleanFeature) {
            BooleanFeature booleanFeature = (BooleanFeature) feature;
            if (split != 0.5d) {
                throw new IllegalArgumentException();
            }
            leftPredicate = createSimplePredicate(booleanFeature, SimplePredicate.Operator.EQUAL, booleanFeature.getValue(0));
            rightPredicate = createSimplePredicate(booleanFeature, SimplePredicate.Operator.EQUAL, booleanFeature.getValue(1));
        } else if (feature instanceof CategoricalFeature) {
            CategoricalFeature categoricalFeature = (CategoricalFeature) feature;
            List<String> values = categoricalFeature.getValues();
            leftPredicate = createSimpleSetPredicate(categoricalFeature, selectValues(values, split, true));
            rightPredicate = createSimpleSetPredicate(categoricalFeature, selectValues(values, split, false));
        } else {
            ContinuousFeature continuousFeature = feature.toContinuousFeature();
            String value = ValueUtil.formatValue(split);
            leftPredicate = createSimplePredicate(continuousFeature, SimplePredicate.Operator.LESS_OR_EQUAL, value);
            rightPredicate = createSimplePredicate(continuousFeature, SimplePredicate.Operator.GREATER_THAN, value);
        }
    } else {
        P prediction = nodepred.get(i);
        node.setScore(scoreEncoder.encode(prediction));
        return;
    }
    int left = ValueUtil.asInt(leftDaughter.get(i));
    if (left != 0) {
        Node leftChild = new Node().setId(String.valueOf(left)).setPredicate(leftPredicate);
        encodeNode(leftChild, left - 1, scoreEncoder, leftDaughter, rightDaughter, bestvar, xbestsplit, nodepred, schema);
        node.addNodes(leftChild);
    }
    int right = ValueUtil.asInt(rightDaughter.get(i));
    if (right != 0) {
        Node rightChild = new Node().setId(String.valueOf(right)).setPredicate(rightPredicate);
        encodeNode(rightChild, right - 1, scoreEncoder, leftDaughter, rightDaughter, bestvar, xbestsplit, nodepred, schema);
        node.addNodes(rightChild);
    }
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) Node(org.dmg.pmml.tree.Node) ArrayList(java.util.ArrayList) List(java.util.List) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Feature(org.jpmml.converter.Feature) BooleanFeature(org.jpmml.converter.BooleanFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) BooleanFeature(org.jpmml.converter.BooleanFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate)

Example 4 with ContinuousFeature

use of org.jpmml.converter.ContinuousFeature in project jpmml-r by jpmml.

the class RangerConverter method encodeNode.

private void encodeNode(Node node, int index, ScoreEncoder scoreEncoder, RNumberVector<?> leftChildIDs, RNumberVector<?> rightChildIDs, RNumberVector<?> splitVarIDs, RNumberVector<?> splitValues, RGenericVector terminalClassCounts, Schema schema) {
    int leftIndex = ValueUtil.asInt(leftChildIDs.getValue(index));
    int rightIndex = ValueUtil.asInt(rightChildIDs.getValue(index));
    Number splitValue = splitValues.getValue(index);
    RNumberVector<?> terminalClassCount = (terminalClassCounts != null ? (RNumberVector<?>) terminalClassCounts.getValue(index) : null);
    if (leftIndex == 0 && rightIndex == 0) {
        scoreEncoder.encode(node, splitValue, terminalClassCount);
        return;
    }
    Predicate leftPredicate;
    Predicate rightPredicate;
    int splitVarIndex = ValueUtil.asInt(splitVarIDs.getValue(index));
    Feature feature = schema.getFeature(splitVarIndex - 1);
    if (feature instanceof CategoricalFeature) {
        CategoricalFeature categoricalFeature = (CategoricalFeature) feature;
        int splitLevelIndex = ValueUtil.asInt(Math.floor(splitValue.doubleValue()));
        List<String> values = categoricalFeature.getValues();
        leftPredicate = createSimpleSetPredicate(categoricalFeature, values.subList(0, splitLevelIndex));
        rightPredicate = createSimpleSetPredicate(categoricalFeature, values.subList(splitLevelIndex, values.size()));
    } else {
        ContinuousFeature continuousFeature = feature.toContinuousFeature();
        String value = ValueUtil.formatValue(splitValue);
        leftPredicate = createSimplePredicate(continuousFeature, SimplePredicate.Operator.LESS_OR_EQUAL, value);
        rightPredicate = createSimplePredicate(continuousFeature, SimplePredicate.Operator.GREATER_THAN, value);
    }
    Node leftChild = new Node().setPredicate(leftPredicate);
    encodeNode(leftChild, leftIndex, scoreEncoder, leftChildIDs, rightChildIDs, splitVarIDs, splitValues, terminalClassCounts, schema);
    Node rightChild = new Node().setPredicate(rightPredicate);
    encodeNode(rightChild, rightIndex, scoreEncoder, leftChildIDs, rightChildIDs, splitVarIDs, splitValues, terminalClassCounts, schema);
    node.addNodes(leftChild, rightChild);
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) Node(org.dmg.pmml.tree.Node) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) Predicate(org.dmg.pmml.Predicate) SimplePredicate(org.dmg.pmml.SimplePredicate)

Example 5 with ContinuousFeature

use of org.jpmml.converter.ContinuousFeature in project jpmml-r by jpmml.

the class SVMConverter method scale.

private List<Feature> scale(List<Feature> features, RExpEncoder encoder) {
    RGenericVector svm = getObject();
    RDoubleVector sv = (RDoubleVector) svm.getValue("SV");
    RBooleanVector scaled = (RBooleanVector) svm.getValue("scaled");
    RGenericVector xScale = (RGenericVector) svm.getValue("x.scale");
    RStringVector rowNames = sv.dimnames(0);
    RStringVector columnNames = sv.dimnames(1);
    if ((scaled.size() != columnNames.size()) || (scaled.size() != features.size())) {
        throw new IllegalArgumentException();
    }
    RDoubleVector xScaledCenter = null;
    RDoubleVector xScaledScale = null;
    if (xScale != null) {
        xScaledCenter = (RDoubleVector) xScale.getValue("scaled:center");
        xScaledScale = (RDoubleVector) xScale.getValue("scaled:scale");
    }
    List<Feature> result = new ArrayList<>();
    for (int i = 0; i < columnNames.size(); i++) {
        String columnName = columnNames.getValue(i);
        Feature feature = features.get(i);
        if (scaled.getValue(i)) {
            feature = feature.toContinuousFeature();
            FieldName name = FeatureUtil.createName("scale", feature);
            DerivedField derivedField = encoder.getDerivedField(name);
            if (derivedField == null) {
                Double center = xScaledCenter.getValue(columnName);
                Double scale = xScaledScale.getValue(columnName);
                Apply apply = PMMLUtil.createApply("/", PMMLUtil.createApply("-", feature.ref(), PMMLUtil.createConstant(center)), PMMLUtil.createConstant(scale));
                derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, apply);
            }
            feature = new ContinuousFeature(encoder, derivedField);
        }
        result.add(feature);
    }
    return result;
}
Also used : Apply(org.dmg.pmml.Apply) ArrayList(java.util.ArrayList) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField)

Aggregations

ContinuousFeature (org.jpmml.converter.ContinuousFeature)27 Feature (org.jpmml.converter.Feature)23 ArrayList (java.util.ArrayList)13 DerivedField (org.dmg.pmml.DerivedField)13 CategoricalFeature (org.jpmml.converter.CategoricalFeature)12 FieldName (org.dmg.pmml.FieldName)8 Apply (org.dmg.pmml.Apply)7 SimplePredicate (org.dmg.pmml.SimplePredicate)7 DataField (org.dmg.pmml.DataField)6 Expression (org.dmg.pmml.Expression)6 Predicate (org.dmg.pmml.Predicate)6 Node (org.dmg.pmml.tree.Node)6 OutputField (org.dmg.pmml.OutputField)4 BooleanFeature (org.jpmml.converter.BooleanFeature)4 Vector (org.apache.spark.ml.linalg.Vector)3 DataType (org.dmg.pmml.DataType)3 BinaryFeature (org.jpmml.converter.BinaryFeature)3 CategoricalLabel (org.jpmml.converter.CategoricalLabel)3 List (java.util.List)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2