use of org.jpmml.converter.Feature in project jpmml-r by jpmml.
the class RExpEncoder method addFeature.
public void addFeature(Field<?> field) {
Feature feature;
OpType opType = field.requireOpType();
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.Feature in project jpmml-r by jpmml.
the class SVMConverter method scaleFeatures.
private void scaleFeatures(RExpEncoder encoder) {
RGenericVector svm = getObject();
RDoubleVector sv = svm.getDoubleElement("SV");
RBooleanVector scaled = svm.getBooleanElement("scaled");
RGenericVector xScale = svm.getGenericElement("x.scale");
RStringVector rowNames = sv.dimnames(0);
RStringVector columnNames = sv.dimnames(1);
List<Feature> features = encoder.getFeatures();
if ((scaled.size() != columnNames.size()) || (scaled.size() != features.size())) {
throw new IllegalArgumentException();
}
RDoubleVector xScaledCenter = xScale.getDoubleElement("scaled:center");
RDoubleVector xScaledScale = xScale.getDoubleElement("scaled:scale");
for (int i = 0; i < columnNames.size(); i++) {
String columnName = columnNames.getValue(i);
if (!scaled.getValue(i)) {
continue;
}
Feature feature = features.get(i);
Double center = xScaledCenter.getElement(columnName);
Double scale = xScaledScale.getElement(columnName);
if (ValueUtil.isZero(center) && ValueUtil.isOne(scale)) {
continue;
}
ContinuousFeature continuousFeature = feature.toContinuousFeature();
Expression expression = continuousFeature.ref();
if (!ValueUtil.isZero(center)) {
expression = PMMLUtil.createApply(PMMLFunctions.SUBTRACT, expression, PMMLUtil.createConstant(center));
}
if (!ValueUtil.isOne(scale)) {
expression = PMMLUtil.createApply(PMMLFunctions.DIVIDE, expression, PMMLUtil.createConstant(scale));
}
DerivedField derivedField = encoder.createDerivedField(FieldNameUtil.create("scale", continuousFeature), OpType.CONTINUOUS, DataType.DOUBLE, expression);
features.set(i, new ContinuousFeature(encoder, derivedField));
}
}
use of org.jpmml.converter.Feature in project jpmml-r by jpmml.
the class MVRConverter method scaleFeatures.
private void scaleFeatures(RExpEncoder encoder) {
RGenericVector mvr = getObject();
RDoubleVector scale = mvr.getDoubleElement("scale", false);
if (scale == null) {
return;
}
List<Feature> features = encoder.getFeatures();
if (scale.size() != features.size()) {
throw new IllegalArgumentException();
}
for (int i = 0; i < features.size(); i++) {
Feature feature = features.get(i);
Double factor = scale.getValue(i);
if (ValueUtil.isOne(factor)) {
continue;
}
ContinuousFeature continuousFeature = feature.toContinuousFeature();
Apply apply = PMMLUtil.createApply(PMMLFunctions.DIVIDE, continuousFeature.ref(), PMMLUtil.createConstant(factor));
DerivedField derivedField = encoder.createDerivedField(FieldNameUtil.create("scale", continuousFeature), OpType.CONTINUOUS, DataType.DOUBLE, apply);
features.set(i, new ContinuousFeature(encoder, derivedField));
}
}
use of org.jpmml.converter.Feature in project jpmml-r by jpmml.
the class RecipeEncoder method createSchema.
@Override
public Schema createSchema() {
RGenericVector recipe = getObject();
Label label = getLabel();
List<? extends Feature> features = getFeatures();
RGenericVector steps = recipe.getGenericElement("steps");
List<String> outcomeNames = this.termRoles.entrySet().stream().filter(entry -> (entry.getValue() == Role.OUTCOME)).map(entry -> entry.getKey()).collect(Collectors.toList());
if (outcomeNames.size() == 1) {
String outcomeName = outcomeNames.get(0);
renameDataField(label.getName(), outcomeName);
label = label.toRenamedLabel(outcomeName);
} else if (outcomeNames.size() >= 2) {
throw new IllegalArgumentException();
}
if (steps != null) {
throw new IllegalArgumentException();
}
return new Schema(this, label, features);
}
use of org.jpmml.converter.Feature in project jpmml-r by jpmml.
the class ScorecardConverter method encodeModel.
@Override
public Scorecard encodeModel(Schema schema) {
RGenericVector glm = getObject();
RDoubleVector coefficients = glm.getDoubleElement("coefficients");
RGenericVector family = glm.getGenericElement("family");
RGenericVector scConf = DecorationUtil.getGenericElement(glm, "sc.conf");
Double intercept = coefficients.getElement(LMConverter.INTERCEPT, false);
List<? extends Feature> features = schema.getFeatures();
SchemaUtil.checkSize(coefficients.size() - (intercept != null ? 1 : 0), features);
RNumberVector<?> odds = scConf.getNumericElement("odds");
RNumberVector<?> basePoints = scConf.getNumericElement("base_points");
RNumberVector<?> pdo = scConf.getNumericElement("pdo");
double factor = (pdo.asScalar()).doubleValue() / Math.log(2);
Map<String, Characteristic> fieldCharacteristics = new LinkedHashMap<>();
for (Feature feature : features) {
String name = feature.getName();
if (!(feature instanceof BinaryFeature)) {
throw new IllegalArgumentException();
}
Double coefficient = getFeatureCoefficient(feature, coefficients);
Characteristic characteristic = fieldCharacteristics.get(name);
if (characteristic == null) {
characteristic = new Characteristic().setName("score(" + FeatureUtil.getName(feature) + ")");
fieldCharacteristics.put(name, characteristic);
}
BinaryFeature binaryFeature = (BinaryFeature) feature;
SimplePredicate simplePredicate = new SimplePredicate(binaryFeature.getName(), SimplePredicate.Operator.EQUAL, binaryFeature.getValue());
Attribute attribute = new Attribute(simplePredicate).setPartialScore(formatScore(-1d * coefficient * factor));
characteristic.addAttributes(attribute);
}
Characteristics characteristics = new Characteristics();
Collection<Map.Entry<String, Characteristic>> entries = fieldCharacteristics.entrySet();
for (Map.Entry<String, Characteristic> entry : entries) {
Characteristic characteristic = entry.getValue();
Attribute attribute = new Attribute(True.INSTANCE).setPartialScore(0d);
characteristic.addAttributes(attribute);
characteristics.addCharacteristics(characteristic);
}
Scorecard scorecard = new Scorecard(MiningFunction.REGRESSION, ModelUtil.createMiningSchema(schema.getLabel()), characteristics).setInitialScore(formatScore((basePoints.asScalar()).doubleValue() - Math.log((odds.asScalar()).doubleValue()) * factor - (intercept != null ? intercept * factor : 0))).setUseReasonCodes(false);
return scorecard;
}
Aggregations