Search in sources :

Example 1 with Apply

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);
    }
}
Also used : Apply(org.dmg.pmml.Apply) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DataField(org.dmg.pmml.DataField) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField)

Example 2 with Apply

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;
}
Also used : Apply(org.dmg.pmml.Apply) ArrayList(java.util.ArrayList) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField)

Example 3 with Apply

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());
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Constant(org.dmg.pmml.Constant) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 4 with Apply

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);
}
Also used : Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 5 with Apply

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

Aggregations

Apply (org.dmg.pmml.Apply)25 Expression (org.dmg.pmml.Expression)11 Test (org.junit.Test)10 DerivedField (org.dmg.pmml.DerivedField)9 Feature (org.jpmml.converter.Feature)9 ContinuousFeature (org.jpmml.converter.ContinuousFeature)7 FieldRef (org.dmg.pmml.FieldRef)6 ArrayList (java.util.ArrayList)5 Constant (org.dmg.pmml.Constant)5 DataField (org.dmg.pmml.DataField)3 FieldName (org.dmg.pmml.FieldName)3 CategoricalFeature (org.jpmml.converter.CategoricalFeature)3 InteractionFeature (org.jpmml.converter.InteractionFeature)2 PMMLEncoder (org.jpmml.converter.PMMLEncoder)2 DocumentFeature (org.jpmml.sparkml.DocumentFeature)2 Binarizer (org.apache.spark.ml.feature.Binarizer)1 PCAModel (org.apache.spark.ml.feature.PCAModel)1 RegexTokenizer (org.apache.spark.ml.feature.RegexTokenizer)1 StringIndexerModel (org.apache.spark.ml.feature.StringIndexerModel)1 Tokenizer (org.apache.spark.ml.feature.Tokenizer)1