use of org.dmg.pmml.DerivedField in project jpmml-r by jpmml.
the class MVRConverter method encodeSchema.
@Override
public void encodeSchema(RExpEncoder encoder) {
RGenericVector mvr = getObject();
RDoubleVector coefficients = (RDoubleVector) mvr.getValue("coefficients");
RDoubleVector scale = (RDoubleVector) mvr.getValue("scale", true);
RExp terms = mvr.getValue("terms");
final RGenericVector model = (RGenericVector) mvr.getValue("model");
RStringVector rowNames = coefficients.dimnames(0);
RStringVector columnNames = coefficients.dimnames(1);
FormulaContext context = new ModelFrameFormulaContext(model);
Formula formula = FormulaUtil.createFormula(terms, context, encoder);
// Dependent variable
{
FieldName name = FieldName.create(columnNames.asScalar());
DataField dataField = (DataField) encoder.getField(name);
encoder.setLabel(dataField);
}
// Independent variables
for (int i = 0; i < rowNames.size(); i++) {
String rowName = rowNames.getValue(i);
Feature feature = formula.resolveFeature(rowName);
if (scale != null) {
feature = feature.toContinuousFeature();
Apply apply = PMMLUtil.createApply("/", feature.ref(), PMMLUtil.createConstant(scale.getValue(i)));
DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("scale", feature), OpType.CONTINUOUS, DataType.DOUBLE, apply);
feature = new ContinuousFeature(encoder, derivedField);
}
encoder.addFeature(feature);
}
}
use of org.dmg.pmml.DerivedField in project jpmml-r by jpmml.
the class SVMConverter method scale.
private List<Feature> scale(List<Feature> features, RExpEncoder encoder) {
RGenericVector svm = getObject();
RDoubleVector sv = (RDoubleVector) svm.getValue("SV");
RBooleanVector scaled = (RBooleanVector) svm.getValue("scaled");
RGenericVector xScale = (RGenericVector) svm.getValue("x.scale");
RStringVector rowNames = sv.dimnames(0);
RStringVector columnNames = sv.dimnames(1);
if ((scaled.size() != columnNames.size()) || (scaled.size() != features.size())) {
throw new IllegalArgumentException();
}
RDoubleVector xScaledCenter = null;
RDoubleVector xScaledScale = null;
if (xScale != null) {
xScaledCenter = (RDoubleVector) xScale.getValue("scaled:center");
xScaledScale = (RDoubleVector) xScale.getValue("scaled:scale");
}
List<Feature> result = new ArrayList<>();
for (int i = 0; i < columnNames.size(); i++) {
String columnName = columnNames.getValue(i);
Feature feature = features.get(i);
if (scaled.getValue(i)) {
feature = feature.toContinuousFeature();
FieldName name = FeatureUtil.createName("scale", feature);
DerivedField derivedField = encoder.getDerivedField(name);
if (derivedField == null) {
Double center = xScaledCenter.getValue(columnName);
Double scale = xScaledScale.getValue(columnName);
Apply apply = PMMLUtil.createApply("/", PMMLUtil.createApply("-", feature.ref(), PMMLUtil.createConstant(center)), PMMLUtil.createConstant(scale));
derivedField = encoder.createDerivedField(name, OpType.CONTINUOUS, DataType.DOUBLE, apply);
}
feature = new ContinuousFeature(encoder, derivedField);
}
result.add(feature);
}
return result;
}
use of org.dmg.pmml.DerivedField in project jpmml-r by jpmml.
the class PreProcessEncoder method filter.
private Schema filter(Schema schema) {
Function<Feature, Feature> function = new Function<Feature, Feature>() {
@Override
public Feature apply(Feature feature) {
Expression expression = encodeExpression(feature);
if (expression == null) {
return feature;
}
DerivedField derivedField = createDerivedField(FeatureUtil.createName("preProcess", feature), OpType.CONTINUOUS, DataType.DOUBLE, expression);
return new ContinuousFeature(PreProcessEncoder.this, derivedField);
}
};
return schema.toTransformedSchema(function);
}
use of org.dmg.pmml.DerivedField in project jpmml-sparkml by jpmml.
the class TermFeature method toContinuousFeature.
@Override
public ContinuousFeature toContinuousFeature() {
PMMLEncoder encoder = ensureEncoder();
DerivedField derivedField = encoder.getDerivedField(getName());
if (derivedField == null) {
Apply apply = createApply();
derivedField = encoder.createDerivedField(getName(), OpType.CONTINUOUS, getDataType(), apply);
}
return new ContinuousFeature(encoder, derivedField);
}
use of org.dmg.pmml.DerivedField 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