Search in sources :

Example 26 with FieldRef

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

the class IForestConverter method encodeModel.

@Override
public Model encodeModel(Schema schema) {
    RGenericVector iForest = getObject();
    RGenericVector trees = iForest.getGenericElement("trees");
    RDoubleVector ntree = iForest.getDoubleElement("ntree");
    if (trees == null) {
        throw new IllegalArgumentException();
    }
    RIntegerVector xrow = trees.getIntegerElement("xrow");
    Schema segmentSchema = schema.toAnonymousSchema();
    List<TreeModel> treeModels = new ArrayList<>();
    for (int i = 0; i < ValueUtil.asInt(ntree.asScalar()); i++) {
        TreeModel treeModel = encodeTreeModel(i, trees, segmentSchema);
        treeModels.add(treeModel);
    }
    // "rawPathLength / avgPathLength(xrow)"
    Transformation normalizedPathLength = new AbstractTransformation() {

        @Override
        public String getName(String name) {
            return "normalizedPathLength";
        }

        @Override
        public Expression createExpression(FieldRef fieldRef) {
            return PMMLUtil.createApply(PMMLFunctions.DIVIDE, fieldRef, PMMLUtil.createConstant(avgPathLength(xrow.asScalar())));
        }
    };
    // "2 ^ (-1 * normalizedPathLength)"
    Transformation anomalyScore = new AbstractTransformation() {

        @Override
        public String getName(String name) {
            return "anomalyScore";
        }

        @Override
        public boolean isFinalResult() {
            return true;
        }

        @Override
        public Expression createExpression(FieldRef fieldRef) {
            return PMMLUtil.createApply(PMMLFunctions.POW, PMMLUtil.createConstant(2d), PMMLUtil.createApply(PMMLFunctions.MULTIPLY, PMMLUtil.createConstant(-1d), fieldRef));
        }
    };
    MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel())).setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.AVERAGE, treeModels)).setOutput(ModelUtil.createPredictedOutput("rawPathLength", OpType.CONTINUOUS, DataType.DOUBLE, normalizedPathLength, anomalyScore));
    return miningModel;
}
Also used : Transformation(org.jpmml.converter.Transformation) AbstractTransformation(org.jpmml.converter.transformations.AbstractTransformation) FieldRef(org.dmg.pmml.FieldRef) Schema(org.jpmml.converter.Schema) ArrayList(java.util.ArrayList) TreeModel(org.dmg.pmml.tree.TreeModel) MiningModel(org.dmg.pmml.mining.MiningModel) AbstractTransformation(org.jpmml.converter.transformations.AbstractTransformation)

Example 27 with FieldRef

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

the class SVMConverter method encodeModel.

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

                    @Override
                    public Expression createExpression(FieldRef fieldRef) {
                        return PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL, fieldRef, PMMLUtil.createConstant(0d));
                    }
                };
                supportVectorMachineModel = encodeRegression(pmmlKernel, sv, rho, coefs, schema).setOutput(ModelUtil.createPredictedOutput("decisionFunction", OpType.CONTINUOUS, DataType.DOUBLE, outlier));
                if (yScale != null && yScale.size() > 0) {
                    throw new IllegalArgumentException();
                }
            }
            break;
        case EPS_REGRESSION:
        case NU_REGRESSION:
            {
                supportVectorMachineModel = encodeRegression(pmmlKernel, sv, rho, coefs, schema);
                if (yScale != null && yScale.size() > 0) {
                    RDoubleVector yScaledCenter = yScale.getDoubleElement("scaled:center");
                    RDoubleVector yScaledScale = yScale.getDoubleElement("scaled:scale");
                    supportVectorMachineModel.setTargets(ModelUtil.createRescaleTargets(-1d * yScaledScale.asScalar(), yScaledCenter.asScalar(), (ContinuousLabel) schema.getLabel()));
                }
            }
            break;
        default:
            throw new IllegalArgumentException();
    }
    return supportVectorMachineModel;
}
Also used : Transformation(org.jpmml.converter.Transformation) OutlierTransformation(org.jpmml.converter.transformations.OutlierTransformation) FieldRef(org.dmg.pmml.FieldRef) OutlierTransformation(org.jpmml.converter.transformations.OutlierTransformation) OpType(org.dmg.pmml.OpType) DataType(org.dmg.pmml.DataType) 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 28 with FieldRef

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

