Search in sources :

Example 11 with Expression

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

the class SVMConverter method encodeModel.

@Override
public SupportVectorMachineModel encodeModel(Schema schema) {
    RGenericVector svm = getObject();
    RDoubleVector type = (RDoubleVector) svm.getValue("type");
    RDoubleVector kernel = (RDoubleVector) svm.getValue("kernel");
    RDoubleVector degree = (RDoubleVector) svm.getValue("degree");
    RDoubleVector gamma = (RDoubleVector) svm.getValue("gamma");
    RDoubleVector coef0 = (RDoubleVector) svm.getValue("coef0");
    RGenericVector yScale = (RGenericVector) svm.getValue("y.scale");
    RIntegerVector nSv = (RIntegerVector) svm.getValue("nSV");
    RDoubleVector sv = (RDoubleVector) svm.getValue("SV");
    RDoubleVector rho = (RDoubleVector) svm.getValue("rho");
    RDoubleVector coefs = (RDoubleVector) svm.getValue("coefs");
    Type svmType = Type.values()[ValueUtil.asInt(type.asScalar())];
    Kernel svmKernel = Kernel.values()[ValueUtil.asInt(kernel.asScalar())];
    SupportVectorMachineModel supportVectorMachineModel;
    switch(svmType) {
        case C_CLASSIFICATION:
        case NU_CLASSIFICATION:
            {
                supportVectorMachineModel = encodeClassification(sv, nSv, rho, coefs, schema);
            }
            break;
        case ONE_CLASSIFICATION:
            {
                Transformation outlier = new OutlierTransformation() {

                    @Override
                    public Expression createExpression(FieldRef fieldRef) {
                        return PMMLUtil.createApply("lessOrEqual", fieldRef, PMMLUtil.createConstant(0d));
                    }
                };
                supportVectorMachineModel = encodeRegression(sv, rho, coefs, schema).setOutput(ModelUtil.createPredictedOutput(FieldName.create("decisionFunction"), OpType.CONTINUOUS, DataType.DOUBLE, outlier));
                if (yScale != null && yScale.size() > 0) {
                    throw new IllegalArgumentException();
                }
            }
            break;
        case EPS_REGRESSION:
        case NU_REGRESSION:
            {
                supportVectorMachineModel = encodeRegression(sv, rho, coefs, schema);
                if (yScale != null && yScale.size() > 0) {
                    RDoubleVector yScaledCenter = (RDoubleVector) yScale.getValue("scaled:center");
                    RDoubleVector yScaledScale = (RDoubleVector) yScale.getValue("scaled:scale");
                    supportVectorMachineModel.setTargets(ModelUtil.createRescaleTargets(-1d * yScaledScale.asScalar(), yScaledCenter.asScalar(), (ContinuousLabel) schema.getLabel()));
                }
            }
            break;
        default:
            throw new IllegalArgumentException();
    }
    supportVectorMachineModel.setKernel(svmKernel.createKernel(degree.asScalar(), gamma.asScalar(), coef0.asScalar()));
    return supportVectorMachineModel;
}
Also used : OpType(org.dmg.pmml.OpType) DataType(org.dmg.pmml.DataType) Transformation(org.jpmml.converter.Transformation) OutlierTransformation(org.jpmml.converter.OutlierTransformation) FieldRef(org.dmg.pmml.FieldRef) OutlierTransformation(org.jpmml.converter.OutlierTransformation) Expression(org.dmg.pmml.Expression) SupportVectorMachineModel(org.dmg.pmml.support_vector_machine.SupportVectorMachineModel) RadialBasisKernel(org.dmg.pmml.support_vector_machine.RadialBasisKernel) PolynomialKernel(org.dmg.pmml.support_vector_machine.PolynomialKernel) LinearKernel(org.dmg.pmml.support_vector_machine.LinearKernel) SigmoidKernel(org.dmg.pmml.support_vector_machine.SigmoidKernel)

