Search in sources :

Example 1 with DerivedField

use of org.dmg.pmml.DerivedFieldDocument.DerivedField in project knime-core by knime.

the class PMMLNeuralNetworkTranslator method addInputLayer.

/**
 * Writes the PMML input layer of the MLP.
 *
 * @param nnModel
 *            the Neural Network model.
 * @param mlp
 *            the underlying {@link MultiLayerPerceptron}.
 */
protected void addInputLayer(final NeuralNetwork nnModel, final MultiLayerPerceptron mlp) {
    Layer inputlayer = mlp.getLayer(0);
    Perceptron[] inputperceptrons = inputlayer.getPerceptrons();
    HashMap<String, Integer> inputmap = mlp.getInputMapping();
    NeuralInputs neuralInputs = nnModel.addNewNeuralInputs();
    neuralInputs.setNumberOfInputs(BigInteger.valueOf(inputperceptrons.length));
    for (int i = 0; i < inputperceptrons.length; i++) {
        NeuralInput neuralInput = neuralInputs.addNewNeuralInput();
        neuralInput.setId(0 + "," + i);
        // search corresponding input column
        String colname = "";
        for (Entry<String, Integer> e : inputmap.entrySet()) {
            if (e.getValue().equals(i)) {
                colname = e.getKey();
            }
        }
        DerivedField df = neuralInput.addNewDerivedField();
        df.setOptype(OPTYPE.CONTINUOUS);
        df.setDataType(DATATYPE.DOUBLE);
        FieldRef fieldRef = df.addNewFieldRef();
        fieldRef.setField(m_nameMapper.getDerivedFieldName(colname));
    }
}
Also used : BigInteger(java.math.BigInteger) SigmoidPerceptron(org.knime.base.data.neural.SigmoidPerceptron) MultiLayerPerceptron(org.knime.base.data.neural.MultiLayerPerceptron) Perceptron(org.knime.base.data.neural.Perceptron) InputPerceptron(org.knime.base.data.neural.InputPerceptron) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) NeuralInputs(org.dmg.pmml.NeuralInputsDocument.NeuralInputs) NeuralLayer(org.dmg.pmml.NeuralLayerDocument.NeuralLayer) Layer(org.knime.base.data.neural.Layer) InputLayer(org.knime.base.data.neural.InputLayer) HiddenLayer(org.knime.base.data.neural.HiddenLayer) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) NeuralInput(org.dmg.pmml.NeuralInputDocument.NeuralInput)

Example 2 with DerivedField

use of org.dmg.pmml.DerivedFieldDocument.DerivedField in project knime-core by knime.

the class PMMLOne2ManyTranslator method createDerivedFields.

private DerivedField[] createDerivedFields() {
    List<DerivedField> derivedFields = new ArrayList<DerivedField>();
    for (Map.Entry<String, List<Pair<String, String>>> entry : m_columnMapping.entrySet()) {
        String columnName = entry.getKey();
        String derivedName = m_mapper.getDerivedFieldName(columnName);
        for (Pair<String, String> nameValue : entry.getValue()) {
            DerivedField derivedField = DerivedField.Factory.newInstance();
            derivedField.setName(nameValue.getFirst());
            derivedField.setOptype(OPTYPE.ORDINAL);
            derivedField.setDataType(DATATYPE.INTEGER);
            NormDiscrete normDiscrete = derivedField.addNewNormDiscrete();
            normDiscrete.setField(derivedName);
            normDiscrete.setValue(nameValue.getSecond());
            normDiscrete.setMapMissingTo(0);
            derivedFields.add(derivedField);
        }
    }
    return derivedFields.toArray(new DerivedField[0]);
}
Also used : NormDiscrete(org.dmg.pmml.NormDiscreteDocument.NormDiscrete) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) Map(java.util.Map)

Example 3 with DerivedField

use of org.dmg.pmml.DerivedFieldDocument.DerivedField in project knime-core by knime.

