Search in sources :

Example 1 with Transformation

use of org.jpmml.converter.Transformation in project jpmml-r by jpmml.

the class IForestConverter method encodeModel.

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

        @Override
        public FieldName getName(FieldName name) {
            return FieldName.create("normalizedPathLength");
        }

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

        @Override
        public FieldName getName(FieldName name) {
            return FieldName.create("anomalyScore");
        }

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

        @Override
        public Expression createExpression(FieldRef fieldRef) {
            return PMMLUtil.createApply("pow", PMMLUtil.createConstant(2d), PMMLUtil.createApply("*", PMMLUtil.createConstant(-1d), fieldRef));
        }
    };
    MiningModel miningModel = new MiningModel(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel())).setSegmentation(MiningModelUtil.createSegmentation(Segmentation.MultipleModelMethod.AVERAGE, treeModels)).setOutput(ModelUtil.createPredictedOutput(FieldName.create("rawPathLength"), OpType.CONTINUOUS, DataType.DOUBLE, normalizedPathLength, anomalyScore));
    return miningModel;
}
Also used : Transformation(org.jpmml.converter.Transformation) AbstractTransformation(org.jpmml.converter.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.AbstractTransformation) FieldName(org.dmg.pmml.FieldName)

Example 2 with Transformation

use of org.jpmml.converter.Transformation 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)

Aggregations

FieldRef (org.dmg.pmml.FieldRef)2 Transformation (org.jpmml.converter.Transformation)2 ArrayList (java.util.ArrayList)1 DataType (org.dmg.pmml.DataType)1 Expression (org.dmg.pmml.Expression)1 FieldName (org.dmg.pmml.FieldName)1 OpType (org.dmg.pmml.OpType)1 MiningModel (org.dmg.pmml.mining.MiningModel)1 LinearKernel (org.dmg.pmml.support_vector_machine.LinearKernel)1 PolynomialKernel (org.dmg.pmml.support_vector_machine.PolynomialKernel)1 RadialBasisKernel (org.dmg.pmml.support_vector_machine.RadialBasisKernel)1 SigmoidKernel (org.dmg.pmml.support_vector_machine.SigmoidKernel)1 SupportVectorMachineModel (org.dmg.pmml.support_vector_machine.SupportVectorMachineModel)1 TreeModel (org.dmg.pmml.tree.TreeModel)1 AbstractTransformation (org.jpmml.converter.AbstractTransformation)1 OutlierTransformation (org.jpmml.converter.OutlierTransformation)1 Schema (org.jpmml.converter.Schema)1