use of org.apache.spark.ml.feature.PCAModel 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;
}
Aggregations