Search in sources :

Example 61 with PMMLPortObject

use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.

the class LinReg2Learner method execute.

/**
 * Compute linear regression model.
 *
 * @param portObjects The input objects.
 * @param exec the execution context
 * @return a {@link LinearRegressionContent} storing computed data
 * @throws Exception if computation of the linear regression model is
 * not successful or if given data is inconsistent with the settings
 * defined in the constructor.
 * @see LinReg2LearnerNodeModel#execute(PortObject[], ExecutionContext)
 */
public LinearRegressionContent execute(final PortObject[] portObjects, final ExecutionContext exec) throws Exception {
    BufferedDataTable data = (BufferedDataTable) portObjects[0];
    PMMLPortObject inPMMLPort = (PMMLPortObject) portObjects[1];
    PMMLPortObjectSpec inPMMLSpec = inPMMLPort.getSpec();
    init(data.getDataTableSpec(), inPMMLSpec, Collections.<String>emptySet());
    double calcDomainTime = 0.2;
    exec.setMessage("Analyzing categorical data");
    BufferedDataTable dataTable = recalcDomainOfLearningFields(data, inPMMLSpec, exec.createSubExecutionContext(calcDomainTime));
    exec.setMessage("Computing linear regression model");
    return m_learner.perform(dataTable, exec.createSubExecutionContext(1.0 - calcDomainTime));
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 62 with PMMLPortObject

use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.

the class PolyRegLearnerNodeModel method createPMMLModel.

private PMMLPortObject createPMMLModel(final PMMLPortObject inPMMLPort, final DataTableSpec inSpec) throws InvalidSettingsException, SAXException {
    NumericPredictor[] preds = new NumericPredictor[m_betas.length - 1];
    int deg = m_settings.getDegree();
    for (int i = 0; i < m_columnNames.length; i++) {
        for (int k = 0; k < deg; k++) {
            preds[i * deg + k] = new NumericPredictor(m_columnNames[i], k + 1, m_betas[i * deg + k + 1]);
        }
    }
    RegressionTable tab = new RegressionTable(m_betas[0], preds);
    PMMLPortObjectSpec pmmlSpec = null;
    if (inPMMLPort != null) {
        pmmlSpec = inPMMLPort.getSpec();
    }
    PMMLPortObjectSpec spec = createModelSpec(pmmlSpec, inSpec);
    /* To maintain compatibility with the previous SAX-based implementation.
         * */
    String targetField = "Response";
    List<String> targetFields = spec.getTargetFields();
    if (!targetFields.isEmpty()) {
        targetField = targetFields.get(0);
    }
    PMMLPortObject outPMMLPort = new PMMLPortObject(spec, inPMMLPort, inSpec);
    PMMLRegressionTranslator trans = new PMMLRegressionTranslator("KNIME Polynomial Regression", "PolynomialRegression", tab, targetField);
    outPMMLPort.addModelTranslater(trans);
    return outPMMLPort;
}
Also used : PMMLRegressionTranslator(org.knime.base.node.mine.regression.PMMLRegressionTranslator) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) NumericPredictor(org.knime.base.node.mine.regression.PMMLRegressionTranslator.NumericPredictor) RegressionTable(org.knime.base.node.mine.regression.PMMLRegressionTranslator.RegressionTable)

Example 63 with PMMLPortObject

use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.

the class SVMPredictorNodeModel method createStreamableOperator.

/**
 * {@inheritDoc}
 */
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    return new StreamableOperator() {

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            PMMLPortObject pmmlModel = (PMMLPortObject) ((PortObjectInput) inputs[0]).getPortObject();
            ColumnRearranger colre = createColumnRearranger(pmmlModel, (DataTableSpec) inSpecs[1]);
            StreamableFunction func = colre.createStreamableFunction(1, 0);
            func.runFinal(inputs, outputs, exec);
        }
    };
}
Also used : ExecutionContext(org.knime.core.node.ExecutionContext) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) StreamableFunction(org.knime.core.node.streamable.StreamableFunction)

Example 64 with PMMLPortObject

use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.

the class SVMPredictorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
public PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    PMMLPortObject port = (PMMLPortObject) inData[0];
    BufferedDataTable testData = (BufferedDataTable) inData[1];
    ColumnRearranger colre = createColumnRearranger(port, testData.getDataTableSpec());
    BufferedDataTable result = exec.createColumnRearrangeTable(testData, colre, exec);
    return new BufferedDataTable[] { result };
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 65 with PMMLPortObject

use of org.knime.core.node.port.pmml.PMMLPortObject in project knime-core by knime.

the class RPropNodeModel method execute.

