Search in sources :

Example 6 with Apply

use of org.dmg.pmml.Apply in project jpmml-r by jpmml.

the class ExpressionTranslatorTest method translateLogicalExpressionChain.

@Test
public void translateLogicalExpressionChain() {
    String string = "(x == 0) | ((x == 1) | (x == 2)) | x == 3";
    Apply apply = (Apply) ExpressionTranslator.translateExpression(string, false);
    List<Expression> expressions = checkApply(apply, "or", Apply.class, Apply.class);
    Expression left = expressions.get(0);
    Expression right = expressions.get(1);
    checkApply((Apply) left, "or", Apply.class, Apply.class);
    checkApply((Apply) right, "equal", FieldRef.class, Constant.class);
    apply = (Apply) ExpressionTranslator.translateExpression(string, true);
    checkApply(apply, "or", Apply.class, Apply.class, Apply.class, Apply.class);
}
Also used : Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) Test(org.junit.Test)

Example 7 with Apply

use of org.dmg.pmml.Apply in project jpmml-sparkml by jpmml.

the class TermFeature method toContinuousFeature.

@Override
public ContinuousFeature toContinuousFeature() {
    PMMLEncoder encoder = ensureEncoder();
    DerivedField derivedField = encoder.getDerivedField(getName());
    if (derivedField == null) {
        Apply apply = createApply();
        derivedField = encoder.createDerivedField(getName(), OpType.CONTINUOUS, getDataType(), apply);
    }
    return new ContinuousFeature(encoder, derivedField);
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) Apply(org.dmg.pmml.Apply) PMMLEncoder(org.jpmml.converter.PMMLEncoder) DerivedField(org.dmg.pmml.DerivedField)

Example 8 with Apply

use of org.dmg.pmml.Apply 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 9 with Apply

use of org.dmg.pmml.Apply in project jpmml-sparkml by jpmml.

the class RegexTokenizerConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    RegexTokenizer transformer = getTransformer();
    if (!transformer.getGaps()) {
        throw new IllegalArgumentException("Expected splitter mode, got token matching mode");
    }
    if (transformer.getMinTokenLength() != 1) {
        throw new IllegalArgumentException("Expected 1 as minimum token length, got " + transformer.getMinTokenLength() + " as minimum token length");
    }
    Feature feature = encoder.getOnlyFeature(transformer.getInputCol());
    Field<?> field = encoder.getField(feature.getName());
    if (transformer.getToLowercase()) {
        Apply apply = PMMLUtil.createApply("lowercase", feature.ref());
        field = encoder.createDerivedField(FeatureUtil.createName("lowercase", feature), OpType.CATEGORICAL, DataType.STRING, apply);
    }
    return Collections.<Feature>singletonList(new DocumentFeature(encoder, field, transformer.getPattern()));
}
Also used : Apply(org.dmg.pmml.Apply) RegexTokenizer(org.apache.spark.ml.feature.RegexTokenizer) DocumentFeature(org.jpmml.sparkml.DocumentFeature) Feature(org.jpmml.converter.Feature) DocumentFeature(org.jpmml.sparkml.DocumentFeature)

Example 10 with Apply

use of org.dmg.pmml.Apply in project jpmml-sparkml by jpmml.

the class BinarizerConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    Binarizer transformer = getTransformer();
    Feature feature = encoder.getOnlyFeature(transformer.getInputCol());
    ContinuousFeature continuousFeature = feature.toContinuousFeature();
    Apply apply = new Apply("if").addExpressions(PMMLUtil.createApply("lessOrEqual", continuousFeature.ref(), PMMLUtil.createConstant(transformer.getThreshold()))).addExpressions(PMMLUtil.createConstant(0d), PMMLUtil.createConstant(1d));
    DerivedField derivedField = encoder.createDerivedField(formatName(transformer), OpType.CATEGORICAL, DataType.DOUBLE, apply);
    return Collections.<Feature>singletonList(new CategoricalFeature(encoder, derivedField, Arrays.asList("0", "1")));
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) Apply(org.dmg.pmml.Apply) Binarizer(org.apache.spark.ml.feature.Binarizer) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) DerivedField(org.dmg.pmml.DerivedField) CategoricalFeature(org.jpmml.converter.CategoricalFeature)

Aggregations

Apply (org.dmg.pmml.Apply)25 Expression (org.dmg.pmml.Expression)11 Test (org.junit.Test)10 DerivedField (org.dmg.pmml.DerivedField)9 Feature (org.jpmml.converter.Feature)9 ContinuousFeature (org.jpmml.converter.ContinuousFeature)7 FieldRef (org.dmg.pmml.FieldRef)6 ArrayList (java.util.ArrayList)5 Constant (org.dmg.pmml.Constant)5 DataField (org.dmg.pmml.DataField)3 FieldName (org.dmg.pmml.FieldName)3 CategoricalFeature (org.jpmml.converter.CategoricalFeature)3 InteractionFeature (org.jpmml.converter.InteractionFeature)2 PMMLEncoder (org.jpmml.converter.PMMLEncoder)2 DocumentFeature (org.jpmml.sparkml.DocumentFeature)2 Binarizer (org.apache.spark.ml.feature.Binarizer)1 PCAModel (org.apache.spark.ml.feature.PCAModel)1 RegexTokenizer (org.apache.spark.ml.feature.RegexTokenizer)1 StringIndexerModel (org.apache.spark.ml.feature.StringIndexerModel)1 Tokenizer (org.apache.spark.ml.feature.Tokenizer)1