use of org.dmg.pmml.scorecard.Characteristic in project jpmml-r by jpmml.
the class ScorecardConverter method encodeModel.
@Override
public Scorecard encodeModel(Schema schema) {
RGenericVector glm = getObject();
RDoubleVector coefficients = (RDoubleVector) glm.getValue("coefficients");
RGenericVector family = (RGenericVector) glm.getValue("family");
RGenericVector scConf;
try {
scConf = (RGenericVector) glm.getValue("sc.conf");
} catch (IllegalArgumentException iae) {
throw new IllegalArgumentException("No scorecard configuration information. Please initialize the \'sc.conf\' element", iae);
}
Double intercept = coefficients.getValue(LMConverter.INTERCEPT, true);
List<? extends Feature> features = schema.getFeatures();
if (coefficients.size() != (features.size() + (intercept != null ? 1 : 0))) {
throw new IllegalArgumentException();
}
RNumberVector<?> odds = (RNumberVector<?>) scConf.getValue("odds");
RNumberVector<?> basePoints = (RNumberVector<?>) scConf.getValue("base_points");
RNumberVector<?> pdo = (RNumberVector<?>) scConf.getValue("pdo");
double factor = (pdo.asScalar()).doubleValue() / Math.log(2);
Map<FieldName, Characteristic> fieldCharacteristics = new LinkedHashMap<>();
for (Feature feature : features) {
FieldName 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(FeatureUtil.createName("score", feature));
fieldCharacteristics.put(name, characteristic);
}
BinaryFeature binaryFeature = (BinaryFeature) feature;
SimplePredicate simplePredicate = new SimplePredicate().setField(binaryFeature.getName()).setOperator(SimplePredicate.Operator.EQUAL).setValue(binaryFeature.getValue());
Attribute attribute = new Attribute().setPartialScore(formatScore(-1d * coefficient * factor)).setPredicate(simplePredicate);
characteristic.addAttributes(attribute);
}
Characteristics characteristics = new Characteristics();
Collection<Map.Entry<FieldName, Characteristic>> entries = fieldCharacteristics.entrySet();
for (Map.Entry<FieldName, Characteristic> entry : entries) {
Characteristic characteristic = entry.getValue();
Attribute attribute = new Attribute().setPartialScore(0d).setPredicate(new True());
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