Search in sources :

Example 11 with Apply

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

the class FormulaUtil method encodeIfElseExpression.

private static Expression encodeIfElseExpression(FunctionExpression functionExpression, VariableMap expressionFields, RExpEncoder encoder) {
    FunctionExpression.Argument testArgument = functionExpression.getArgument("test", 0);
    expressionFields.putAll(testArgument);
    FunctionExpression.Argument yesArgument = functionExpression.getArgument("yes", 1);
    FunctionExpression.Argument noArgument = functionExpression.getArgument("no", 2);
    expressionFields.putAll(yesArgument);
    expressionFields.putAll(noArgument);
    // XXX: "Missing values in test give missing values in the result"
    Apply apply = PMMLUtil.createApply("if").addExpressions(prepareExpression(testArgument, expressionFields, encoder)).addExpressions(prepareExpression(yesArgument, expressionFields, encoder), prepareExpression(noArgument, expressionFields, encoder));
    return apply;
}
Also used : Apply(org.dmg.pmml.Apply)

Example 12 with Apply

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

the class EarthConverter method encodeSchema.

@Override
public void encodeSchema(RExpEncoder encoder) {
    RGenericVector earth = getObject();
    RDoubleVector dirs = (RDoubleVector) earth.getValue("dirs");
    RDoubleVector cuts = (RDoubleVector) earth.getValue("cuts");
    RDoubleVector selectedTerms = (RDoubleVector) earth.getValue("selected.terms");
    RDoubleVector coefficients = (RDoubleVector) earth.getValue("coefficients");
    RExp terms = earth.getValue("terms");
    final RGenericVector xlevels;
    try {
        xlevels = (RGenericVector) earth.getValue("xlevels");
    } catch (IllegalArgumentException iae) {
        throw new IllegalArgumentException("No variable levels information. Please initialize the \'xlevels\' element", iae);
    }
    RStringVector dirsRows = dirs.dimnames(0);
    RStringVector dirsColumns = dirs.dimnames(1);
    RStringVector cutsRows = cuts.dimnames(0);
    RStringVector cutsColumns = cuts.dimnames(1);
    if (!(dirsRows.getValues()).equals(cutsRows.getValues()) || !(dirsColumns.getValues()).equals(cutsColumns.getValues())) {
        throw new IllegalArgumentException();
    }
    int rows = dirsRows.size();
    int columns = dirsColumns.size();
    List<String> predictorNames = dirsColumns.getValues();
    FormulaContext context = new FormulaContext() {

        @Override
        public List<String> getCategories(String variable) {
            if (xlevels.hasValue(variable)) {
                RStringVector levels = (RStringVector) xlevels.getValue(variable);
                return levels.getValues();
            }
            return null;
        }

        @Override
        public RGenericVector getData() {
            return null;
        }
    };
    Formula formula = FormulaUtil.createFormula(terms, context, encoder);
    // Dependent variable
    {
        RStringVector yNames = coefficients.dimnames(1);
        FieldName name = FieldName.create(yNames.asScalar());
        DataField dataField = (DataField) encoder.getField(name);
        encoder.setLabel(dataField);
    }
    // Independent variables
    for (int i = 1; i < selectedTerms.size(); i++) {
        int termIndex = ValueUtil.asInt(selectedTerms.getValue(i)) - 1;
        List<Double> dirsRow = FortranMatrixUtil.getRow(dirs.getValues(), rows, columns, termIndex);
        List<Double> cutsRow = FortranMatrixUtil.getRow(cuts.getValues(), rows, columns, termIndex);
        List<Feature> features = new ArrayList<>();
        predictors: for (int j = 0; j < predictorNames.size(); j++) {
            String predictorName = predictorNames.get(j);
            int dir = ValueUtil.asInt(dirsRow.get(j));
            double cut = cutsRow.get(j);
            if (dir == 0) {
                continue predictors;
            }
            Feature feature = formula.resolveFeature(predictorName);
            switch(dir) {
                case -1:
                case 1:
                    {
                        feature = feature.toContinuousFeature();
                        FieldName name = FieldName.create(formatHingeFunction(dir, feature, cut));
                        DerivedField derivedField = encoder.getDerivedField(name);
                        if (derivedField == null) {
                            Apply apply = createHingeFunction(dir, feature, cut);
                            derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, apply);
                        }
                        feature = new ContinuousFeature(encoder, derivedField);
                    }
                    break;
                case 2:
                    break;
                default:
                    throw new IllegalArgumentException();
            }
            features.add(feature);
        }
        Feature feature;
        if (features.size() == 1) {
            feature = features.get(0);
        } else if (features.size() > 1) {
            feature = new InteractionFeature(encoder, FieldName.create(dirsRows.getValue(i)), DataType.DOUBLE, features);
        } else {
            throw new IllegalArgumentException();
        }
        encoder.addFeature(feature);
    }
}
Also used : InteractionFeature(org.jpmml.converter.InteractionFeature) Apply(org.dmg.pmml.Apply) ArrayList(java.util.ArrayList) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) InteractionFeature(org.jpmml.converter.InteractionFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DataField(org.dmg.pmml.DataField) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField)