the class ExpressionTranslatorTest method translateLogicalExpression.

@Test
public void translateLogicalExpression() {
    String string = "a >= 0.0 & b >= 0.0 | c <= 0.0";
    Expression expected = PMMLUtil.createApply(PMMLFunctions.OR, PMMLUtil.createApply(PMMLFunctions.AND, PMMLUtil.createApply(PMMLFunctions.GREATEROREQUAL, new FieldRef("a"), PMMLUtil.createConstant("0.0", DataType.DOUBLE)), PMMLUtil.createApply(PMMLFunctions.GREATEROREQUAL, new FieldRef("b"), PMMLUtil.createConstant("0.0", DataType.DOUBLE))), PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL, new FieldRef("c"), PMMLUtil.createConstant("0.0", DataType.DOUBLE)));
    Expression actual = ExpressionTranslator.translateExpression(string);
    assertTrue(ReflectionUtil.equals(expected, actual));
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) FunctionExpression(org.jpmml.rexp.FunctionExpression) Test(org.junit.Test)

Example 29 with FieldRef

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

the class ExpressionTranslatorTest method translateRelationalExpression.

@Test
public void translateRelationalExpression() {
    String string = "if(x < 0) \"negative\" else if(x > 0) \"positive\" else \"zero\"";
    Expression expected = PMMLUtil.createApply(PMMLFunctions.IF, PMMLUtil.createApply(PMMLFunctions.LESSTHAN, new FieldRef("x"), PMMLUtil.createConstant("0", DataType.INTEGER)), PMMLUtil.createConstant("negative", DataType.STRING), PMMLUtil.createApply(PMMLFunctions.IF, PMMLUtil.createApply(PMMLFunctions.GREATERTHAN, new FieldRef("x"), PMMLUtil.createConstant("0", DataType.INTEGER)), PMMLUtil.createConstant("positive", DataType.STRING), PMMLUtil.createConstant("zero", DataType.STRING)));
    Expression actual = ExpressionTranslator.translateExpression(string);
    assertTrue(ReflectionUtil.equals(expected, actual));
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) FunctionExpression(org.jpmml.rexp.FunctionExpression) Test(org.junit.Test)

Example 30 with FieldRef

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

the class ExpressionTranslatorTest method translateIfExpression.

@Test
public void translateIfExpression() {
    String string = "if(is.na(x)) TRUE else FALSE";
    Expression expected = PMMLUtil.createApply(PMMLFunctions.IF, PMMLUtil.createApply(PMMLFunctions.ISMISSING, new FieldRef("x")), PMMLUtil.createConstant("true", DataType.BOOLEAN), PMMLUtil.createConstant("false", DataType.BOOLEAN));
    Expression actual = ExpressionTranslator.translateExpression(string);
    assertTrue(ReflectionUtil.equals(expected, actual));
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) FunctionExpression(org.jpmml.rexp.FunctionExpression) Test(org.junit.Test)

Aggregations

FieldRef (org.dmg.pmml.FieldRef)40 Test (org.junit.Test)23 Apply (org.dmg.pmml.Apply)17 Expression (org.dmg.pmml.Expression)11 KiePMMLFieldRef (org.kie.pmml.commons.model.expressions.KiePMMLFieldRef)10 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)8 Statement (com.github.javaparser.ast.stmt.Statement)8 Constant (org.dmg.pmml.Constant)7 DerivedField (org.dmg.pmml.DerivedField)7 FunctionExpression (org.jpmml.rexp.FunctionExpression)7 ArrayList (java.util.ArrayList)6 FieldName (org.dmg.pmml.FieldName)6 KiePMMLApply (org.kie.pmml.commons.model.expressions.KiePMMLApply)6 DefineFunction (org.dmg.pmml.DefineFunction)5 ParameterField (org.dmg.pmml.ParameterField)5 KiePMMLConstant (org.kie.pmml.commons.model.expressions.KiePMMLConstant)4 List (java.util.List)3 DataType (org.dmg.pmml.DataType)3 OpType (org.dmg.pmml.OpType)3 Transformation (org.jpmml.converter.Transformation)3