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;
}
Aggregations