Search in sources :

Example 81 with PMMLPortObject

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

the class LogRegLearner method execute.

/**
 * Compute logistic regression model.
 *
 * @param portObjects The input objects.
 * @param exec the execution context
 * @return a {@link LogisticRegressionContent} storing computed data
 * @throws Exception if computation of the logistic regression model is
 * not successful or if given data is inconsistent with the settings
 * defined in the constructor.
 * @see LogRegLearnerNodeModel#execute(PortObjectSpec[])
 */
public LogisticRegressionContent 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());
    // the learner typically needs five steps with two runs over the data each step, calculating
    // the domain needs run over the data
    double calcDomainTime = 1.0 / (5.0 * 2.0 + 1.0);
    exec.setMessage("Analyzing categorical data");
    BufferedDataTable dataTable = recalcDomainForTargetAndLearningFields(data, inPMMLSpec, exec.createSubExecutionContext(calcDomainTime));
    checkConstantLearningFields(dataTable, inPMMLSpec);
    exec.setMessage("Building logistic 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 82 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];
    final DataTableSpec tableSpec = data.getDataTableSpec();
    final LogRegLearner learner = new LogRegLearner(new PortObjectSpec[] { tableSpec }, m_settings);
    m_content = learner.execute(new PortObject[] { data }, exec);
    String warn = learner.getWarningMessage();
    if (warn != null) {
        setWarningMessage(warn);
    }
    // second argument is ignored since we provide a spec
    PMMLPortObject outPMMLPort = new PMMLPortObject((PMMLPortObjectSpec) learner.getOutputSpec()[0], null, tableSpec);
    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) 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)

Example 83 with PMMLPortObject

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

the class GeneralRegressionPredictorNodeModel method execute.

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

Example 84 with PMMLPortObject

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

the class PolyRegLearnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable inTable = (BufferedDataTable) inData[0];
    DataTableSpec inSpec = inTable.getDataTableSpec();
    final int colCount = inSpec.getNumColumns();
    String[] selectedCols = computeSelectedColumns(inSpec);
    Set<String> hash = new HashSet<String>(Arrays.asList(selectedCols));
    m_colSelected = new boolean[colCount];
    for (int i = 0; i < colCount; i++) {
        m_colSelected[i] = hash.contains(inTable.getDataTableSpec().getColumnSpec(i).getName());
    }
    final int rowCount = inTable.getRowCount();
    final int independentVariables = selectedCols.length;
    final int degree = m_settings.getDegree();
    final int dependentIndex = inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn());
    double[][] xMat = new double[rowCount][1 + independentVariables * degree];
    double[][] yMat = new double[rowCount][1];
    int rowIndex = 0;
    for (DataRow row : inTable) {
        exec.checkCanceled();
        exec.setProgress(0.2 * rowIndex / rowCount);
        xMat[rowIndex][0] = 1;
        int colIndex = 1;
        for (int i = 0; i < row.getNumCells(); i++) {
            if ((m_colSelected[i] || (i == dependentIndex)) && row.getCell(i).isMissing()) {
                throw new IllegalArgumentException("Missing values are not supported by this node.");
            }
            if (m_colSelected[i]) {
                double val = ((DoubleValue) row.getCell(i)).getDoubleValue();
                double poly = val;
                xMat[rowIndex][colIndex] = poly;
                colIndex++;
                for (int d = 2; d <= degree; d++) {
                    poly *= val;
                    xMat[rowIndex][colIndex] = poly;
                    colIndex++;
                }
            } else if (i == dependentIndex) {
                double val = ((DoubleValue) row.getCell(i)).getDoubleValue();
                yMat[rowIndex][0] = val;
            }
        }
        rowIndex++;
    }
    // compute X'
    double[][] xTransMat = MathUtils.transpose(xMat);
    exec.setProgress(0.24);
    exec.checkCanceled();
    // compute X'X
    double[][] xxMat = MathUtils.multiply(xTransMat, xMat);
    exec.setProgress(0.28);
    exec.checkCanceled();
    // compute X'Y
    double[][] xyMat = MathUtils.multiply(xTransMat, yMat);
    exec.setProgress(0.32);
    exec.checkCanceled();
    // compute (X'X)^-1
    double[][] xxInverse;
    try {
        xxInverse = MathUtils.inverse(xxMat);
        exec.setProgress(0.36);
        exec.checkCanceled();
    } catch (ArithmeticException ex) {
        throw new ArithmeticException("The attributes of the data samples" + " are not mutually independent.");
    }
    // compute (X'X)^-1 * (X'Y)
    final double[][] betas = MathUtils.multiply(xxInverse, xyMat);
    exec.setProgress(0.4);
    m_betas = new double[independentVariables * degree + 1];
    for (int i = 0; i < betas.length; i++) {
        m_betas[i] = betas[i][0];
    }
    m_columnNames = selectedCols;
    String[] temp = new String[m_columnNames.length + 1];
    System.arraycopy(m_columnNames, 0, temp, 0, m_columnNames.length);
    temp[temp.length - 1] = m_settings.getTargetColumn();
    FilterColumnTable filteredTable = new FilterColumnTable(inTable, temp);
    DataArray rowContainer = new DefaultDataArray(filteredTable, 1, m_settings.getMaxRowsForView());
    int ignore = rowContainer.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn());
    m_meanValues = new double[independentVariables];
    for (DataRow row : rowContainer) {
        int k = 0;
        for (int i = 0; i < row.getNumCells(); i++) {
            if (i != ignore) {
                m_meanValues[k++] += ((DoubleValue) row.getCell(i)).getDoubleValue();
            }
        }
    }
    for (int i = 0; i < m_meanValues.length; i++) {
        m_meanValues[i] /= rowContainer.size();
    }
    ColumnRearranger crea = new ColumnRearranger(inTable.getDataTableSpec());
    crea.append(getCellFactory(inTable.getDataTableSpec().findColumnIndex(m_settings.getTargetColumn())));
    // handle the optional PMML input
    PMMLPortObject inPMMLPort = (PMMLPortObject) inData[1];
    PortObject[] bdt = new PortObject[] { exec.createColumnRearrangeTable(inTable, crea, exec.createSubProgress(0.6)), createPMMLModel(inPMMLPort, inTable.getDataTableSpec()) };
    m_squaredError /= rowCount;
    m_viewData = new PolyRegViewData(m_meanValues, m_betas, m_squaredError, m_columnNames, m_settings.getDegree(), m_settings.getTargetColumn());
    m_rowContainer = rowContainer;
    return bdt;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) FilterColumnTable(org.knime.base.data.filter.column.FilterColumnTable) DataRow(org.knime.core.data.DataRow) DataArray(org.knime.base.node.util.DataArray) DefaultDataArray(org.knime.base.node.util.DefaultDataArray) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DoubleValue(org.knime.core.data.DoubleValue) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) HashSet(java.util.HashSet)

Example 85 with PMMLPortObject

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

the class RegressionTreeModelPortObject method createDecisionTreePMMLPortObject.

public PMMLPortObject createDecisionTreePMMLPortObject() {
    final RegressionTreeModel model = getModel();
    DataTableSpec attributeLearnSpec = model.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 TreeModelRegression tree = model.getTreeModel();
    portObject.addModelTranslater(new RegressionTreeModelPMMLTranslator(tree, model.getMetaData(), m_spec.getLearnTableSpec()));
    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) RegressionTreeModelPMMLTranslator(org.knime.base.node.mine.treeensemble2.model.pmml.RegressionTreeModelPMMLTranslator)

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