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