Search in sources :

Example 71 with PMMLPortObjectSpec

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

Example 72 with PMMLPortObjectSpec

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

the class RegressionTreePMMLPredictorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) inSpecs[0];
    DataType targetType = extractTargetType(pmmlSpec);
    if (!targetType.isCompatible(DoubleValue.class)) {
        throw new InvalidSettingsException("This node expects a regression model.");
    }
    try {
        AbstractTreeModelPMMLTranslator.checkPMMLSpec(pmmlSpec);
    } catch (IllegalArgumentException e) {
        throw new InvalidSettingsException(e.getMessage());
    }
    RegressionTreeModelPortObjectSpec modelSpec = translateSpec(pmmlSpec);
    String targetColName = modelSpec.getTargetColumn().getName();
    if (m_configuration == null) {
        m_configuration = RegressionTreePredictorConfiguration.createDefault(targetColName);
    } else if (!m_configuration.isChangePredictionColumnName()) {
        m_configuration.setPredictionColumnName(TreeEnsemblePredictorConfiguration.getPredictColumnName(targetColName));
    }
    DataTableSpec dataSpec = (DataTableSpec) inSpecs[1];
    final RegressionTreePredictor pred = new RegressionTreePredictor(null, modelSpec, dataSpec, m_configuration);
    return new PortObjectSpec[] { pred.getPredictionRearranger().createSpec() };
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataTableSpec(org.knime.core.data.DataTableSpec) DoubleValue(org.knime.core.data.DoubleValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) RegressionTreeModelPortObjectSpec(org.knime.base.node.mine.treeensemble2.model.RegressionTreeModelPortObjectSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DataType(org.knime.core.data.DataType) RegressionTreeModelPortObjectSpec(org.knime.base.node.mine.treeensemble2.model.RegressionTreeModelPortObjectSpec)

Example 73 with PMMLPortObjectSpec

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

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

the class LogisticRegressionContent method load.

/**
 * @param parContent the content that holds the internals
 * @param spec the data table spec of the training data
 * @return a instance with he loaded values
 * @throws InvalidSettingsException when data are not well formed
 */
static LogisticRegressionContent load(final ModelContentRO parContent, final DataTableSpec spec) throws InvalidSettingsException {
    String target = parContent.getString(CFG_TARGET);
    String[] learningCols = parContent.getStringArray(CFG_LEARNING_COLS);
    PMMLPortObjectSpec pmmlSpec = createSpec(spec, target, learningCols);
    String[] factors = parContent.getStringArray(CFG_FACTORS);
    // Since 3.0
    final String[] vectorColumns = parContent.getStringArray(CFG_VECTOR_NAMES);
    final int[] vectorLengths = parContent.getIntArray(CFG_VECTOR_LENGTHS);
    CheckUtils.checkSetting(vectorColumns.length == vectorLengths.length, "The length of vector names and their length do not match: " + vectorColumns.length + " <> " + vectorLengths.length);
    Map<String, Integer> vectorLengthMap = new LinkedHashMap<>();
    for (int i = 0; i < vectorLengths.length; i++) {
        vectorLengthMap.put(vectorColumns[i], vectorLengths[i]);
    }
    String[] covariates = parContent.getStringArray(CFG_COVARIATES);
    double[] coeff = parContent.getDoubleArray(CFG_COEFFICIENTS);
    double likelihood = parContent.getDouble(CFG_LOG_LIKELIHOOD);
    double[] covMat = parContent.getDoubleArray(CFG_COVARIANCE_MATRIX);
    int iter = parContent.getInt(CFG_ITER);
    // introduced in 2.9
    DataCell targetReferenceCategory = parContent.getDataCell(CFG_TARGET_REFERENCE_CATEGORY, null);
    boolean sortTargetCategories = parContent.getBoolean(CFG_SORT_TARGET_CATEGORIES, true);
    boolean sortFactorsCategories = parContent.getBoolean(CFG_SORT_FACTORS_CATEGORIES, true);
    return new LogisticRegressionContent(pmmlSpec, Arrays.asList(factors), Arrays.asList(covariates), vectorLengthMap, targetReferenceCategory, sortTargetCategories, sortFactorsCategories, toMatrix(coeff, coeff.length), likelihood, toMatrix(covMat, coeff.length), iter);
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) LinkedHashMap(java.util.LinkedHashMap) DataCell(org.knime.core.data.DataCell)

Example 75 with PMMLPortObjectSpec

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

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