Search in sources :

Example 6 with DiscretizeBin

use of org.dmg.pmml.DiscretizeBin in project drools by kiegroup.

the class KiePMMLDiscretizeBinFactoryTest method getDiscretizeBinVariableDeclaration.

@Test
public void getDiscretizeBinVariableDeclaration() throws IOException {
    String variableName = "variableName";
    double leftMargin = 45.32;
    Interval interval = new Interval();
    interval.setLeftMargin(leftMargin);
    interval.setRightMargin(null);
    interval.setClosure(Interval.Closure.CLOSED_OPEN);
    String binValue = "binValue";
    DiscretizeBin discretizeBin = new DiscretizeBin();
    discretizeBin.setBinValue(binValue);
    discretizeBin.setInterval(interval);
    BlockStmt retrieved = KiePMMLDiscretizeBinFactory.getDiscretizeBinVariableDeclaration(variableName, discretizeBin);
    String closureString = CLOSURE.class.getName() + "." + CLOSURE.byName(interval.getClosure().value()).name();
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, leftMargin, closureString, binValue));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(Collections.class, KiePMMLDiscretizeBin.class, KiePMMLInterval.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLDiscretizeBin(org.kie.pmml.commons.model.expressions.KiePMMLDiscretizeBin) DiscretizeBin(org.dmg.pmml.DiscretizeBin) Interval(org.dmg.pmml.Interval) KiePMMLInterval(org.kie.pmml.commons.model.expressions.KiePMMLInterval) Test(org.junit.Test)

Example 7 with DiscretizeBin

use of org.dmg.pmml.DiscretizeBin in project drools by kiegroup.

the class KiePMMLDiscretizeFactory method getDiscretizeVariableDeclaration.

static BlockStmt getDiscretizeVariableDeclaration(final String variableName, final Discretize discretize) {
    final MethodDeclaration methodDeclaration = DISCRETIZE_TEMPLATE.getMethodsByName(GETKIEPMMLDISCRETIZE).get(0).clone();
    final BlockStmt discretizeBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(discretizeBody, DISCRETIZE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, DISCRETIZE, discretizeBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    int counter = 0;
    final NodeList<Expression> arguments = new NodeList<>();
    for (DiscretizeBin discretizeBin : discretize.getDiscretizeBins()) {
        String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
        arguments.add(new NameExpr(nestedVariableName));
        BlockStmt toAdd = getDiscretizeBinVariableDeclaration(nestedVariableName, discretizeBin);
        toAdd.getStatements().forEach(toReturn::addStatement);
        counter++;
    }
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, DISCRETIZE, toReturn))).asObjectCreationExpr();
    final Expression nameExpr = new StringLiteralExpr(discretize.getField().getValue());
    final Expression mapMissingToExpr = getExpressionForObject(discretize.getMapMissingTo());
    final Expression defaultValueExpr = getExpressionForObject(discretize.getDefaultValue());
    final Expression dataTypeExpression = getExpressionForDataType(discretize.getDataType());
    objectCreationExpr.getArguments().set(0, nameExpr);
    objectCreationExpr.getArguments().get(2).asMethodCallExpr().setArguments(arguments);
    objectCreationExpr.getArguments().set(3, mapMissingToExpr);
    objectCreationExpr.getArguments().set(4, defaultValueExpr);
    objectCreationExpr.getArguments().set(5, dataTypeExpression);
    discretizeBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) DiscretizeBin(org.dmg.pmml.DiscretizeBin)

Example 8 with DiscretizeBin

use of org.dmg.pmml.DiscretizeBin in project drools by kiegroup.

the class PMMLModelTestUtils method getRandomDiscretizeBin.

public static DiscretizeBin getRandomDiscretizeBin() {
    Interval interval = getRandomInterval();
    DiscretizeBin toReturn = new DiscretizeBin();
    toReturn.setInterval(interval);
    toReturn.setBinValue(RandomStringUtils.random(6, true, false));
    return toReturn;
}
Also used : DiscretizeBin(org.dmg.pmml.DiscretizeBin) Interval(org.dmg.pmml.Interval)

Example 9 with DiscretizeBin

use of org.dmg.pmml.DiscretizeBin in project shifu by ShifuML.

the class WoeLocalTransformCreator method createNumericalDerivedField.

/**
 * Create @DerivedField for numerical variable
 *
 * @param config
 *            - ColumnConfig for numerical variable
 * @param cutoff
 *            - cutoff of normalization
 * @param normType
 *            - the normalization method that is used to generate DerivedField
 * @return DerivedField for variable
 */
