Search in sources :

Example 1 with StandardScalerModel

use of org.apache.spark.ml.feature.StandardScalerModel 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

ArrayList (java.util.ArrayList)1 StandardScalerModel (org.apache.spark.ml.feature.StandardScalerModel)1 Vector (org.apache.spark.ml.linalg.Vector)1 DerivedField (org.dmg.pmml.DerivedField)1 Expression (org.dmg.pmml.Expression)1 ContinuousFeature (org.jpmml.converter.ContinuousFeature)1 Feature (org.jpmml.converter.Feature)1