Search in sources :

Example 1 with Feature

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

the class LMConverter method encodeSchema.

public void encodeSchema(FormulaContext context, RExp terms, RExpEncoder encoder) {
    RIntegerVector response = (RIntegerVector) terms.getAttributeValue("response");
    Formula formula = FormulaUtil.createFormula(terms, context, encoder);
    // Dependent variable
    int responseIndex = response.asScalar();
    if (responseIndex != 0) {
        DataField dataField = (DataField) formula.getField(responseIndex - 1);
        encoder.setLabel(dataField);
    } else {
        throw new IllegalArgumentException();
    }
    String interceptName = getInterceptName();
    // Independent variables
    List<String> coefficientNames = getCoefficientNames();
    for (String coefficientName : coefficientNames) {
        if ((interceptName).equals(coefficientName)) {
            continue;
        }
        Feature feature = formula.resolveFeature(coefficientName);
        encoder.addFeature(feature);
    }
    this.formula = formula;
}
Also used : DataField(org.dmg.pmml.DataField) Feature(org.jpmml.converter.Feature)

Example 2 with Feature

use of org.jpmml.converter.Feature 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 Feature

use of org.jpmml.converter.Feature 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 Feature

use of org.jpmml.converter.Feature 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 Feature

use of org.jpmml.converter.Feature 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

Feature (org.jpmml.converter.Feature)69 ContinuousFeature (org.jpmml.converter.ContinuousFeature)39 ArrayList (java.util.ArrayList)30 CategoricalFeature (org.jpmml.converter.CategoricalFeature)29 DerivedField (org.dmg.pmml.DerivedField)16 DataField (org.dmg.pmml.DataField)12 SimplePredicate (org.dmg.pmml.SimplePredicate)12 List (java.util.List)11 Predicate (org.dmg.pmml.Predicate)11 Node (org.dmg.pmml.tree.Node)11 BooleanFeature (org.jpmml.converter.BooleanFeature)11 Apply (org.dmg.pmml.Apply)9 BinaryFeature (org.jpmml.converter.BinaryFeature)9 Expression (org.dmg.pmml.Expression)7 FieldName (org.dmg.pmml.FieldName)7 CategoricalLabel (org.jpmml.converter.CategoricalLabel)7 InteractionFeature (org.jpmml.converter.InteractionFeature)6 Vector (org.apache.spark.ml.linalg.Vector)5 BranchNode (org.dmg.pmml.tree.BranchNode)5 LeafNode (org.dmg.pmml.tree.LeafNode)5