Example 12 with Expression

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

the class ExpressionTranslatorTest method translateLogicalExpression.

@Test
public void translateLogicalExpression() {
    Apply apply = (Apply) ExpressionTranslator.translateExpression("a >= 0.0 & b >= 0.0 | c <= 0.0");
    List<Expression> expressions = checkApply(apply, "or", Apply.class, Apply.class);
    Expression left = expressions.get(0);
    Expression right = expressions.get(1);
    expressions = checkApply((Apply) left, "and", Apply.class, Apply.class);
    checkApply((Apply) right, "lessOrEqual", FieldRef.class, Constant.class);
    left = expressions.get(0);
    right = expressions.get(1);
    checkApply((Apply) left, "greaterOrEqual", FieldRef.class, Constant.class);
    checkApply((Apply) right, "greaterOrEqual", FieldRef.class, Constant.class);
}
Also used : Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 13 with Expression

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

the class ExpressionTranslatorTest method checkFunctionExpression.

private static List<Expression> checkFunctionExpression(FunctionExpression functionExpression, String function, String... tags) {
    assertEquals(function, functionExpression.getFunction());
    List<FunctionExpression.Argument> arguments = functionExpression.getArguments();
    assertEquals(tags.length, arguments.size());
    List<Expression> expressions = new ArrayList<>();
    for (int i = 0; i < arguments.size(); i++) {
        FunctionExpression.Argument argument = arguments.get(i);
        String tag = argument.getTag();
        Expression expression = argument.getExpression();
        assertEquals(tag, tags[i]);
        expressions.add(expression);
    }
    return expressions;
}
Also used : Expression(org.dmg.pmml.Expression) ArrayList(java.util.ArrayList)

Example 14 with Expression

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

the class ExpressionTranslatorTest method translateExponentiationExpression.

@Test
public void translateExponentiationExpression() {
    Apply apply = (Apply) ExpressionTranslator.translateExpression("-2^-3");
    List<Expression> expressions = checkApply(apply, "*", Constant.class, Apply.class);
    Expression left = expressions.get(0);
    Expression right = expressions.get(1);
    checkConstant((Constant) left, "-1", null);
    expressions = checkApply((Apply) right, "pow", Constant.class, Constant.class);
    left = expressions.get(0);
    right = expressions.get(1);
    checkConstant((Constant) left, "2", null);
    checkConstant((Constant) right, "-3", null);
    apply = (Apply) ExpressionTranslator.translateExpression("-2^-2*1.5");
    expressions = checkApply(apply, "*", Apply.class, Constant.class);
    left = expressions.get(0);
    right = expressions.get(1);
    expressions = checkApply((Apply) left, "*", Constant.class, Apply.class);
    checkConstant((Constant) right, "1.5", DataType.DOUBLE);
    left = expressions.get(0);
    right = expressions.get(1);
    checkConstant((Constant) left, "-1", null);
    expressions = checkApply((Apply) right, "pow", Constant.class, Constant.class);
    left = expressions.get(0);
    right = expressions.get(1);
    checkConstant((Constant) left, "2", null);
    checkConstant((Constant) right, "-2", null);
}
Also used : Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) Constant(org.dmg.pmml.Constant) Test(org.junit.Test)

Example 15 with Expression

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

the class ExpressionTranslatorTest method checkApply.

private static List<Expression> checkApply(Apply apply, String function, Class<? extends Expression>... expressionClazzes) {
    assertEquals(function, apply.getFunction());
    List<Expression> expressions = apply.getExpressions();
    assertEquals(expressionClazzes.length, expressions.size());
    for (int i = 0; i < expressionClazzes.length; i++) {
        Class<? extends Expression> expressionClazz = expressionClazzes[i];
        Expression expression = expressions.get(i);
        assertEquals(expressionClazz, expression.getClass());
    }
    return expressions;
}
Also used : Expression(org.dmg.pmml.Expression)

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