/**
 * The execution consists of three steps:
 * <ol>
 * <li>A neural network is build with the inputs and outputs according to
 * the input datatable, number of hidden layers as specified.</li>
 * <li>Input DataTables are converted into double-arrays so they can be
 * attached to the neural net.</li>
 * <li>The neural net is trained.</li>
 * </ol>
 *
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    // If class column is not set, it is the last column.
    DataTableSpec posSpec = (DataTableSpec) inData[INDATA].getSpec();
    if (m_classcol.getStringValue() == null) {
        m_classcol.setStringValue(posSpec.getColumnSpec(posSpec.getNumColumns() - 1).getName());
    }
    List<String> learningCols = new LinkedList<String>();
    List<String> targetCols = new LinkedList<String>();
    // Determine the number of inputs and the number of outputs. Make also
    // sure that the inputs are double values.
    int nrInputs = 0;
    int nrOutputs = 0;
    HashMap<String, Integer> inputmap = new HashMap<String, Integer>();
    HashMap<DataCell, Integer> classMap = new HashMap<DataCell, Integer>();
    for (DataColumnSpec colspec : posSpec) {
        // check for class column
        if (colspec.getName().toString().compareTo(m_classcol.getStringValue()) == 0) {
            targetCols.add(colspec.getName());
            if (colspec.getType().isCompatible(DoubleValue.class)) {
                // check if the values are in range [0,1]
                DataColumnDomain domain = colspec.getDomain();
                if (domain.hasBounds()) {
                    double lower = ((DoubleValue) domain.getLowerBound()).getDoubleValue();
                    double upper = ((DoubleValue) domain.getUpperBound()).getDoubleValue();
                    if (lower < 0 || upper > 1) {
                        throw new InvalidSettingsException("Domain range for regression in column " + colspec.getName() + " not in range [0,1]");
                    }
                }
                nrOutputs = 1;
                classMap = new HashMap<DataCell, Integer>();
                classMap.put(new StringCell(colspec.getName()), 0);
                m_regression = true;
            } else {
                m_regression = false;
                DataColumnDomain domain = colspec.getDomain();
                if (domain.hasValues()) {
                    Set<DataCell> allvalues = domain.getValues();
                    int outputneuron = 0;
                    classMap = new HashMap<DataCell, Integer>();
                    for (DataCell value : allvalues) {
                        classMap.put(value, outputneuron);
                        outputneuron++;
                    }
                    nrOutputs = allvalues.size();
                } else {
                    throw new Exception("Could not find domain values in" + "nominal column " + colspec.getName().toString());
                }
            }
        } else {
            if (!colspec.getType().isCompatible(DoubleValue.class)) {
                throw new Exception("Only double columns for input");
            }
            inputmap.put(colspec.getName(), nrInputs);
            learningCols.add(colspec.getName());
            nrInputs++;
        }
    }
    assert targetCols.size() == 1 : "Only one class column allowed.";
    m_architecture.setNrInputNeurons(nrInputs);
    m_architecture.setNrHiddenLayers(m_nrHiddenLayers.getIntValue());
    m_architecture.setNrHiddenNeurons(m_nrHiddenNeuronsperLayer.getIntValue());
    m_architecture.setNrOutputNeurons(nrOutputs);
    Random random = new Random();
    if (m_useRandomSeed.getBooleanValue()) {
        random.setSeed(m_randomSeed.getIntValue());
    }
    m_mlp = new MultiLayerPerceptron(m_architecture, random);
    if (m_regression) {
        m_mlp.setMode(MultiLayerPerceptron.REGRESSION_MODE);
    } else {
        m_mlp.setMode(MultiLayerPerceptron.CLASSIFICATION_MODE);
    }
    // Convert inputs to double arrays. Values from the class column are
    // encoded as bitvectors.
    int classColNr = posSpec.findColumnIndex(m_classcol.getStringValue());
    List<Double[]> samples = new ArrayList<Double[]>();
    List<Double[]> outputs = new ArrayList<Double[]>();
    Double[] sample = new Double[nrInputs];
    Double[] output = new Double[nrOutputs];
    final RowIterator rowIt = ((BufferedDataTable) inData[INDATA]).iterator();
    int rowcounter = 0;
    while (rowIt.hasNext()) {
        boolean add = true;
        output = new Double[nrOutputs];
        sample = new Double[nrInputs];
        DataRow row = rowIt.next();
        int nrCells = row.getNumCells();
        int index = 0;
        for (int i = 0; i < nrCells; i++) {
            if (i != classColNr) {
                if (!row.getCell(i).isMissing()) {
                    DoubleValue dc = (DoubleValue) row.getCell(i);
                    sample[index] = dc.getDoubleValue();
                    index++;
                } else {
                    if (m_ignoreMV.getBooleanValue()) {
                        add = false;
                        break;
                    } else {
                        throw new Exception("Missing values in input" + " datatable");
                    }
                }
            } else {
                if (row.getCell(i).isMissing()) {
                    add = false;
                    if (!m_ignoreMV.getBooleanValue()) {
                        throw new Exception("Missing value in class" + " column");
                    }
                    break;
                }
                if (m_regression) {
                    DoubleValue dc = (DoubleValue) row.getCell(i);
                    output[0] = dc.getDoubleValue();
                } else {
                    for (int j = 0; j < nrOutputs; j++) {
                        if (classMap.get(row.getCell(i)) == j) {
                            output[j] = new Double(1.0);
                        } else {
                            output[j] = new Double(0.0);
                        }
                    }
                }
            }
        }
        if (add) {
            samples.add(sample);
            outputs.add(output);
            rowcounter++;
        }
    }
    Double[][] samplesarr = new Double[rowcounter][nrInputs];
    Double[][] outputsarr = new Double[rowcounter][nrInputs];
    for (int i = 0; i < samplesarr.length; i++) {
        samplesarr[i] = samples.get(i);
        outputsarr[i] = outputs.get(i);
    }
    // Now finally train the network.
    m_mlp.setClassMapping(classMap);
    m_mlp.setInputMapping(inputmap);
    RProp myrprop = new RProp();
    m_errors = new double[m_nrIterations.getIntValue()];
    for (int iteration = 0; iteration < m_nrIterations.getIntValue(); iteration++) {
        exec.setProgress((double) iteration / (double) m_nrIterations.getIntValue(), "Iteration " + iteration);
        myrprop.train(m_mlp, samplesarr, outputsarr);
        double error = 0;
        for (int j = 0; j < outputsarr.length; j++) {
            double[] myoutput = m_mlp.output(samplesarr[j]);
            for (int o = 0; o < outputsarr[0].length; o++) {
                error += (myoutput[o] - outputsarr[j][o]) * (myoutput[o] - outputsarr[j][o]);
            }
        }
        m_errors[iteration] = error;
        exec.checkCanceled();
    }
    // handle the optional PMML input
    PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[INMODEL] : null;
    PMMLPortObjectSpec inPMMLSpec = null;
    if (inPMMLPort != null) {
        inPMMLSpec = inPMMLPort.getSpec();
    }
    PMMLPortObjectSpec outPortSpec = createPMMLPortObjectSpec(inPMMLSpec, posSpec, learningCols, targetCols);
    PMMLPortObject outPMMLPort = new PMMLPortObject(outPortSpec, inPMMLPort, posSpec);
    outPMMLPort.addModelTranslater(new PMMLNeuralNetworkTranslator(m_mlp));
    return new PortObject[] { outPMMLPort };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) HashMap(java.util.HashMap) PMMLNeuralNetworkTranslator(org.knime.base.node.mine.neural.mlp2.PMMLNeuralNetworkTranslator) ArrayList(java.util.ArrayList) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) Random(java.util.Random) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PortObject(org.knime.core.node.port.PortObject) LinkedList(java.util.LinkedList) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) IOException(java.io.IOException) MultiLayerPerceptron(org.knime.base.data.neural.MultiLayerPerceptron) SettingsModelInteger(org.knime.core.node.defaultnodesettings.SettingsModelInteger) DataColumnDomain(org.knime.core.data.DataColumnDomain) DoubleValue(org.knime.core.data.DoubleValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) RProp(org.knime.base.data.neural.methods.RProp)

Aggregations

PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)90 BufferedDataTable (org.knime.core.node.BufferedDataTable)53 DataTableSpec (org.knime.core.data.DataTableSpec)50 PortObject (org.knime.core.node.port.PortObject)47 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)35 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)25 PMMLPortObjectSpecCreator (org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)21 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)18 ExecutionContext (org.knime.core.node.ExecutionContext)13 DataColumnSpec (org.knime.core.data.DataColumnSpec)12 DataRow (org.knime.core.data.DataRow)12 DerivedFieldMapper (org.knime.core.node.port.pmml.preproc.DerivedFieldMapper)11 StreamableOperator (org.knime.core.node.streamable.StreamableOperator)11 Node (org.w3c.dom.Node)10 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)9 IOException (java.io.IOException)8 DataCell (org.knime.core.data.DataCell)8 PMMLDocument (org.dmg.pmml.PMMLDocument)7 PMMLGeneralRegressionTranslator (org.knime.base.node.mine.regression.pmmlgreg.PMMLGeneralRegressionTranslator)7 StreamableFunction (org.knime.core.node.streamable.StreamableFunction)7