use of org.dmg.pmml.Apply in project jpmml-r by jpmml.
the class MVRConverter method encodeSchema.
@Override
public void encodeSchema(RExpEncoder encoder) {
RGenericVector mvr = getObject();
RDoubleVector coefficients = (RDoubleVector) mvr.getValue("coefficients");
RDoubleVector scale = (RDoubleVector) mvr.getValue("scale", true);
RExp terms = mvr.getValue("terms");
final RGenericVector model = (RGenericVector) mvr.getValue("model");
RStringVector rowNames = coefficients.dimnames(0);
RStringVector columnNames = coefficients.dimnames(1);
FormulaContext context = new ModelFrameFormulaContext(model);
Formula formula = FormulaUtil.createFormula(terms, context, encoder);
// Dependent variable
{
FieldName name = FieldName.create(columnNames.asScalar());
DataField dataField = (DataField) encoder.getField(name);
encoder.setLabel(dataField);
}
// Independent variables
for (int i = 0; i < rowNames.size(); i++) {
String rowName = rowNames.getValue(i);
Feature feature = formula.resolveFeature(rowName);
if (scale != null) {
feature = feature.toContinuousFeature();
Apply apply = PMMLUtil.createApply("/", feature.ref(), PMMLUtil.createConstant(scale.getValue(i)));
DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("scale", feature), OpType.CONTINUOUS, DataType.DOUBLE, apply);
feature = new ContinuousFeature(encoder, derivedField);
}
encoder.addFeature(feature);
}
}
use of org.dmg.pmml.Apply in project jpmml-r by jpmml.
the class SVMConverter method scale.
private List<Feature> scale(List<Feature> features, RExpEncoder encoder) {
RGenericVector svm = getObject();
RDoubleVector sv = (RDoubleVector) svm.getValue("SV");
RBooleanVector scaled = (RBooleanVector) svm.getValue("scaled");
RGenericVector xScale = (RGenericVector) svm.getValue("x.scale");
RStringVector rowNames = sv.dimnames(0);
RStringVector columnNames = sv.dimnames(1);
if ((scaled.size() != columnNames.size()) || (scaled.size() != features.size())) {
throw new IllegalArgumentException();
}
RDoubleVector xScaledCenter = null;
RDoubleVector xScaledScale = null;
if (xScale != null) {
xScaledCenter = (RDoubleVector) xScale.getValue("scaled:center");
xScaledScale = (RDoubleVector) xScale.getValue("scaled:scale");
}
List<Feature> result = new ArrayList<>();
for (int i = 0; i < columnNames.size(); i++) {
String columnName = columnNames.getValue(i);
Feature feature = features.get(i);
if (scaled.getValue(i)) {
feature = feature.toContinuousFeature();
FieldName name = FeatureUtil.createName("scale", feature);
DerivedField derivedField = encoder.getDerivedField(name);
if (derivedField == null) {
Double center = xScaledCenter.getValue(columnName);
Double scale = xScaledScale.getValue(columnName);
Apply apply = PMMLUtil.createApply("/", PMMLUtil.createApply("-", feature.ref(), PMMLUtil.createConstant(center)), PMMLUtil.createConstant(scale));
derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, apply);
}
feature = new ContinuousFeature(encoder, derivedField);
}
result.add(feature);
}
return result;
}
use of org.dmg.pmml.Apply in project jpmml-r by jpmml.
the class ExpressionCompactorTest method compactNegationExpression.
@Test
public void compactNegationExpression() {
FieldRef fieldRef = new FieldRef(FieldName.create("x"));
Constant constant = createConstant("0");
Apply apply = compact(createApply("not", createApply("equal", fieldRef, constant)));
assertEquals("notEqual", apply.getFunction());
assertEquals(Arrays.asList(fieldRef, constant), apply.getExpressions());
apply = compact(createApply("not", createApply("isMissing", fieldRef)));
assertEquals("isNotMissing", apply.getFunction());
assertEquals(Arrays.asList(fieldRef), apply.getExpressions());
}
use of org.dmg.pmml.Apply in project jpmml-r by jpmml.
the class ExpressionTranslatorTest method translateParenthesizedExpression.
@Test
public void translateParenthesizedExpression() {
Apply apply = (Apply) ExpressionTranslator.translateExpression("TRUE | TRUE & FALSE");
checkApply(apply, "or", Constant.class, Apply.class);
apply = (Apply) ExpressionTranslator.translateExpression("(TRUE | TRUE) & FALSE");
checkApply(apply, "and", Apply.class, Constant.class);
}
use of org.dmg.pmml.Apply 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"));
}
Aggregations