Search in sources :

Example 1 with AbstractTransformation

use of org.jpmml.converter.transformations.AbstractTransformation 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 2 with AbstractTransformation

use of org.jpmml.converter.transformations.AbstractTransformation in project jpmml-sparkml by jpmml.

the class LinearSVCModelConverter method encodeModel.

@Override
public MiningModel encodeModel(Schema schema) {
    LinearSVCModel model = getTransformer();
    Transformation transformation = new AbstractTransformation() {

        @Override
        public FieldName getName(FieldName name) {
            return FieldNameUtil.create("threshold", name);
        }

        @Override
        public Expression createExpression(FieldRef fieldRef) {
            return PMMLUtil.createApply(PMMLFunctions.THRESHOLD).addExpressions(fieldRef, PMMLUtil.createConstant(model.getThreshold()));
        }
    };
    Schema segmentSchema = schema.toAnonymousRegressorSchema(DataType.DOUBLE);
    Model linearModel = LinearModelUtil.createRegression(this, model.coefficients(), model.intercept(), segmentSchema).setOutput(ModelUtil.createPredictedOutput(FieldName.create("margin"), OpType.CONTINUOUS, DataType.DOUBLE, transformation));
    return MiningModelUtil.createBinaryLogisticClassification(linearModel, 1d, 0d, RegressionModel.NormalizationMethod.NONE, false, schema);
}
Also used : Transformation(org.jpmml.converter.Transformation) AbstractTransformation(org.jpmml.converter.transformations.AbstractTransformation) FieldRef(org.dmg.pmml.FieldRef) AbstractTransformation(org.jpmml.converter.transformations.AbstractTransformation) Schema(org.jpmml.converter.Schema) RegressionModel(org.dmg.pmml.regression.RegressionModel) Model(org.dmg.pmml.Model) LinearSVCModel(org.apache.spark.ml.classification.LinearSVCModel) MiningModel(org.dmg.pmml.mining.MiningModel) FieldName(org.dmg.pmml.FieldName) LinearSVCModel(org.apache.spark.ml.classification.LinearSVCModel)

Aggregations

FieldRef (org.dmg.pmml.FieldRef)2 MiningModel (org.dmg.pmml.mining.MiningModel)2 Schema (org.jpmml.converter.Schema)2 Transformation (org.jpmml.converter.Transformation)2 AbstractTransformation (org.jpmml.converter.transformations.AbstractTransformation)2 ArrayList (java.util.ArrayList)1 LinearSVCModel (org.apache.spark.ml.classification.LinearSVCModel)1 FieldName (org.dmg.pmml.FieldName)1 Model (org.dmg.pmml.Model)1 RegressionModel (org.dmg.pmml.regression.RegressionModel)1 TreeModel (org.dmg.pmml.tree.TreeModel)1