Search in sources :

Example 16 with PMMLPortObject

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

the class LogRegLearnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    final BufferedDataTable data = (BufferedDataTable) inObjects[0];
    DataTableSpec tableSpec = data.getDataTableSpec();
    // handle the optional PMML input
    PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inObjects[1] : null;
    PMMLPortObjectSpec inPMMLSpec = null;
    if (inPMMLPort != null) {
        inPMMLSpec = inPMMLPort.getSpec();
    } else {
        PMMLPortObjectSpecCreator creator = new PMMLPortObjectSpecCreator(tableSpec);
        inPMMLSpec = creator.createSpec();
        inPMMLPort = new PMMLPortObject(inPMMLSpec);
    }
    LogRegLearner learner = new LogRegLearner(new PortObjectSpec[] { tableSpec, inPMMLSpec }, m_pmmlInEnabled, m_settings);
    m_content = learner.execute(new PortObject[] { data, inPMMLPort }, exec);
    String warn = learner.getWarningMessage();
    if (warn != null) {
        setWarningMessage(warn);
    }
    // third argument is ignored since we provide a port
    PMMLPortObject outPMMLPort = new PMMLPortObject((PMMLPortObjectSpec) learner.getOutputSpec()[0], inPMMLPort, null);
    PMMLGeneralRegressionTranslator trans = new PMMLGeneralRegressionTranslator(m_content.createGeneralRegressionContent());
    outPMMLPort.addModelTranslater(trans);
    return new PortObject[] { outPMMLPort, m_content.createTablePortObject(exec) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLGeneralRegressionTranslator(org.knime.base.node.mine.regression.pmmlgreg.PMMLGeneralRegressionTranslator) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PortObject(org.knime.core.node.port.PortObject) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 17 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 18 with PMMLPortObject

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

the class RegressionPredictorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
public PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    PMMLPortObject regModel = (PMMLPortObject) inData[0];
    List<Node> models = regModel.getPMMLValue().getModels(PMMLModelType.RegressionModel);
    if (models.isEmpty()) {
        String msg = "No Regression Model found.";
        LOGGER.error(msg);
        throw new RuntimeException(msg);
    }
    PMMLRegressionTranslator trans = new PMMLRegressionTranslator();
    regModel.initializeModelTranslator(trans);
    BufferedDataTable data = (BufferedDataTable) inData[1];
    DataTableSpec spec = data.getDataTableSpec();
    ColumnRearranger c = createRearranger(spec, regModel.getSpec(), trans);
    BufferedDataTable out = exec.createColumnRearrangeTable(data, c, exec);
    return new BufferedDataTable[] { out };
}
Also used : PMMLRegressionTranslator(org.knime.base.node.mine.regression.PMMLRegressionTranslator) DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) Node(org.w3c.dom.Node) BufferedDataTable(org.knime.core.node.BufferedDataTable)

Example 19 with PMMLPortObject

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

the class TreeEnsembleModelPortObject method createDecisionTreePMMLPortObject.

public PMMLPortObject createDecisionTreePMMLPortObject(final int modelIndex) {
    final TreeEnsembleModel ensembleModel = getEnsembleModel();
    DataTableSpec attributeLearnSpec = ensembleModel.getLearnAttributeSpec(m_spec.getLearnTableSpec());
    DataColumnSpec targetSpec = m_spec.getTargetColumn();
    PMMLPortObjectSpecCreator pmmlSpecCreator = new PMMLPortObjectSpecCreator(new DataTableSpec(attributeLearnSpec, new DataTableSpec(targetSpec)));
    try {
        pmmlSpecCreator.setLearningCols(attributeLearnSpec);
    } catch (InvalidSettingsException e) {
        // (as of KNIME v2.5.1)
        throw new IllegalStateException(e);
    }
    pmmlSpecCreator.setTargetCol(targetSpec);
    PMMLPortObjectSpec pmmlSpec = pmmlSpecCreator.createSpec();
    PMMLPortObject portObject = new PMMLPortObject(pmmlSpec);
    final AbstractTreeModel<?> model = ensembleModel.getTreeModel(modelIndex);
    portObject.addModelTranslater(new TreeModelPMMLTranslator(model));
    return portObject;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)

Example 20 with PMMLPortObject

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

the class TreeEnsembleModelExtractorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    TreeEnsembleModelPortObject treeEnsembleModel = (TreeEnsembleModelPortObject) inObjects[0];
    DataTableSpec outSpec = createOutSpec();
    BufferedDataContainer container = exec.createDataContainer(outSpec, false, 0);
    int nrModels = treeEnsembleModel.getEnsembleModel().getNrModels();
    for (int i = 0; i < nrModels; i++) {
        PMMLPortObject pmmlObject = treeEnsembleModel.createDecisionTreePMMLPortObject(i);
        DataCell cell = PMMLCellFactory.create(pmmlObject.getPMMLValue().toString());
        RowKey key = RowKey.createRowKey(i);
        container.addRowToTable(new DefaultRow(key, cell));
        exec.checkCanceled();
        exec.setProgress(i / (double) nrModels, "Exported model " + (i + 1) + "/" + nrModels);
    }
    container.close();
    return new BufferedDataTable[] { container.getTable() };
}
Also used : TreeEnsembleModelPortObject(org.knime.base.node.mine.treeensemble.model.TreeEnsembleModelPortObject) DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) RowKey(org.knime.core.data.RowKey) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

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