the class PMMLMany2OneTranslator method createDerivedField.

private DerivedField createDerivedField() {
    final DerivedField derivedField = DerivedField.Factory.newInstance();
    derivedField.setName(m_appendedCol);
    derivedField.setDataType(DATATYPE.STRING);
    derivedField.setOptype(OPTYPE.CATEGORICAL);
    Apply parentApply = null;
    for (String col : m_sourceCols) {
        Apply ifApply;
        if (parentApply == null) {
            ifApply = derivedField.addNewApply();
        } else {
            ifApply = parentApply.addNewApply();
        }
        ifApply.setFunction("if");
        Apply innerIf = ifApply.addNewApply();
        innerIf.setFunction("equal");
        innerIf.addNewFieldRef().setField(col);
        if (m_method == IncludeMethod.Maximum || m_method == IncludeMethod.Minimum) {
            Apply a = innerIf.addNewApply();
            a.setFunction(IncludeMethod.Maximum == m_method ? "max" : "min");
            for (String s : m_sourceCols) {
                a.addNewFieldRef().setField(s);
            }
        } else {
            // if (m_method == IncludeMethod.Binary) {
            innerIf.addNewConstant().setStringValue("1");
        }
        ifApply.addNewConstant().setStringValue(col);
        parentApply = ifApply;
    }
    if (parentApply != null) {
        parentApply.addNewConstant().setStringValue("missing");
    }
    return derivedField;
}
Also used : Apply(org.dmg.pmml.ApplyDocument.Apply) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField)

Example 4 with DerivedField

use of org.dmg.pmml.DerivedFieldDocument.DerivedField in project knime-core by knime.

the class PMMLGeneralRegressionTranslator method updateVectorLengthsBasedOnDerivedFields.

/**
 * @param derivedFieldList
 */
private void updateVectorLengthsBasedOnDerivedFields(final List<DerivedField> derivedFieldList) {
    final Map<String, Integer> lengths = new LinkedHashMap<>();
    for (final DerivedField df : derivedFieldList) {
        final String name = df.getName();
        Optional<NameAndIndex> vni = VectorHandling.parse(name);
        if (vni.isPresent()) {
            final String key = vni.get().getName();
            try {
                String function = df.getApply().getFunction();
                if (!"substring".equals(function)) {
                    continue;
                }
                final List<FieldRef> fieldRefList = df.getApply().getFieldRefList();
                if (fieldRefList.isEmpty() || !key.equals(fieldRefList.get(0).getField())) {
                    LOGGER.debug("Field name is not related to the derived field name: " + fieldRefList + " <-> " + key);
                    continue;
                }
                if (2 != df.getApply().getConstantList().size()) {
                    LOGGER.debug("substring requires two parameters: " + df);
                    continue;
                }
                if (!DATATYPE.INTEGER.equals(df.getDataType())) {
                    LOGGER.debug("Array value should be integer: " + df);
                    continue;
                }
                if (!OPTYPE.CONTINUOUS.equals(df.getOptype())) {
                    LOGGER.debug("The optype should be continuous: " + df);
                    continue;
                }
                int index = vni.get().getIndex();
                int old = Math.max(0, lengths.getOrDefault(key, Integer.valueOf(0)).intValue());
                if (old <= index) {
                    lengths.put(key, index + 1);
                }
            } catch (RuntimeException e) {
                // Ignore
                LOGGER.debug(df.toString(), e);
            }
        }
    }
    LOGGER.debug(lengths);
    m_content.updateVectorLengths(lengths);
}
Also used : BigInteger(java.math.BigInteger) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) NameAndIndex(org.knime.base.node.mine.regression.pmmlgreg.VectorHandling.NameAndIndex) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with DerivedField

use of org.dmg.pmml.DerivedFieldDocument.DerivedField in project knime-core by knime.

the class PMMLNeuralNetworkTranslator method addInputLayer.

