Search in sources :

Example 1 with DerivedField

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

the class MVRConverter method encodeSchema.

@Override
public void encodeSchema(RExpEncoder encoder) {
    RGenericVector mvr = getObject();
    RDoubleVector coefficients = (RDoubleVector) mvr.getValue("coefficients");
    RDoubleVector scale = (RDoubleVector) mvr.getValue("scale", true);
    RExp terms = mvr.getValue("terms");
    final RGenericVector model = (RGenericVector) mvr.getValue("model");
    RStringVector rowNames = coefficients.dimnames(0);
    RStringVector columnNames = coefficients.dimnames(1);
    FormulaContext context = new ModelFrameFormulaContext(model);
    Formula formula = FormulaUtil.createFormula(terms, context, encoder);
    // Dependent variable
    {
        FieldName name = FieldName.create(columnNames.asScalar());
        DataField dataField = (DataField) encoder.getField(name);
        encoder.setLabel(dataField);
    }
    // Independent variables
    for (int i = 0; i < rowNames.size(); i++) {
        String rowName = rowNames.getValue(i);
        Feature feature = formula.resolveFeature(rowName);
        if (scale != null) {
            feature = feature.toContinuousFeature();
            Apply apply = PMMLUtil.createApply("/", feature.ref(), PMMLUtil.createConstant(scale.getValue(i)));
            DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("scale", feature), OpType.CONTINUOUS, DataType.DOUBLE, apply);
            feature = new ContinuousFeature(encoder, derivedField);
        }
        encoder.addFeature(feature);
    }
}
Also used : Apply(org.dmg.pmml.Apply) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DataField(org.dmg.pmml.DataField) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField)

Example 2 with DerivedField

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

the class SVMConverter method scale.

private List<Feature> scale(List<Feature> features, RExpEncoder encoder) {
    RGenericVector svm = getObject();
    RDoubleVector sv = (RDoubleVector) svm.getValue("SV");
    RBooleanVector scaled = (RBooleanVector) svm.getValue("scaled");
    RGenericVector xScale = (RGenericVector) svm.getValue("x.scale");
    RStringVector rowNames = sv.dimnames(0);
    RStringVector columnNames = sv.dimnames(1);
    if ((scaled.size() != columnNames.size()) || (scaled.size() != features.size())) {
        throw new IllegalArgumentException();
    }
    RDoubleVector xScaledCenter = null;
    RDoubleVector xScaledScale = null;
    if (xScale != null) {
        xScaledCenter = (RDoubleVector) xScale.getValue("scaled:center");
        xScaledScale = (RDoubleVector) xScale.getValue("scaled:scale");
    }
    List<Feature> result = new ArrayList<>();
    for (int i = 0; i < columnNames.size(); i++) {
        String columnName = columnNames.getValue(i);
        Feature feature = features.get(i);
        if (scaled.getValue(i)) {
            feature = feature.toContinuousFeature();
            FieldName name = FeatureUtil.createName("scale", feature);
            DerivedField derivedField = encoder.getDerivedField(name);
            if (derivedField == null) {
                Double center = xScaledCenter.getValue(columnName);
                Double scale = xScaledScale.getValue(columnName);
                Apply apply = PMMLUtil.createApply("/", PMMLUtil.createApply("-", feature.ref(), PMMLUtil.createConstant(center)), PMMLUtil.createConstant(scale));
                derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, apply);
            }
            feature = new ContinuousFeature(encoder, derivedField);
        }
        result.add(feature);
    }
    return result;
}
Also used : Apply(org.dmg.pmml.Apply) ArrayList(java.util.ArrayList) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) FieldName(org.dmg.pmml.FieldName) DerivedField(org.dmg.pmml.DerivedField)

Example 3 with DerivedField

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

the class PreProcessEncoder method filter.

private Schema filter(Schema schema) {
    Function<Feature, Feature> function = new Function<Feature, Feature>() {

        @Override
        public Feature apply(Feature feature) {
            Expression expression = encodeExpression(feature);
            if (expression == null) {
                return feature;
            }
            DerivedField derivedField = createDerivedField(FeatureUtil.createName("preProcess", feature), OpType.CONTINUOUS, DataType.DOUBLE, expression);
            return new ContinuousFeature(PreProcessEncoder.this, derivedField);
        }
    };
    return schema.toTransformedSchema(function);
}
Also used : Function(java.util.function.Function) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Expression(org.dmg.pmml.Expression) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) DerivedField(org.dmg.pmml.DerivedField)

Example 4 with DerivedField

use of org.dmg.pmml.DerivedField 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 5 with DerivedField

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

the class StandardScalerModelConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    StandardScalerModel transformer = getTransformer();
    List<Feature> features = encoder.getFeatures(transformer.getInputCol());
    Vector mean = transformer.mean();
    if (transformer.getWithMean() && mean.size() != features.size()) {
        throw new IllegalArgumentException();
    }
    Vector std = transformer.std();
    if (transformer.getWithStd() && std.size() != features.size()) {
        throw new IllegalArgumentException();
    }
    List<Feature> result = new ArrayList<>();
    for (int i = 0; i < features.size(); i++) {
        Feature feature = features.get(i);
        ContinuousFeature continuousFeature = feature.toContinuousFeature();
        Expression expression = continuousFeature.ref();
        if (transformer.getWithMean()) {
            double meanValue = mean.apply(i);
            if (!ValueUtil.isZero(meanValue)) {
                expression = PMMLUtil.createApply("-", expression, PMMLUtil.createConstant(meanValue));
            }
        }
        if (transformer.getWithStd()) {
            double stdValue = std.apply(i);
            if (!ValueUtil.isOne(stdValue)) {
                expression = PMMLUtil.createApply("*", expression, PMMLUtil.createConstant(1d / stdValue));
            }
        }
        DerivedField derivedField = encoder.createDerivedField(formatName(transformer, i), OpType.CONTINUOUS, DataType.DOUBLE, expression);
        result.add(new ContinuousFeature(encoder, derivedField));
    }
    return result;
}
Also used : StandardScalerModel(org.apache.spark.ml.feature.StandardScalerModel) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Expression(org.dmg.pmml.Expression) ArrayList(java.util.ArrayList) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Vector(org.apache.spark.ml.linalg.Vector) DerivedField(org.dmg.pmml.DerivedField)

Aggregations

DerivedField (org.dmg.pmml.DerivedField)44 ArrayList (java.util.ArrayList)17 Feature (org.jpmml.converter.Feature)14 ContinuousFeature (org.jpmml.converter.ContinuousFeature)13 FieldName (org.dmg.pmml.FieldName)11 Apply (org.dmg.pmml.Apply)10 Expression (org.dmg.pmml.Expression)8 Test (org.junit.Test)8 KiePMMLDerivedField (org.kie.pmml.commons.transformations.KiePMMLDerivedField)8 Constant (org.dmg.pmml.Constant)7 NormContinuous (org.dmg.pmml.NormContinuous)6 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)5 List (java.util.List)5 DataField (org.dmg.pmml.DataField)5 CategoricalFeature (org.jpmml.converter.CategoricalFeature)5 Discretize (org.dmg.pmml.Discretize)4 FieldRef (org.dmg.pmml.FieldRef)4 MapValues (org.dmg.pmml.MapValues)4 Statement (com.github.javaparser.ast.stmt.Statement)3 HashMap (java.util.HashMap)3