use of org.dmg.pmml.FieldRef in project jpmml-r by jpmml.
the class ExpressionCompactorTest method compactNegationExpression.
@Test
public void compactNegationExpression() {
FieldRef fieldRef = new FieldRef(FieldName.create("x"));
Constant constant = createConstant("0");
Apply apply = compact(createApply("not", createApply("equal", fieldRef, constant)));
assertEquals("notEqual", apply.getFunction());
assertEquals(Arrays.asList(fieldRef, constant), apply.getExpressions());
apply = compact(createApply("not", createApply("isMissing", fieldRef)));
assertEquals("isNotMissing", apply.getFunction());
assertEquals(Arrays.asList(fieldRef), apply.getExpressions());
}
use of org.dmg.pmml.FieldRef in project jpmml-r by jpmml.
the class ExpressionTranslatorTest method translate.
@Test
public void translate() {
Apply apply = (Apply) ExpressionTranslator.translateExpression("(1.0 + log(A / B)) ^ 2");
List<Expression> expressions = checkApply(apply, "pow", Apply.class, Constant.class);
Expression left = expressions.get(0);
Expression right = expressions.get(1);
checkConstant((Constant) right, "2", null);
expressions = checkApply((Apply) left, "+", Constant.class, Apply.class);
left = expressions.get(0);
right = expressions.get(1);
checkConstant((Constant) left, "1.0", DataType.DOUBLE);
expressions = checkApply((Apply) right, "ln", Apply.class);
left = expressions.get(0);
expressions = checkApply((Apply) left, "/", FieldRef.class, FieldRef.class);
left = expressions.get(0);
right = expressions.get(1);
checkFieldRef((FieldRef) left, FieldName.create("A"));
checkFieldRef((FieldRef) right, FieldName.create("B"));
}
use of org.dmg.pmml.FieldRef in project jpmml-sparkml by jpmml.
the class TermFeature method toWeightedTermFeature.
public WeightedTermFeature toWeightedTermFeature(double weight) {
PMMLEncoder encoder = ensureEncoder();
DefineFunction defineFunction = getDefineFunction();
String name = (defineFunction.getName()).replace("tf@", "tf-idf@");
DefineFunction weightedDefineFunction = encoder.getDefineFunction(name);
if (weightedDefineFunction == null) {
ParameterField weightField = new ParameterField(FieldName.create("weight"));
List<ParameterField> parameterFields = new ArrayList<>(defineFunction.getParameterFields());
parameterFields.add(weightField);
Apply apply = PMMLUtil.createApply("*", defineFunction.getExpression(), new FieldRef(weightField.getName()));
weightedDefineFunction = new DefineFunction(name, OpType.CONTINUOUS, parameterFields).setDataType(DataType.DOUBLE).setExpression(apply);
encoder.addDefineFunction(weightedDefineFunction);
}
return new WeightedTermFeature(encoder, weightedDefineFunction, getFeature(), getValue(), weight);
}
use of org.dmg.pmml.FieldRef in project shifu by ShifuML.
the class PMMLAdapterCommonUtil method getOutputFields.
/**
* Create PMML neural output for the neural network models
*
* @param schema
* the schema
* @param layerID
* which layer the output neuron lies
* @return neural outputs
*/
public static NeuralOutputs getOutputFields(final MiningSchema schema, final int layerID) {
List<String> outputID = getSchemaFieldViaUsageType(schema, UsageType.TARGET);
NeuralOutputs outputs = new NeuralOutputs();
int outputFieldsNum = outputID.size();
outputs.setNumberOfOutputs(outputFieldsNum);
/* if ( outputFieldsNum > 0 ) {
for (int i = 0; i < outputFieldsNum; i++) {
DerivedField field = new DerivedField(OpType.CONTINUOUS, DataType.DOUBLE);
field.withExpression(new NormDiscrete()
.withField(new FieldName(outputID.get(i)))
.withValue(outputID.get(i)));
outputs.withNeuralOutputs(new NeuralOutput(field, String.valueOf(layerID + "," + i)));
}
} else {*/
for (int i = 0; i < outputFieldsNum; i++) {
DerivedField field = new DerivedField(OpType.CONTINUOUS, DataType.DOUBLE);
field.setExpression(new FieldRef(new FieldName(outputID.get(i))));
outputs.addNeuralOutputs(new NeuralOutput(String.valueOf(layerID + "," + i), field));
}
/* }*/
return outputs;
}
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 = (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;
}
Aggregations