Search in sources :

Example 1 with Expression

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

the class ExpressionTranslatorTest method translate.

@Test
public void translate() {
    Apply apply = (Apply) ExpressionTranslator.translateExpression("(1.0 + log(A / B)) ^ 2");
    List<Expression> expressions = checkApply(apply, "pow", Apply.class, Constant.class);
    Expression left = expressions.get(0);
    Expression right = expressions.get(1);
    checkConstant((Constant) right, "2", null);
    expressions = checkApply((Apply) left, "+", Constant.class, Apply.class);
    left = expressions.get(0);
    right = expressions.get(1);
    checkConstant((Constant) left, "1.0", DataType.DOUBLE);
    expressions = checkApply((Apply) right, "ln", Apply.class);
    left = expressions.get(0);
    expressions = checkApply((Apply) left, "/", FieldRef.class, FieldRef.class);
    left = expressions.get(0);
    right = expressions.get(1);
    checkFieldRef((FieldRef) left, FieldName.create("A"));
    checkFieldRef((FieldRef) right, FieldName.create("B"));
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) Constant(org.dmg.pmml.Constant) Test(org.junit.Test)

Example 2 with Expression

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

the class ExpressionTranslatorTest method translateLogicalExpressionChain.

@Test
public void translateLogicalExpressionChain() {
    String string = "(x == 0) | ((x == 1) | (x == 2)) | x == 3";
    Apply apply = (Apply) ExpressionTranslator.translateExpression(string, false);
    List<Expression> expressions = checkApply(apply, "or", Apply.class, Apply.class);
    Expression left = expressions.get(0);
    Expression right = expressions.get(1);
    checkApply((Apply) left, "or", Apply.class, Apply.class);
    checkApply((Apply) right, "equal", FieldRef.class, Constant.class);
    apply = (Apply) ExpressionTranslator.translateExpression(string, true);
    checkApply(apply, "or", Apply.class, Apply.class, Apply.class, Apply.class);
}
Also used : Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 3 with Expression

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

the class ExpressionTranslatorTest method translateFunctionExpression.

@Test
public void translateFunctionExpression() {
    FunctionExpression functionExpression = (FunctionExpression) ExpressionTranslator.translateExpression("parent(first = child(A, log(A)), child(1 + B, right = 0), \"third\" = child(left = 0, c(A, B, C)))");
    checkFunctionExpression(functionExpression, "parent", "first", null, "third");
    FunctionExpression.Argument first = functionExpression.getArgument("first");
    FunctionExpression.Argument second;
    try {
        second = functionExpression.getArgument("second");
        fail();
    } catch (IllegalArgumentException iae) {
        second = functionExpression.getArgument(1);
    }
    FunctionExpression.Argument third = functionExpression.getArgument("third");
    assertEquals("first = child(A, log(A))", first.format());
    assertEquals("child(A, log(A))", first.formatExpression());
    assertEquals("child(1 + B, right = 0)", second.format());
    assertEquals("child(1 + B, right = 0)", second.formatExpression());
    assertEquals("\"third\" = child(left = 0, c(A, B, C))", third.format());
    assertEquals("child(left = 0, c(A, B, C))", third.formatExpression());
    List<Expression> expressions = checkFunctionExpression((FunctionExpression) first.getExpression(), "child", null, null);
    Expression left = expressions.get(0);
    Expression right = expressions.get(1);
    checkFieldRef((FieldRef) left, FieldName.create("A"));
    checkApply((Apply) right, "ln", FieldRef.class);
    expressions = checkFunctionExpression((FunctionExpression) second.getExpression(), "child", null, "right");
    left = expressions.get(0);
    right = expressions.get(1);
    checkApply((Apply) left, "+", Constant.class, FieldRef.class);
    checkConstant((Constant) right, "0", null);
    expressions = checkFunctionExpression((FunctionExpression) third.getExpression(), "child", "left", null);
    left = expressions.get(0);
    right = expressions.get(1);
    checkConstant((Constant) left, "0", null);
    checkFunctionExpression((FunctionExpression) right, "c", null, null, null);
}
Also used : Expression(org.dmg.pmml.Expression) Test(org.junit.Test)

