Search in sources :

Example 1 with RegressionTable

use of org.knime.base.node.mine.regression.PMMLRegressionTranslator.RegressionTable 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 2 with RegressionTable

use of org.knime.base.node.mine.regression.PMMLRegressionTranslator.RegressionTable in project knime-core by knime.

the class RegressionPredictorNodeModel method createRearranger.

private ColumnRearranger createRearranger(final DataTableSpec inSpec, final PMMLPortObjectSpec regModelSpec, final PMMLRegressionTranslator regModel) throws InvalidSettingsException {
    if (regModelSpec == null) {
        throw new InvalidSettingsException("No input");
    }
    // exclude last (response column)
    String targetCol = "Response";
    for (String s : regModelSpec.getTargetFields()) {
        targetCol = s;
        break;
    }
    final List<String> learnFields;
    if (regModel != null) {
        RegressionTable regTable = regModel.getRegressionTable();
        learnFields = new ArrayList<String>();
        for (NumericPredictor p : regTable.getVariables()) {
            learnFields.add(p.getName());
        }
    } else {
        learnFields = new ArrayList<String>(regModelSpec.getLearningFields());
    }
    final int[] colIndices = new int[learnFields.size()];
    int k = 0;
    for (String learnCol : learnFields) {
        int index = inSpec.findColumnIndex(learnCol);
        if (index < 0) {
            throw new InvalidSettingsException("Missing column for " + "regressor variable : \"" + learnCol + "\"");
        }
        DataColumnSpec regressor = inSpec.getColumnSpec(index);
        String name = regressor.getName();
        DataColumnSpec col = inSpec.getColumnSpec(index);
        if (!col.getType().isCompatible(DoubleValue.class)) {
            throw new InvalidSettingsException("Incompatible type of " + "column \"" + name + "\": " + col.getType());
        }
        colIndices[k++] = index;
    }
    // try to use some smart naming scheme for the append column
    String oldName = targetCol;
    if (inSpec.containsName(oldName) && !oldName.toLowerCase().endsWith("(prediction)")) {
        oldName = oldName + " (prediction)";
    }
    String newColName = DataTableSpec.getUniqueColumnName(inSpec, oldName);
    DataColumnSpec newCol = new DataColumnSpecCreator(newColName, DoubleCell.TYPE).createSpec();
    SingleCellFactory fac = new SingleCellFactory(newCol) {

        @Override
        public DataCell getCell(final DataRow row) {
            RegressionTable t = regModel.getRegressionTable();
            int j = 0;
            double result = t.getIntercept();
            for (NumericPredictor p : t.getVariables()) {
                DataCell c = row.getCell(colIndices[j++]);
                if (c.isMissing()) {
                    return DataType.getMissingCell();
                }
                double v = ((DoubleValue) c).getDoubleValue();
                if (p.getExponent() != 1) {
                    v = Math.pow(v, p.getExponent());
                }
                result += p.getCoefficient() * v;
            }
            return new DoubleCell(result);
        }
    };
    ColumnRearranger c = new ColumnRearranger(inSpec);
    c.append(fac);
    return c;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) NumericPredictor(org.knime.base.node.mine.regression.PMMLRegressionTranslator.NumericPredictor) DataRow(org.knime.core.data.DataRow) RegressionTable(org.knime.base.node.mine.regression.PMMLRegressionTranslator.RegressionTable) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 3 with RegressionTable

use of org.knime.base.node.mine.regression.PMMLRegressionTranslator.RegressionTable 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 4 with RegressionTable

use of org.knime.base.node.mine.regression.PMMLRegressionTranslator.RegressionTable in project knime-core by knime.

the class LinearRegressionContent method createPortObject.

/**
 * Creates a new PMML regression port object from this linear regression
 * model.
 * @param inPMMLPort the incoming PMMLPort object (can be null)
 * @param dts the full data table spec with which the regression
 *      model was created.
 * @param learningSpec a data table spec containing only learning columns
 * @return a port object
 * @throws InvalidSettingsException if the settings are invalid
 */
public PMMLPortObject createPortObject(final PMMLPortObject inPMMLPort, final DataTableSpec dts, final DataTableSpec learningSpec) throws InvalidSettingsException {
    PMMLPortObjectSpec inPMMLSpec = null;
    if (inPMMLPort != null) {
        inPMMLSpec = inPMMLPort.getSpec();
    }
    PMMLPortObjectSpec spec = createPortObjectSpec(inPMMLSpec, dts, learningSpec);
    PMMLPortObject outPMMLPort = new PMMLPortObject(spec, inPMMLPort);
    NumericPredictor[] nps = new NumericPredictor[m_multipliers.length];
    for (int i = 0; i < nps.length; i++) {
        nps[i] = new NumericPredictor(m_spec.getColumnSpec(i).getName(), 1, m_multipliers[i]);
    }
    RegressionTable regressionTable = new RegressionTable(m_offset, nps);
    /* To maintain compatibility with the previous SAX-based implementation.
         * */
    String targetField = "Response";
    List<String> targetFields = spec.getTargetFields();
    if (!targetFields.isEmpty()) {
        targetField = targetFields.get(0);
    }
    PMMLRegressionTranslator trans = new PMMLRegressionTranslator(MODEL_NAME, ALGORITHM_NAME, regressionTable, 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)

Aggregations

NumericPredictor (org.knime.base.node.mine.regression.PMMLRegressionTranslator.NumericPredictor)4 RegressionTable (org.knime.base.node.mine.regression.PMMLRegressionTranslator.RegressionTable)4 PMMLRegressionTranslator (org.knime.base.node.mine.regression.PMMLRegressionTranslator)3 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)3 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)3 DataCell (org.knime.core.data.DataCell)1 DataColumnSpec (org.knime.core.data.DataColumnSpec)1 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)1 DataRow (org.knime.core.data.DataRow)1 DoubleValue (org.knime.core.data.DoubleValue)1 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)1 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)1 DoubleCell (org.knime.core.data.def.DoubleCell)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1