@Override
protected List<DerivedField> createNumericalDerivedField(ColumnConfig config, double cutoff, ModelNormalizeConf.NormType normType) {
    List<Double> binWoeList = (normType.equals(ModelNormalizeConf.NormType.WOE) ? config.getBinCountWoe() : config.getBinWeightedWoe());
    List<Double> binBoundaryList = config.getBinBoundary();
    List<DiscretizeBin> discretizeBinList = new ArrayList<DiscretizeBin>();
    for (int i = 0; i < binBoundaryList.size(); i++) {
        DiscretizeBin discretizeBin = new DiscretizeBin();
        Interval interval = new Interval();
        if (i == 0) {
            if (binBoundaryList.size() == 1) {
                interval.setClosure(Interval.Closure.OPEN_OPEN).setLeftMargin(Double.NEGATIVE_INFINITY).setRightMargin(Double.POSITIVE_INFINITY);
            } else {
                interval.setClosure(Interval.Closure.OPEN_OPEN).setRightMargin(binBoundaryList.get(i + 1));
            }
        } else if (i == binBoundaryList.size() - 1) {
            interval.setClosure(Interval.Closure.CLOSED_OPEN).setLeftMargin(binBoundaryList.get(i));
        } else {
            interval.setClosure(Interval.Closure.CLOSED_OPEN).setLeftMargin(binBoundaryList.get(i)).setRightMargin(binBoundaryList.get(i + 1));
        }
        discretizeBin.setInterval(interval).setBinValue(Double.toString(binWoeList.get(i)));
        discretizeBinList.add(discretizeBin);
    }
    Discretize discretize = new Discretize();
    discretize.setDataType(DataType.DOUBLE).setField(FieldName.create(NormalUtils.getSimpleColumnName(config, columnConfigList, segmentExpansions, datasetHeaders))).setMapMissingTo(Normalizer.normalize(config, null, cutoff, normType).get(0).toString()).setDefaultValue(Normalizer.normalize(config, null, cutoff, normType).get(0).toString()).addDiscretizeBins(discretizeBinList.toArray(new DiscretizeBin[discretizeBinList.size()]));
    // derived field name is consisted of FieldName and "_zscl"
    List<DerivedField> derivedFields = new ArrayList<DerivedField>();
    derivedFields.add(new DerivedField(OpType.CONTINUOUS, DataType.DOUBLE).setName(FieldName.create(genPmmlColumnName(NormalUtils.getSimpleColumnName(config.getColumnName()), normType))).setExpression(discretize));
    return derivedFields;
}
Also used : ArrayList(java.util.ArrayList) Discretize(org.dmg.pmml.Discretize) DiscretizeBin(org.dmg.pmml.DiscretizeBin) DerivedField(org.dmg.pmml.DerivedField) Interval(org.dmg.pmml.Interval)

Example 10 with DiscretizeBin

use of org.dmg.pmml.DiscretizeBin in project jpmml-r by jpmml.

the class FormulaUtil method createDiscretize.

private static Discretize createDiscretize(String name, List<String> categories) {
    Discretize discretize = new Discretize(name);
    for (String category : categories) {
        Interval interval = ExpressionTranslator.translateInterval(category);
        DiscretizeBin discretizeBin = new DiscretizeBin(category, interval);
        discretize.addDiscretizeBins(discretizeBin);
    }
    return discretize;
}
Also used : Discretize(org.dmg.pmml.Discretize) DiscretizeBin(org.dmg.pmml.DiscretizeBin) Interval(org.dmg.pmml.Interval)

Aggregations

DiscretizeBin (org.dmg.pmml.DiscretizeBin)10 Interval (org.dmg.pmml.Interval)6 Discretize (org.dmg.pmml.Discretize)5 KiePMMLDiscretizeBin (org.kie.pmml.commons.model.expressions.KiePMMLDiscretizeBin)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)3 Test (org.junit.Test)3 Statement (com.github.javaparser.ast.stmt.Statement)2 ArrayList (java.util.ArrayList)2 DerivedField (org.dmg.pmml.DerivedField)2 NodeList (com.github.javaparser.ast.NodeList)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 Expression (com.github.javaparser.ast.expr.Expression)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 Bucketizer (org.apache.spark.ml.feature.Bucketizer)1 CategoricalFeature (org.jpmml.converter.CategoricalFeature)1 ContinuousFeature (org.jpmml.converter.ContinuousFeature)1 Feature (org.jpmml.converter.Feature)1