/**
 * Writes the PMML input layer of the MLP.
 *
 * @param nnModel
 *            the Neural Network model.
 * @param mlp
 *            the underlying {@link MultiLayerPerceptron}.
 */
protected void addInputLayer(final NeuralNetwork nnModel, final MultiLayerPerceptron mlp) {
    Layer inputlayer = mlp.getLayer(0);
    Perceptron[] inputperceptrons = inputlayer.getPerceptrons();
    HashMap<String, Integer> inputmap = mlp.getInputMapping();
    NeuralInputs neuralInputs = nnModel.addNewNeuralInputs();
    neuralInputs.setNumberOfInputs(BigInteger.valueOf(inputperceptrons.length));
    for (int i = 0; i < inputperceptrons.length; i++) {
        NeuralInput neuralInput = neuralInputs.addNewNeuralInput();
        neuralInput.setId(0 + "," + i);
        // search corresponding input column
        String colname = "";
        for (Entry<String, Integer> e : inputmap.entrySet()) {
            if (e.getValue().equals(i)) {
                colname = e.getKey();
            }
        }
        DerivedField df = neuralInput.addNewDerivedField();
        df.setOptype(OPTYPE.CONTINUOUS);
        df.setDataType(DATATYPE.DOUBLE);
        FieldRef fieldRef = df.addNewFieldRef();
        fieldRef.setField(m_nameMapper.getDerivedFieldName(colname));
    }
}
Also used : BigInteger(java.math.BigInteger) SigmoidPerceptron(org.knime.base.data.neural.SigmoidPerceptron) MultiLayerPerceptron(org.knime.base.data.neural.MultiLayerPerceptron) Perceptron(org.knime.base.data.neural.Perceptron) InputPerceptron(org.knime.base.data.neural.InputPerceptron) FieldRef(org.dmg.pmml.FieldRefDocument.FieldRef) NeuralInputs(org.dmg.pmml.NeuralInputsDocument.NeuralInputs) NeuralLayer(org.dmg.pmml.NeuralLayerDocument.NeuralLayer) Layer(org.knime.base.data.neural.Layer) InputLayer(org.knime.base.data.neural.InputLayer) HiddenLayer(org.knime.base.data.neural.HiddenLayer) DerivedField(org.dmg.pmml.DerivedFieldDocument.DerivedField) NeuralInput(org.dmg.pmml.NeuralInputDocument.NeuralInput)

Aggregations

DerivedField (org.dmg.pmml.DerivedFieldDocument.DerivedField)41 ArrayList (java.util.ArrayList)12 FieldRef (org.dmg.pmml.FieldRefDocument.FieldRef)11 BigInteger (java.math.BigInteger)9 DerivedFieldMapper (org.knime.core.node.port.pmml.preproc.DerivedFieldMapper)8 MapValues (org.dmg.pmml.MapValuesDocument.MapValues)7 DataColumnSpec (org.knime.core.data.DataColumnSpec)6 Apply (org.dmg.pmml.ApplyDocument.Apply)5 DiscretizeBin (org.dmg.pmml.DiscretizeBinDocument.DiscretizeBin)5 Discretize (org.dmg.pmml.DiscretizeDocument.Discretize)5 Interval (org.dmg.pmml.IntervalDocument.Interval)5 NormDiscrete (org.dmg.pmml.NormDiscreteDocument.NormDiscrete)5 DataCell (org.knime.core.data.DataCell)5 DataType (org.knime.core.data.DataType)5 LinkedHashMap (java.util.LinkedHashMap)4 LinkedHashSet (java.util.LinkedHashSet)4 Map (java.util.Map)4 LocalTransformations (org.dmg.pmml.LocalTransformationsDocument.LocalTransformations)4 NeuralLayer (org.dmg.pmml.NeuralLayerDocument.NeuralLayer)4 NeuralOutput (org.dmg.pmml.NeuralOutputDocument.NeuralOutput)4