Search in sources :

Example 1 with PMMLDiscretizeBin

use of org.knime.core.util.binning.auto.pmml.PMMLDiscretizeBin in project knime-core by knime.

the class AutoBinnerUtility method translate.

/**
 * This method translates a {@link PMMLPreprocDiscretize} object into {@link PMMLPortObject}.
 *
 * @param pmmlDiscretize {@link PMMLPreprocDiscretize} object
 * @param dataTableSpec {@link DataTableSpec} if incoming {@link BufferedDataTable}
 * @return a {@link PMMLPortObject} containing required parameters for binning operation
 */
public static PMMLPortObject translate(final PMMLPreprocDiscretize pmmlDiscretize, final DataTableSpec dataTableSpec) {
    final Map<String, Bin[]> columnToBins = new HashMap<>();
    final Map<String, String> columnToAppend = new HashMap<>();
    final List<String> replacedColumnNames = pmmlDiscretize.getConfiguration().getNames();
    for (String replacedColumnName : replacedColumnNames) {
        final PMMLDiscretize discretize = pmmlDiscretize.getConfiguration().getDiscretize(replacedColumnName);
        final List<PMMLDiscretizeBin> bins = discretize.getBins();
        final String originalColumnName = discretize.getField();
        final boolean replaceColumnTheSame = replacedColumnName.equals(originalColumnName);
        columnToAppend.put(originalColumnName, replaceColumnTheSame ? null : replacedColumnName);
        final NumericBin[] numericBin = bins.stream().map(AutoBinnerUtility::getNumericBin).toArray(NumericBin[]::new);
        columnToBins.put(originalColumnName, numericBin);
    }
    final DataTableSpec newDataTableSpec = createNewDataTableSpec(dataTableSpec, columnToAppend);
    final PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(newDataTableSpec);
    final PMMLPortObject pmmlPortObject = new PMMLPortObject(pmmlSpecCreator.createSpec(), null, newDataTableSpec);
    final PMMLBinningTranslator trans = new PMMLBinningTranslator(columnToBins, columnToAppend, new DerivedFieldMapper(pmmlPortObject));
    final TransformationDictionary exportToTransDict = trans.exportToTransDict();
    pmmlPortObject.addGlobalTransformations(exportToTransDict);
    return pmmlPortObject;
}
Also used : NumericBin(org.knime.core.util.binning.numeric.NumericBin) DataTableSpec(org.knime.core.data.DataTableSpec) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TransformationDictionary(org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary) PMMLDiscretizeBin(org.knime.core.util.binning.auto.pmml.PMMLDiscretizeBin) DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLBinningTranslator(org.knime.core.util.binning.numeric.PMMLBinningTranslator) PMMLDiscretize(org.knime.core.util.binning.auto.pmml.PMMLDiscretize) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 2 with PMMLDiscretizeBin

use of org.knime.core.util.binning.auto.pmml.PMMLDiscretizeBin in project knime-core by knime.

the class AutoBinnerUtility method createBins.

private static Map<String, List<PMMLDiscretizeBin>> createBins(final AutoBinnerLearnSettings settings, final Map<String, double[]> edgesMap, final String[] includeCols) {
    final Map<String, List<PMMLDiscretizeBin>> binMap = new HashMap<String, List<PMMLDiscretizeBin>>();
    for (final String target : includeCols) {
        if (isTargetValid(edgesMap, target)) {
            final double[] edges = edgesMap.get(target);
            final String[] binNames;
            if (BinNaming.NUMBERED == settings.getBinNaming()) {
                binNames = getNumberedBinNames(edges.length - 1);
            } else if (BinNaming.EDGES == settings.getBinNaming()) {
                binNames = getEdgedBinNames(edges, settings);
            } else {
                // BinNaming.MIDPOINTS
                binNames = getMidPointsBinNames(edges, settings);
            }
            final List<PMMLDiscretizeBin> bins = new ArrayList<PMMLDiscretizeBin>();
            bins.add(new PMMLDiscretizeBin(binNames[0], Arrays.asList(new PMMLInterval(edges[0], edges[1], Closure.CLOSED_CLOSED))));
            for (int i = 1; i < binNames.length; i++) {
                bins.add(new PMMLDiscretizeBin(binNames[i], Arrays.asList(new PMMLInterval(edges[i], edges[i + 1], Closure.OPEN_CLOSED))));
            }
            binMap.put(target, bins);
        } else {
            binMap.put(target, new ArrayList<PMMLDiscretizeBin>());
        }
    }
    return binMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) PMMLDiscretizeBin(org.knime.core.util.binning.auto.pmml.PMMLDiscretizeBin) PMMLInterval(org.knime.core.util.binning.auto.pmml.PMMLInterval)

Aggregations

HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 PMMLDiscretizeBin (org.knime.core.util.binning.auto.pmml.PMMLDiscretizeBin)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 TransformationDictionary (org.dmg.pmml.TransformationDictionaryDocument.TransformationDictionary)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)1 PMMLPortObjectSpecCreator (org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)1 DerivedFieldMapper (org.knime.core.node.port.pmml.preproc.DerivedFieldMapper)1 PMMLDiscretize (org.knime.core.util.binning.auto.pmml.PMMLDiscretize)1 PMMLInterval (org.knime.core.util.binning.auto.pmml.PMMLInterval)1 NumericBin (org.knime.core.util.binning.numeric.NumericBin)1 PMMLBinningTranslator (org.knime.core.util.binning.numeric.PMMLBinningTranslator)1