use of org.jpmml.converter.ContinuousFeature in project jpmml-r by jpmml.
the class RExpEncoder method addFeature.
public void addFeature(Field<?> field) {
Feature feature;
OpType opType = field.getOpType();
switch(opType) {
case CATEGORICAL:
feature = new CategoricalFeature(this, (DataField) field);
break;
case CONTINUOUS:
feature = new ContinuousFeature(this, field);
break;
default:
throw new IllegalArgumentException();
}
addFeature(feature);
}
use of org.jpmml.converter.ContinuousFeature 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;
}
use of org.jpmml.converter.ContinuousFeature in project jpmml-sparkml by jpmml.
the class MinMaxScalerModelConverter method encodeFeatures.
@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
MinMaxScalerModel transformer = getTransformer();
double rescaleFactor = (transformer.getMax() - transformer.getMin());
double rescaleConstant = transformer.getMin();
List<Feature> features = encoder.getFeatures(transformer.getInputCol());
Vector originalMax = transformer.originalMax();
if (originalMax.size() != features.size()) {
throw new IllegalArgumentException();
}
Vector originalMin = transformer.originalMin();
if (originalMin.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();
double max = originalMax.apply(i);
double min = originalMin.apply(i);
Expression expression = PMMLUtil.createApply("/", PMMLUtil.createApply("-", continuousFeature.ref(), PMMLUtil.createConstant(min)), PMMLUtil.createConstant(max - min));
if (!ValueUtil.isOne(rescaleFactor)) {
expression = PMMLUtil.createApply("*", expression, PMMLUtil.createConstant(rescaleFactor));
}
if (!ValueUtil.isZero(rescaleConstant)) {
expression = PMMLUtil.createApply("+", expression, PMMLUtil.createConstant(rescaleConstant));
}
DerivedField derivedField = encoder.createDerivedField(formatName(transformer, i), OpType.CONTINUOUS, DataType.DOUBLE, expression);
result.add(new ContinuousFeature(encoder, derivedField));
}
return result;
}
use of org.jpmml.converter.ContinuousFeature 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;
}
use of org.jpmml.converter.ContinuousFeature in project jpmml-sparkml by jpmml.
the class BucketizerConverter method encodeFeatures.
@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
Bucketizer transformer = getTransformer();
Feature feature = encoder.getOnlyFeature(transformer.getInputCol());
ContinuousFeature continuousFeature = feature.toContinuousFeature();
Discretize discretize = new Discretize(continuousFeature.getName());
List<String> categories = new ArrayList<>();
double[] splits = transformer.getSplits();
for (int i = 0; i < (splits.length - 1); i++) {
String category = String.valueOf(i);
categories.add(category);
Interval interval = new Interval((i < (splits.length - 2)) ? Interval.Closure.CLOSED_OPEN : Interval.Closure.CLOSED_CLOSED).setLeftMargin(formatMargin(splits[i])).setRightMargin(formatMargin(splits[i + 1]));
DiscretizeBin discretizeBin = new DiscretizeBin(category, interval);
discretize.addDiscretizeBins(discretizeBin);
}
DerivedField derivedField = encoder.createDerivedField(formatName(transformer), OpType.CATEGORICAL, DataType.INTEGER, discretize);
return Collections.<Feature>singletonList(new CategoricalFeature(encoder, derivedField, categories));
}
Aggregations