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"));
}
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);
}
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);
}
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);
}
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;
}
Aggregations