Search in sources :

Example 1 with DiscretizeBin

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

the class FormulaUtil method createDiscretize.

private static Discretize createDiscretize(FieldName 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)

Example 2 with DiscretizeBin

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

the class BucketizerConverter method encodeFeatures.

@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder) {
    Bucketizer transformer = getTransformer();
    Feature feature = encoder.getOnlyFeature(transformer.getInputCol());
    ContinuousFeature continuousFeature = feature.toContinuousFeature();
    Discretize discretize = new Discretize(continuousFeature.getName());
    List<String> categories = new ArrayList<>();
    double[] splits = transformer.getSplits();
    for (int i = 0; i < (splits.length - 1); i++) {
        String category = String.valueOf(i);
        categories.add(category);
        Interval interval = new Interval((i < (splits.length - 2)) ? Interval.Closure.CLOSED_OPEN : Interval.Closure.CLOSED_CLOSED).setLeftMargin(formatMargin(splits[i])).setRightMargin(formatMargin(splits[i + 1]));
        DiscretizeBin discretizeBin = new DiscretizeBin(category, interval);
        discretize.addDiscretizeBins(discretizeBin);
    }
    DerivedField derivedField = encoder.createDerivedField(formatName(transformer), OpType.CATEGORICAL, DataType.INTEGER, discretize);
    return Collections.<Feature>singletonList(new CategoricalFeature(encoder, derivedField, categories));
}
Also used : ArrayList(java.util.ArrayList) Feature(org.jpmml.converter.Feature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) CategoricalFeature(org.jpmml.converter.CategoricalFeature) ContinuousFeature(org.jpmml.converter.ContinuousFeature) Discretize(org.dmg.pmml.Discretize) Bucketizer(org.apache.spark.ml.feature.Bucketizer) DiscretizeBin(org.dmg.pmml.DiscretizeBin) DerivedField(org.dmg.pmml.DerivedField) Interval(org.dmg.pmml.Interval)

Example 3 with DiscretizeBin

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

the class KiePMMLDiscretizeBinInstanceFactoryTest method getKiePMMLDiscretizeBin.

@Test
public void getKiePMMLDiscretizeBin() {
    DiscretizeBin toConvert = getRandomDiscretizeBin();
    KiePMMLDiscretizeBin retrieved = KiePMMLDiscretizeBinInstanceFactory.getKiePMMLDiscretizeBin(toConvert);
    commonVerifyKiePMMLDiscretizeBin(retrieved, toConvert);
}
Also used : KiePMMLDiscretizeBin(org.kie.pmml.commons.model.expressions.KiePMMLDiscretizeBin) InstanceFactoriesTestCommon.commonVerifyKiePMMLDiscretizeBin(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDiscretizeBin) KiePMMLDiscretizeBin(org.kie.pmml.commons.model.expressions.KiePMMLDiscretizeBin) DiscretizeBin(org.dmg.pmml.DiscretizeBin) InstanceFactoriesTestCommon.commonVerifyKiePMMLDiscretizeBin(org.kie.pmml.compiler.commons.factories.InstanceFactoriesTestCommon.commonVerifyKiePMMLDiscretizeBin) PMMLModelTestUtils.getRandomDiscretizeBin(org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomDiscretizeBin) Test(org.junit.Test)

Example 4 with DiscretizeBin

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

the class KiePMMLDiscretizeFactoryTest method getDiscretizeVariableDeclaration.

@Test
public void getDiscretizeVariableDeclaration() throws IOException {
    String variableName = "variableName";
    Discretize discretize = new Discretize();
    discretize.setField(FieldName.create(NAME));
    discretize.setDataType(dataType);
    discretize.setMapMissingTo(MAP_MISSING_TO);
    discretize.setDefaultValue(DEFAULTVALUE);
    discretize.addDiscretizeBins(discretizeBins.toArray(new DiscretizeBin[0]));
    BlockStmt retrieved = KiePMMLDiscretizeFactory.getDiscretizeVariableDeclaration(variableName, discretize);
    String dataTypeString = getDATA_TYPEString(discretize.getDataType());
    String text = getFileContent(TEST_01_SOURCE);
    Statement expected = JavaParserUtils.parseBlock(String.format(text, variableName, NAME, MAP_MISSING_TO, DEFAULTVALUE, dataTypeString));
    assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
    List<Class<?>> imports = Arrays.asList(Arrays.class, Collections.class, KiePMMLDiscretize.class, KiePMMLDiscretizeBin.class, KiePMMLInterval.class);
    commonValidateCompilationWithImports(retrieved, imports);
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLDiscretize(org.kie.pmml.commons.model.expressions.KiePMMLDiscretize) Discretize(org.dmg.pmml.Discretize) BeforeClass(org.junit.BeforeClass) CommonTestingUtils.getDATA_TYPEString(org.kie.pmml.compiler.api.CommonTestingUtils.getDATA_TYPEString) KiePMMLDiscretizeBin(org.kie.pmml.commons.model.expressions.KiePMMLDiscretizeBin) DiscretizeBin(org.dmg.pmml.DiscretizeBin) Test(org.junit.Test)

Example 5 with DiscretizeBin

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

the class KiePMMLDiscretizeFactoryTest method getDiscretizeBin.

private static DiscretizeBin getDiscretizeBin(Interval interval, String binValue) {
    DiscretizeBin toReturn = new DiscretizeBin();
    toReturn.setBinValue(binValue);
    toReturn.setInterval(interval);
    return toReturn;
}
Also used : KiePMMLDiscretizeBin(org.kie.pmml.commons.model.expressions.KiePMMLDiscretizeBin) DiscretizeBin(org.dmg.pmml.DiscretizeBin)

Aggregations

DiscretizeBin (org.dmg.pmml.DiscretizeBin)9 Interval (org.dmg.pmml.Interval)5 Discretize (org.dmg.pmml.Discretize)4 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