Search in sources :

Example 16 with PMMLPortObjectSpec

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

the class PolyRegLearnerNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec tableSpec = (DataTableSpec) inSpecs[0];
    PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) inSpecs[1];
    String[] selectedCols = computeSelectedColumns(tableSpec);
    for (String colName : selectedCols) {
        DataColumnSpec dcs = tableSpec.getColumnSpec(colName);
        if (dcs == null) {
            throw new InvalidSettingsException("Selected column '" + colName + "' does not exist in input table");
        }
        if (!dcs.getType().isCompatible(DoubleValue.class)) {
            throw new InvalidSettingsException("Selected column '" + dcs.getName() + "' from the input table is not a numeric column.");
        }
    }
    if (m_settings.getTargetColumn() == null) {
        throw new InvalidSettingsException("No target column selected");
    }
    if (tableSpec.findColumnIndex(m_settings.getTargetColumn()) == -1) {
        throw new InvalidSettingsException("Target column '" + m_settings.getTargetColumn() + "' does not exist.");
    }
    DataColumnSpecCreator crea = new DataColumnSpecCreator("PolyReg prediction", DoubleCell.TYPE);
    DataColumnSpec col1 = crea.createSpec();
    crea = new DataColumnSpecCreator("Prediction Error", DoubleCell.TYPE);
    DataColumnSpec col2 = crea.createSpec();
    return new PortObjectSpec[] { AppendedColumnTable.getTableSpec(tableSpec, col1, col2), createModelSpec(pmmlSpec, tableSpec) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec)

Example 17 with PMMLPortObjectSpec

use of org.knime.core.node.port.pmml.PMMLPortObjectSpec 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 PMMLPortObjectSpec

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

the class RegressionPredictorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    PMMLPortObjectSpec regModelSpec = (PMMLPortObjectSpec) inSpecs[0];
    DataTableSpec dataSpec = (DataTableSpec) inSpecs[1];
    if (dataSpec == null || regModelSpec == null) {
        throw new InvalidSettingsException("No input specification available");
    }
    for (String learnColName : regModelSpec.getLearningFields()) {
        if (!dataSpec.containsName(learnColName)) {
            throw new InvalidSettingsException("Learning column \"" + learnColName + "\" not found in input data to be predicted");
        }
    }
    ColumnRearranger rearranger = createRearranger(dataSpec, regModelSpec, null);
    DataTableSpec outSpec = rearranger.createSpec();
    return new DataTableSpec[] { outSpec };
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataTableSpec(org.knime.core.data.DataTableSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 19 with PMMLPortObjectSpec

use of org.knime.core.node.port.pmml.PMMLPortObjectSpec 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 PMMLPortObjectSpec

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

the class LearnerTest method testPerformLowBirthWeightData.

/**
 * Test method for {@link org.knime.base.node.mine.regression.logistic.learner.Learner#perform(BufferedDataTable, org.knime.core.node.ExecutionContext)}.
 * @throws CanceledExecutionException
 */
@Test
public final void testPerformLowBirthWeightData() throws Exception {
    final BufferedDataTable data = m_exec.createBufferedDataTable(new LowBirthWeightData(), m_exec);
    PMMLPortObjectSpecCreator specCreator = new PMMLPortObjectSpecCreator(data.getDataTableSpec());
    specCreator.setLearningColsNames(Arrays.asList(new String[] { "AGE", "LWT", "RACE", "FTV" }));
    specCreator.setTargetColName("LOW");
    final PMMLPortObjectSpec spec = specCreator.createSpec();
    // done in KNIME thread pool, expected by code
    Future<LogisticRegressionContent> callable = KNIMEConstants.GLOBAL_THREAD_POOL.enqueue(new Callable<LogisticRegressionContent>() {

        @Override
        public LogisticRegressionContent call() throws Exception {
            final Learner learner = new Learner(spec, null, true, true);
            return learner.perform(data, m_exec);
        }
    });
    LogisticRegressionContent content = callable.get();
    // Reference results are published in the book:
    // Applied Logistic Regression,
    // David W. Hosmer and Stanley Lemeshow
    // Wiley, 2000 (2nd. ed)
    // The table of results are found on page 36
    Assert.assertEquals(-111.286, content.getEstimatedLikelihood(), 0.001);
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) Test(org.junit.Test)

Aggregations

PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)77 DataTableSpec (org.knime.core.data.DataTableSpec)57 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)40 DataColumnSpec (org.knime.core.data.DataColumnSpec)31 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)30 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)23 PMMLPortObjectSpecCreator (org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator)23 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)22 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)20 BufferedDataTable (org.knime.core.node.BufferedDataTable)15 PortObject (org.knime.core.node.port.PortObject)12 DataCell (org.knime.core.data.DataCell)10 DoubleValue (org.knime.core.data.DoubleValue)10 DataRow (org.knime.core.data.DataRow)8 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)7 ArrayList (java.util.ArrayList)6 LinkedList (java.util.LinkedList)6 DataColumnDomain (org.knime.core.data.DataColumnDomain)6 DoubleCell (org.knime.core.data.def.DoubleCell)6 IOException (java.io.IOException)4