Search in sources :

Example 1 with FieldRef

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());
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Constant(org.dmg.pmml.Constant) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 2 with FieldRef

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"));
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) Constant(org.dmg.pmml.Constant) Test(org.junit.Test)

Example 3 with FieldRef

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);
}
Also used : FieldRef(org.dmg.pmml.FieldRef) Apply(org.dmg.pmml.Apply) PMMLEncoder(org.jpmml.converter.PMMLEncoder) ArrayList(java.util.ArrayList) DefineFunction(org.dmg.pmml.DefineFunction) ParameterField(org.dmg.pmml.ParameterField)

Example 4 with FieldRef

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;
}
Also used : FieldRef(org.dmg.pmml.FieldRef) DerivedField(org.dmg.pmml.DerivedField) FieldName(org.dmg.pmml.FieldName)

Example 5 with FieldRef

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

FieldRef (org.dmg.pmml.FieldRef)12 Apply (org.dmg.pmml.Apply)6 Expression (org.dmg.pmml.Expression)5 ArrayList (java.util.ArrayList)4 FieldName (org.dmg.pmml.FieldName)4 Test (org.junit.Test)4 Constant (org.dmg.pmml.Constant)3 DerivedField (org.dmg.pmml.DerivedField)3 DefineFunction (org.dmg.pmml.DefineFunction)2 ParameterField (org.dmg.pmml.ParameterField)2 Feature (org.jpmml.converter.Feature)2 Transformation (org.jpmml.converter.Transformation)2 HashMap (java.util.HashMap)1 List (java.util.List)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 CountVectorizerModel (org.apache.spark.ml.feature.CountVectorizerModel)1 DataType (org.dmg.pmml.DataType)1 Discretize (org.dmg.pmml.Discretize)1 InlineTable (org.dmg.pmml.InlineTable)1 MapValues (org.dmg.pmml.MapValues)1