Search in sources :

Example 1 with PMMLNaiveBayesModelTranslator

use of org.knime.base.node.mine.bayes.naivebayes.datamodel2.PMMLNaiveBayesModelTranslator in project knime-core by knime.

the class NaiveBayesLearnerNodeModel2 method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
    LOGGER.debug("Entering execute of " + NaiveBayesLearnerNodeModel2.class.getName());
    assert (inData != null && ((inData.length == 2 && m_pmmlInEnabled) || (inData.length == 1 && !m_pmmlInEnabled)) && inData[TRAINING_DATA_PORT] != null);
    final PortObject inObject = inData[TRAINING_DATA_PORT];
    if (!(inObject instanceof BufferedDataTable)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final BufferedDataTable trainingTable = (BufferedDataTable) inObject;
    final boolean ignoreMissingVals = m_ignoreMissingVals.getBooleanValue();
    final boolean pmmlCompatible = m_pmmlCompatible.getBooleanValue();
    final int maxNoOfNomVals = m_maxNoOfNominalVals.getIntValue();
    m_model = new NaiveBayesModel(trainingTable, m_classifyColumnName.getStringValue(), exec, maxNoOfNomVals, ignoreMissingVals, pmmlCompatible, m_threshold.getDoubleValue());
    final List<String> missingModels = m_model.getAttributesWithMissingVals();
    if (missingModels.size() > 0) {
        final StringBuilder buf = new StringBuilder();
        buf.append("The following attributes contain missing values: ");
        for (int i = 0, length = missingModels.size(); i < length; i++) {
            if (i != 0) {
                buf.append(", ");
            }
            if (i > 3) {
                buf.append("...(see View)");
                break;
            }
            buf.append(missingModels.get(i));
        }
        setWarningMessage(buf.toString());
    }
    if (m_model.containsSkippedAttributes()) {
        setWarningMessage(m_model.getSkippedAttributesString(3));
    }
    LOGGER.debug("Exiting execute of " + NaiveBayesLearnerNodeModel2.class.getName());
    // handle the optional PMML input
    final PMMLPortObject inPMMLPort = m_pmmlInEnabled ? (PMMLPortObject) inData[MODEL_INPORT] : null;
    final DataTableSpec tableSpec = trainingTable.getSpec();
    final PMMLPortObjectSpec outPortSpec = createPMMLSpec(tableSpec, inPMMLPort == null ? null : inPMMLPort.getSpec(), m_model.getPMMLLearningCols(), m_model.getClassColumnName());
    final PMMLPortObject outPMMLPort = new PMMLPortObject(outPortSpec, inPMMLPort, tableSpec);
    outPMMLPort.addModelTranslater(new PMMLNaiveBayesModelTranslator(m_model));
    return new PortObject[] { outPMMLPort, m_model.getStatisticsTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLNaiveBayesModelTranslator(org.knime.base.node.mine.bayes.naivebayes.datamodel2.PMMLNaiveBayesModelTranslator) NaiveBayesModel(org.knime.base.node.mine.bayes.naivebayes.datamodel2.NaiveBayesModel) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PortObject(org.knime.core.node.port.PortObject)

Example 2 with PMMLNaiveBayesModelTranslator

use of org.knime.base.node.mine.bayes.naivebayes.datamodel2.PMMLNaiveBayesModelTranslator in project knime-core by knime.

the class NaiveBayesPredictorNodeModel2 method createColumnRearranger.

/* Helper to create the column rearranger that does the actual work */
private ColumnRearranger createColumnRearranger(final PMMLPortObject pmmlPortObj, final DataTableSpec inSpec) {
    final PMMLNaiveBayesModelTranslator translator = new PMMLNaiveBayesModelTranslator();
    pmmlPortObj.initializeModelTranslator(translator);
    final NaiveBayesModel model = translator.getModel();
    PredictorHelper predictorHelper = PredictorHelper.getInstance();
    final String classColumnName = model.getClassColumnName();
    final String predictionColName = m_overridePredicted.getBooleanValue() ? m_predictionColumnName.getStringValue() : predictorHelper.computePredictionDefault(classColumnName);
    final NaiveBayesCellFactory appender = new NaiveBayesCellFactory(model, predictionColName, inSpec, m_inclProbVals.getBooleanValue(), m_probabilitySuffix.getStringValue());
    final ColumnRearranger rearranger = new ColumnRearranger(inSpec);
    rearranger.append(appender);
    return rearranger;
}
Also used : PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLNaiveBayesModelTranslator(org.knime.base.node.mine.bayes.naivebayes.datamodel2.PMMLNaiveBayesModelTranslator) NaiveBayesModel(org.knime.base.node.mine.bayes.naivebayes.datamodel2.NaiveBayesModel) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Aggregations

NaiveBayesModel (org.knime.base.node.mine.bayes.naivebayes.datamodel2.NaiveBayesModel)2 PMMLNaiveBayesModelTranslator (org.knime.base.node.mine.bayes.naivebayes.datamodel2.PMMLNaiveBayesModelTranslator)2 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2 PredictorHelper (org.knime.base.node.mine.util.PredictorHelper)1 DataTableSpec (org.knime.core.data.DataTableSpec)1 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)1 BufferedDataTable (org.knime.core.node.BufferedDataTable)1 PortObject (org.knime.core.node.port.PortObject)1 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)1 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)1