Search in sources :

Example 1 with AbstractTransformation

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

Aggregations

ArrayList (java.util.ArrayList)1 FieldName (org.dmg.pmml.FieldName)1 FieldRef (org.dmg.pmml.FieldRef)1 MiningModel (org.dmg.pmml.mining.MiningModel)1 TreeModel (org.dmg.pmml.tree.TreeModel)1 AbstractTransformation (org.jpmml.converter.AbstractTransformation)1 Schema (org.jpmml.converter.Schema)1 Transformation (org.jpmml.converter.Transformation)1