Search in sources :

Example 1 with Characteristics

use of org.dmg.pmml.scorecard.Characteristics 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)

Example 2 with Characteristics

use of org.dmg.pmml.scorecard.Characteristics in project drools by kiegroup.

the class KiePMMLScorecardModelCharacteristicASTFactoryTest method declareRulesFromCharacteristics.

@Test
public void declareRulesFromCharacteristics() {
    Characteristics characteristics = scorecardModel.getCharacteristics();
    String parentPath = "_will";
    List<KiePMMLDroolsRule> retrieved = getKiePMMLScorecardModelCharacteristicASTFactory().declareRulesFromCharacteristics(characteristics, parentPath, null);
    final List<Characteristic> characteristicList = characteristics.getCharacteristics();
    List<Attribute> attributes = new ArrayList<>();
    AtomicInteger counter = new AtomicInteger(0);
    for (int i = 0; i < characteristicList.size(); i++) {
        Characteristic characteristic = characteristicList.get(i);
        attributes.addAll(characteristic.getAttributes());
        for (int j = 0; j < characteristic.getAttributes().size(); j++) {
            Attribute attribute = characteristic.getAttributes().get(j);
            KiePMMLDroolsRule rule = retrieved.get(counter.incrementAndGet());
            int expectedOperatorValuesSize = 1;
            Integer expectedAndConstraints = null;
            Integer expectedInConstraints = null;
            BOOLEAN_OPERATOR expectedOperator = BOOLEAN_OPERATOR.AND;
            if (attribute.getPredicate() instanceof SimplePredicate) {
                expectedAndConstraints = 1;
            }
            if (attribute.getPredicate() instanceof CompoundPredicate) {
                expectedOperatorValuesSize = ((CompoundPredicate) attribute.getPredicate()).getPredicates().size();
                expectedAndConstraints = 1;
            }
            if (attribute.getPredicate() instanceof SimpleSetPredicate) {
                expectedInConstraints = 1;
            }
            boolean isLastCharacteristic = (i == characteristicList.size() - 1);
            String statusToSet = isLastCharacteristic ? DONE : String.format(PATH_PATTERN, parentPath, characteristicList.get(i + 1).getName());
            commonValidateRule(rule, attribute, statusToSet, parentPath + "_" + characteristic.getName(), j, isLastCharacteristic, expectedAndConstraints, expectedInConstraints, expectedOperator, null, expectedOperatorValuesSize);
        }
    }
    assertEquals(attributes.size() + 1, retrieved.size());
}
Also used : Attribute(org.dmg.pmml.scorecard.Attribute) Characteristic(org.dmg.pmml.scorecard.Characteristic) ArrayList(java.util.ArrayList) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) SimplePredicate(org.dmg.pmml.SimplePredicate) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Characteristics(org.dmg.pmml.scorecard.Characteristics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Test(org.junit.Test)

Example 3 with Characteristics

use of org.dmg.pmml.scorecard.Characteristics in project drools by kiegroup.

the class KiePMMLCharacteristicsFactory method getKiePMMLCharacteristicsSourcesMap.

static Map<String, String> getKiePMMLCharacteristicsSourcesMap(final ScorecardCompilationDTO compilationDTO) {
    final String characteristicsClassName = compilationDTO.getCharacteristicsClassName();
    final Characteristics characteristics = compilationDTO.getCharacteristics();
    final List<Field<?>> fields = compilationDTO.getFields();
    final String packageName = compilationDTO.getPackageName();
    CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(characteristicsClassName, packageName, KIE_PMML_CHARACTERISTICS_TEMPLATE_JAVA, KIE_PMML_CHARACTERISTICS_TEMPLATE);
    final ClassOrInterfaceDeclaration characteristicsTemplate = cloneCU.getClassByName(characteristicsClassName).orElseThrow(() -> new KiePMMLException(MAIN_CLASS_NOT_FOUND + ": " + KIE_PMML_CHARACTERISTICS_TEMPLATE));
    setCharacteristicsVariableDeclaration(characteristicsClassName, characteristics, fields, characteristicsTemplate);
    Map<String, String> toReturn = new HashMap<>();
    String fullClassName = packageName + "." + characteristicsClassName;
    toReturn.put(fullClassName, cloneCU.toString());
    return toReturn;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Field(org.dmg.pmml.Field) KiePMMLCharacteristics(org.kie.pmml.models.scorecard.model.KiePMMLCharacteristics) Characteristics(org.dmg.pmml.scorecard.Characteristics) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) HashMap(java.util.HashMap) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Aggregations

Characteristics (org.dmg.pmml.scorecard.Characteristics)3 SimplePredicate (org.dmg.pmml.SimplePredicate)2 Attribute (org.dmg.pmml.scorecard.Attribute)2 Characteristic (org.dmg.pmml.scorecard.Characteristic)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CompoundPredicate (org.dmg.pmml.CompoundPredicate)1 Field (org.dmg.pmml.Field)1 FieldName (org.dmg.pmml.FieldName)1 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicate)1 True (org.dmg.pmml.True)1 Scorecard (org.dmg.pmml.scorecard.Scorecard)1 BinaryFeature (org.jpmml.converter.BinaryFeature)1 Feature (org.jpmml.converter.Feature)1 Test (org.junit.Test)1