Example 4 with Expression

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

the class PreProcessEncoder method filter.

private Schema filter(Schema schema) {
    Function<Feature, Feature> function = new Function<Feature, Feature>() {

        @Override
        public Feature apply(Feature feature) {
            Expression expression = encodeExpression(feature);
            if (expression == null) {
                return feature;
            }
            DerivedField derivedField = createDerivedField(FeatureUtil.createName("preProcess", feature), OpType.CONTINUOUS, DataType.DOUBLE, expression);
            return new ContinuousFeature(PreProcessEncoder.this, derivedField);
        }
    };
    return schema.toTransformedSchema(function);
}
Also used : Function(java.util.function.Function) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Expression(org.dmg.pmml.Expression) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DerivedField(org.dmg.pmml.DerivedField)

Example 5 with Expression

use of org.dmg.pmml.Expression in project jpmml-sparkml by jpmml.

the class StandardScalerModelConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    StandardScalerModel transformer = getTransformer();
    List<Feature> features = encoder.getFeatures(transformer.getInputCol());
    Vector mean = transformer.mean();
    if (transformer.getWithMean() && mean.size() != features.size()) {
        throw new IllegalArgumentException();
    }
    Vector std = transformer.std();
    if (transformer.getWithStd() && std.size() != features.size()) {
        throw new IllegalArgumentException();
    }
    List<Feature> result = new ArrayList<>();
    for (int i = 0; i < features.size(); i++) {
        Feature feature = features.get(i);
        ContinuousFeature continuousFeature = feature.toContinuousFeature();
        Expression expression = continuousFeature.ref();
        if (transformer.getWithMean()) {
            double meanValue = mean.apply(i);
            if (!ValueUtil.isZero(meanValue)) {
                expression = PMMLUtil.createApply("-", expression, PMMLUtil.createConstant(meanValue));
            }
        }
        if (transformer.getWithStd()) {
            double stdValue = std.apply(i);
            if (!ValueUtil.isOne(stdValue)) {
                expression = PMMLUtil.createApply("*", expression, PMMLUtil.createConstant(1d / stdValue));
            }
        }
        DerivedField derivedField = encoder.createDerivedField(formatName(transformer, i), OpType.CONTINUOUS, DataType.DOUBLE, expression);
        result.add(new ContinuousFeature(encoder, derivedField));
    }
    return result;
}
Also used : StandardScalerModel(org.apache.spark.ml.feature.StandardScalerModel) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Expression(org.dmg.pmml.Expression) ArrayList(java.util.ArrayList) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Vector(org.apache.spark.ml.linalg.Vector) DerivedField(org.dmg.pmml.DerivedField)

Aggregations

Expression (org.dmg.pmml.Expression)22 Apply (org.dmg.pmml.Apply)11 Test (org.junit.Test)8 DerivedField (org.dmg.pmml.DerivedField)7 ArrayList (java.util.ArrayList)6 ContinuousFeature (org.jpmml.converter.ContinuousFeature)6 Feature (org.jpmml.converter.Feature)6 FieldRef (org.dmg.pmml.FieldRef)5 Constant (org.dmg.pmml.Constant)4 Vector (org.apache.spark.ml.linalg.Vector)3 DataType (org.dmg.pmml.DataType)2 FieldName (org.dmg.pmml.FieldName)2 OpType (org.dmg.pmml.OpType)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 MaxAbsScalerModel (org.apache.spark.ml.feature.MaxAbsScalerModel)1 MinMaxScalerModel (org.apache.spark.ml.feature.MinMaxScalerModel)1 PCAModel (org.apache.spark.ml.feature.PCAModel)1