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;
}
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);
}
Aggregations