Search in sources :

Example 21 with Apply

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

the class ExpressionTranslatorTest method translateArithmeticExpressionChain.

@Test
public void translateArithmeticExpressionChain() {
    Apply apply = (Apply) ExpressionTranslator.translateExpression("A + B - X + C");
    List<Expression> expressions = checkApply(apply, "+", Apply.class, FieldRef.class);
    Expression left = expressions.get(0);
    Expression right = expressions.get(1);
    expressions = checkApply((Apply) left, "-", Apply.class, FieldRef.class);
    checkFieldRef((FieldRef) right, FieldName.create("C"));
    left = expressions.get(0);
    right = expressions.get(1);
    expressions = checkApply((Apply) left, "+", FieldRef.class, FieldRef.class);
    checkFieldRef((FieldRef) right, FieldName.create("X"));
    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) Test(org.junit.Test)

Example 22 with Apply

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

the class PCAModelConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    PCAModel transformer = getTransformer();
    List<Feature> features = encoder.getFeatures(transformer.getInputCol());
    DenseMatrix pc = transformer.pc();
    if (pc.numRows() != features.size()) {
        throw new IllegalArgumentException();
    }
    List<Feature> result = new ArrayList<>();
    for (int i = 0; i < transformer.getK(); i++) {
        Apply apply = new Apply("sum");
        for (int j = 0; j < features.size(); j++) {
            Feature feature = features.get(j);
            ContinuousFeature continuousFeature = feature.toContinuousFeature();
            Expression expression = continuousFeature.ref();
            Double coefficient = pc.apply(j, i);
            if (!ValueUtil.isOne(coefficient)) {
                expression = PMMLUtil.createApply("*", expression, PMMLUtil.createConstant(coefficient));
            }
            apply.addExpressions(expression);
        }
        DerivedField derivedField = encoder.createDerivedField(formatName(transformer, i), OpType.CONTINUOUS, DataType.DOUBLE, apply);
        result.add(new ContinuousFeature(encoder, derivedField));
    }
    return result;
}
Also used : PCAModel(org.apache.spark.ml.feature.PCAModel) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Expression(org.dmg.pmml.Expression) Apply(org.dmg.pmml.Apply) ArrayList(java.util.ArrayList) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DerivedField(org.dmg.pmml.DerivedField) DenseMatrix(org.apache.spark.ml.linalg.DenseMatrix)

Example 23 with Apply

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

the class WeightedTermFeature method createApply.

@Override
public Apply createApply() {
    Double weight = getWeight();
    Apply apply = super.createApply().addExpressions(PMMLUtil.createConstant(weight));
    return apply;
}
Also used : Apply(org.dmg.pmml.Apply)

Example 24 with Apply

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

the class StringIndexerModelConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    StringIndexerModel transformer = getTransformer();
    Feature feature = encoder.getOnlyFeature(transformer.getInputCol());
    List<String> categories = new ArrayList<>();
    categories.addAll(Arrays.asList(transformer.labels()));
    String handleInvalid = transformer.getHandleInvalid();
    Field<?> field = encoder.toCategorical(feature.getName(), categories);
    if (field instanceof DataField) {
        DataField dataField = (DataField) field;
        InvalidValueTreatmentMethod invalidValueTreatmentMethod;
        switch(handleInvalid) {
            case "keep":
                invalidValueTreatmentMethod = InvalidValueTreatmentMethod.AS_IS;
                break;
            case "error":
                invalidValueTreatmentMethod = InvalidValueTreatmentMethod.RETURN_INVALID;
                break;
            default:
                throw new IllegalArgumentException(handleInvalid);
        }
        InvalidValueDecorator invalidValueDecorator = new InvalidValueDecorator().setInvalidValueTreatment(invalidValueTreatmentMethod);
        encoder.addDecorator(dataField.getName(), invalidValueDecorator);
    } else if (field instanceof DerivedField) {
    // Ignored
    } else {
        throw new IllegalArgumentException();
    }
    switch(handleInvalid) {
        case "keep":
            Apply setApply = PMMLUtil.createApply("isIn", feature.ref());
            for (String category : categories) {
                setApply.addExpressions(PMMLUtil.createConstant(category, feature.getDataType()));
            }
            categories.add(StringIndexerModelConverter.LABEL_UNKNOWN);
            Apply apply = PMMLUtil.createApply("if", setApply, feature.ref(), PMMLUtil.createConstant(StringIndexerModelConverter.LABEL_UNKNOWN, DataType.STRING));
            field = encoder.createDerivedField(FeatureUtil.createName("handleInvalid", feature), OpType.CATEGORICAL, feature.getDataType(), apply);
            break;
        default:
            break;
    }
    return Collections.<Feature>singletonList(new CategoricalFeature(encoder, field, categories));
}
Also used : Apply(org.dmg.pmml.Apply) ArrayList(java.util.ArrayList) Feature(org.jpmml.converter.Feature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) StringIndexerModel(org.apache.spark.ml.feature.StringIndexerModel) CategoricalFeature(org.jpmml.converter.CategoricalFeature) InvalidValueTreatmentMethod(org.dmg.pmml.InvalidValueTreatmentMethod) InvalidValueDecorator(org.jpmml.converter.InvalidValueDecorator) DataField(org.dmg.pmml.DataField) DerivedField(org.dmg.pmml.DerivedField)

Example 25 with Apply

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

the class TokenizerConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    Tokenizer transformer = getTransformer();
    Feature feature = encoder.getOnlyFeature(transformer.getInputCol());
    Apply apply = PMMLUtil.createApply("lowercase", feature.ref());
    DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("lowercase", feature), OpType.CATEGORICAL, DataType.STRING, apply);
    return Collections.<Feature>singletonList(new DocumentFeature(encoder, derivedField, "\\s+"));
}
Also used : Apply(org.dmg.pmml.Apply) DocumentFeature(org.jpmml.sparkml.DocumentFeature) Tokenizer(org.apache.spark.ml.feature.Tokenizer) Feature(org.jpmml.converter.Feature) DocumentFeature(org.jpmml.sparkml.DocumentFeature) DerivedField(org.dmg.pmml.DerivedField)

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