Search in sources :

Example 1 with MaxAbsScalerModel

use of org.apache.spark.ml.feature.MaxAbsScalerModel in project jpmml-sparkml by jpmml.

the class MaxAbsScalerModelConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    MaxAbsScalerModel transformer = getTransformer();
    List<Feature> features = encoder.getFeatures(transformer.getInputCol());
    Vector maxAbs = transformer.maxAbs();
    if (maxAbs.size() != features.size()) {
        throw new IllegalArgumentException();
    }
    List<Feature> result = new ArrayList<>();
    for (int i = 0; i < features.size(); i++) {
        Feature feature = features.get(i);
        double maxAbsUnzero = maxAbs.apply(i);
        if (maxAbsUnzero == 0d) {
            maxAbsUnzero = 1d;
        }
        if (!ValueUtil.isOne(maxAbsUnzero)) {
            ContinuousFeature continuousFeature = feature.toContinuousFeature();
            Expression expression = PMMLUtil.createApply("/", continuousFeature.ref(), PMMLUtil.createConstant(maxAbsUnzero));
            DerivedField derivedField = encoder.createDerivedField(formatName(transformer, i), OpType.CONTINUOUS, DataType.DOUBLE, expression);
            feature = new ContinuousFeature(encoder, derivedField);
        }
        result.add(feature);
    }
    return result;
}
Also used : ContinuousFeature(org.jpmml.converter.ContinuousFeature) Expression(org.dmg.pmml.Expression) MaxAbsScalerModel(org.apache.spark.ml.feature.MaxAbsScalerModel) 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 MaxAbsScalerModel (org.apache.spark.ml.feature.MaxAbsScalerModel)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