Search in sources :

Example 1 with Scorecard

use of org.dmg.pmml.scorecard.Scorecard 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;
}
Also used : Attribute(org.dmg.pmml.scorecard.Attribute) Characteristic(org.dmg.pmml.scorecard.Characteristic) True(org.dmg.pmml.True) BinaryFeature(org.jpmml.converter.BinaryFeature) Feature(org.jpmml.converter.Feature) BinaryFeature(org.jpmml.converter.BinaryFeature) SimplePredicate(org.dmg.pmml.SimplePredicate) LinkedHashMap(java.util.LinkedHashMap) Characteristics(org.dmg.pmml.scorecard.Characteristics) Scorecard(org.dmg.pmml.scorecard.Scorecard) FieldName(org.dmg.pmml.FieldName) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 FieldName (org.dmg.pmml.FieldName)1 SimplePredicate (org.dmg.pmml.SimplePredicate)1 True (org.dmg.pmml.True)1 Attribute (org.dmg.pmml.scorecard.Attribute)1 Characteristic (org.dmg.pmml.scorecard.Characteristic)1 Characteristics (org.dmg.pmml.scorecard.Characteristics)1 Scorecard (org.dmg.pmml.scorecard.Scorecard)1 BinaryFeature (org.jpmml.converter.BinaryFeature)1 Feature (org.jpmml.converter.Feature)1