Example 13 with Apply

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

the class ExpressionCompactor method negateExpression.

private static void negateExpression(Apply apply) {
    List<Expression> expressions = apply.getExpressions();
    if (expressions.size() != 1) {
        throw new IllegalArgumentException();
    }
    ListIterator<Expression> expressionIt = expressions.listIterator();
    Expression expression = expressionIt.next();
    if (expression instanceof Apply) {
        Apply nestedApply = (Apply) expression;
        String negatedFunction = negate(nestedApply.getFunction());
        if (negatedFunction != null) {
            apply.setFunction(negatedFunction);
            expressionIt.remove();
            List<Expression> nestedExpressions = nestedApply.getExpressions();
            for (Expression nestedExpression : nestedExpressions) {
                expressionIt.add(nestedExpression);
            }
        }
    }
}
Also used : Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply)

Example 14 with Apply

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

the class ExpressionCompactor method inlineLogicalExpressions.

private static void inlineLogicalExpressions(Apply apply) {
    String function = apply.getFunction();
    List<Expression> expressions = apply.getExpressions();
    for (ListIterator<Expression> expressionIt = expressions.listIterator(); expressionIt.hasNext(); ) {
        Expression expression = expressionIt.next();
        if (expression instanceof Apply) {
            Apply nestedApply = (Apply) expression;
            if ((function).equals(nestedApply.getFunction())) {
                expressionIt.remove();
                // Deep inline
                inlineLogicalExpressions(nestedApply);
                List<Expression> nestedExpressions = nestedApply.getExpressions();
                for (Expression nestedExpression : nestedExpressions) {
                    expressionIt.add(nestedExpression);
                }
            }
        }
    }
}
Also used : Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply)

Example 15 with Apply

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

the class Formula method addField.

public void addField(Field<?> field) {
    RExpEncoder encoder = getEncoder();
    Feature feature = new ContinuousFeature(encoder, field);
    if (field instanceof DerivedField) {
        DerivedField derivedField = (DerivedField) field;
        Expression expression = derivedField.getExpression();
        if (expression instanceof Apply) {
            Apply apply = (Apply) expression;
            if (checkApply(apply, "pow", FieldRef.class, Constant.class)) {
                List<Expression> expressions = apply.getExpressions();
                FieldRef fieldRef = (FieldRef) expressions.get(0);
                Constant constant = (Constant) expressions.get(1);
                try {
                    int power = Integer.parseInt(constant.getValue());
                    feature = new PowerFeature(encoder, fieldRef.getField(), DataType.DOUBLE, power);
                } catch (NumberFormatException nfe) {
                // Ignored
                }
            }
        }
    }
    putFeature(field.getName(), feature);
    this.fields.add(field);
}
Also used : PowerFeature(org.jpmml.converter.PowerFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) Constant(org.dmg.pmml.Constant) Feature(org.jpmml.converter.Feature) PowerFeature(org.jpmml.converter.PowerFeature) BinaryFeature(org.jpmml.converter.BinaryFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) BooleanFeature(org.jpmml.converter.BooleanFeature) InteractionFeature(org.jpmml.converter.InteractionFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) DerivedField(org.dmg.pmml.DerivedField)

Aggregations

Apply (org.dmg.pmml.Apply)25 Expression (org.dmg.pmml.Expression)11 Test (org.junit.Test)10 DerivedField (org.dmg.pmml.DerivedField)9 Feature (org.jpmml.converter.Feature)9 ContinuousFeature (org.jpmml.converter.ContinuousFeature)7 FieldRef (org.dmg.pmml.FieldRef)6 ArrayList (java.util.ArrayList)5 Constant (org.dmg.pmml.Constant)5 DataField (org.dmg.pmml.DataField)3 FieldName (org.dmg.pmml.FieldName)3 CategoricalFeature (org.jpmml.converter.CategoricalFeature)3 InteractionFeature (org.jpmml.converter.InteractionFeature)2 PMMLEncoder (org.jpmml.converter.PMMLEncoder)2 DocumentFeature (org.jpmml.sparkml.DocumentFeature)2 Binarizer (org.apache.spark.ml.feature.Binarizer)1 PCAModel (org.apache.spark.ml.feature.PCAModel)1 RegexTokenizer (org.apache.spark.ml.feature.RegexTokenizer)1 StringIndexerModel (org.apache.spark.ml.feature.StringIndexerModel)1 Tokenizer (org.apache.spark.ml.feature.Tokenizer)1