Search in sources :

Example 16 with Expression

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

the class ExpressionTranslatorTest method translateRelationalExpression.

@Test
public void translateRelationalExpression() {
    Apply apply = (Apply) ExpressionTranslator.translateExpression("if(x < 0) \"negative\" else if(x > 0) \"positive\" else \"zero\"");
    List<Expression> expressions = checkApply(apply, "if", Apply.class, Constant.class, Apply.class);
    Expression condition = expressions.get(0);
    checkApply((Apply) condition, "lessThan", FieldRef.class, Constant.class);
    Expression first = expressions.get(1);
    Expression second = expressions.get(2);
    checkConstant((Constant) first, "negative", null);
    expressions = checkApply((Apply) second, "if", Apply.class, Constant.class, Constant.class);
    condition = expressions.get(0);
    checkApply((Apply) condition, "greaterThan", FieldRef.class, Constant.class);
    first = expressions.get(1);
    second = expressions.get(2);
    checkConstant((Constant) first, "positive", null);
    checkConstant((Constant) second, "zero", null);
}
Also used : Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) Constant(org.dmg.pmml.Constant) Test(org.junit.Test)

Example 17 with Expression

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

the class ExpressionTranslatorTest method translateIfExpression.

@Test
public void translateIfExpression() {
    Apply apply = (Apply) ExpressionTranslator.translateExpression("if(is.na(x)) TRUE else FALSE");
    List<Expression> expressions = checkApply(apply, "if", Apply.class, Constant.class, Constant.class);
    Expression condition = expressions.get(0);
    checkApply((Apply) condition, "isMissing", FieldRef.class);
    Expression first = expressions.get(1);
    Expression second = expressions.get(2);
    checkConstant((Constant) first, "true", DataType.BOOLEAN);
    checkConstant((Constant) second, "false", DataType.BOOLEAN);
}
Also used : Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 18 with Expression

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

the class ExpressionTranslatorTest method translateArithmeticExpressionChain.

@Test
public void translateArithmeticExpressionChain() {
    Apply apply = (Apply) ExpressionTranslator.translateExpression("A + B - X + C");
    List<Expression> expressions = checkApply(apply, "+", Apply.class, FieldRef.class);
    Expression left = expressions.get(0);
    Expression right = expressions.get(1);
    expressions = checkApply((Apply) left, "-", Apply.class, FieldRef.class);
    checkFieldRef((FieldRef) right, FieldName.create("C"));
    left = expressions.get(0);
    right = expressions.get(1);
    expressions = checkApply((Apply) left, "+", FieldRef.class, FieldRef.class);
    checkFieldRef((FieldRef) right, FieldName.create("X"));
    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) Test(org.junit.Test)

Example 19 with Expression

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

the class PreProcessEncoder method encodeExpression.

private Expression encodeExpression(Feature feature) {
    FieldName name = feature.getName();
    Expression expression = feature.ref();
    List<Double> ranges = this.ranges.get(name);
    if (ranges != null) {
        Double min = ranges.get(0);
        Double max = ranges.get(1);
        expression = PMMLUtil.createApply("/", PMMLUtil.createApply("-", expression, PMMLUtil.createConstant(min)), PMMLUtil.createConstant(max - min));
    }
    Double mean = this.mean.get(name);
    if (mean != null) {
        expression = PMMLUtil.createApply("-", expression, PMMLUtil.createConstant(mean));
    }
    Double std = this.std.get(name);
    if (std != null) {
        expression = PMMLUtil.createApply("/", expression, PMMLUtil.createConstant(std));
    }
    Double median = this.median.get(name);
    if (median != null) {
        expression = PMMLUtil.createApply("if", PMMLUtil.createApply("isNotMissing", new FieldRef(name)), expression, PMMLUtil.createConstant(median));
    }
    if (expression instanceof FieldRef) {
        return null;
    }
    return expression;
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) FieldName(org.dmg.pmml.FieldName)

Example 20 with Expression

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

the class MaxAbsScalerModelConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    MaxAbsScalerModel transformer = getTransformer();
    List<Feature> features = encoder.getFeatures(transformer.getInputCol());
    Vector maxAbs = transformer.maxAbs();
    if (maxAbs.size() != features.size()) {
        throw new IllegalArgumentException();
    }
    List<Feature> result = new ArrayList<>();
    for (int i = 0; i < features.size(); i++) {
        Feature feature = features.get(i);
        double maxAbsUnzero = maxAbs.apply(i);
        if (maxAbsUnzero == 0d) {
            maxAbsUnzero = 1d;
        }
        if (!ValueUtil.isOne(maxAbsUnzero)) {
            ContinuousFeature continuousFeature = feature.toContinuousFeature();
            Expression expression = PMMLUtil.createApply("/", continuousFeature.ref(), PMMLUtil.createConstant(maxAbsUnzero));
            DerivedField derivedField = encoder.createDerivedField(formatName(transformer, i), OpType.CONTINUOUS, DataType.DOUBLE, expression);
            feature = new ContinuousFeature(encoder, derivedField);
        }
        result.add(feature);
    }
    return result;
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) Expression(org.dmg.pmml.Expression) MaxAbsScalerModel(org.apache.spark.ml.feature.MaxAbsScalerModel) 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