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