Search in sources :

Example 1 with NaiveBayesPortObject

use of org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject in project knime-core by knime.

the class NaiveBayesPredictorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    LOGGER.debug("Entering execute(inData, exec) of class " + "NaiveBayesPredictorNodeModel.");
    // check input data
    assert (inData != null && inData.length == 2 && inData[DATA_IN_PORT] != null && inData[MODEL_IN_PORT] != null);
    final PortObject dataObject = inData[DATA_IN_PORT];
    if (!(dataObject instanceof BufferedDataTable)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final BufferedDataTable data = (BufferedDataTable) dataObject;
    final PortObject modelObject = inData[MODEL_IN_PORT];
    if (!(modelObject instanceof NaiveBayesPortObject)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final NaiveBayesModel model = ((NaiveBayesPortObject) modelObject).getModel();
    exec.setMessage("Classifying rows...");
    if (model == null) {
        throw new Exception("Node not properly configured. " + "No Naive Bayes Model available.");
    }
    final double laplaceCorrector = m_laplaceCorrector.getDoubleValue();
    final NaiveBayesCellFactory appender = new NaiveBayesCellFactory(model, data.getDataTableSpec(), m_inclProbVals.getBooleanValue(), laplaceCorrector);
    final ColumnRearranger rearranger = new ColumnRearranger(data.getDataTableSpec());
    rearranger.append(appender);
    final BufferedDataTable returnVal = exec.createColumnRearrangeTable(data, rearranger, exec);
    LOGGER.debug("Exiting execute(inData, exec) of class " + "NaiveBayesPredictorNodeModel.");
    return new PortObject[] { returnVal };
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) NaiveBayesPortObject(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) NaiveBayesModel(org.knime.base.node.mine.bayes.naivebayes.datamodel.NaiveBayesModel) NaiveBayesPortObject(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject) PortObject(org.knime.core.node.port.PortObject) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 2 with NaiveBayesPortObject

use of org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject in project knime-core by knime.

the class NaiveBayesLearnerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
    LOGGER.debug("Entering execute of " + NaiveBayesLearnerNodeModel.class.getName());
    // check input data
    assert (inData != null && inData.length == 1 && 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 String colName = m_classifyColumnName.getStringValue();
    final boolean ignoreMissingVals = m_ignoreMissingVals.getBooleanValue();
    final int maxNoOfNomVals = m_maxNoOfNominalVals.getIntValue();
    m_model = new NaiveBayesModel(trainingTable, colName, exec, maxNoOfNomVals, ignoreMissingVals);
    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 " + NaiveBayesLearnerNodeModel.class.getName());
    // return no data tables (empty array)
    return new PortObject[] { new NaiveBayesPortObject(trainingTable.getDataTableSpec(), m_model) };
}
Also used : NaiveBayesPortObject(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) NaiveBayesModel(org.knime.base.node.mine.bayes.naivebayes.datamodel.NaiveBayesModel) NaiveBayesPortObject(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject) PortObject(org.knime.core.node.port.PortObject)

Example 3 with NaiveBayesPortObject

use of org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject in project knime-core by knime.

the class NaiveBayesPredictorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    LOGGER.debug("Entering execute(inData, exec) of class " + "NaiveBayesPredictorNodeModel.");
    // check input data
    assert (inData != null && inData.length == 2 && inData[DATA_IN_PORT] != null && inData[MODEL_IN_PORT] != null);
    final PortObject dataObject = inData[DATA_IN_PORT];
    if (!(dataObject instanceof BufferedDataTable)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final BufferedDataTable data = (BufferedDataTable) dataObject;
    final PortObject modelObject = inData[MODEL_IN_PORT];
    if (!(modelObject instanceof NaiveBayesPortObject)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final NaiveBayesModel model = ((NaiveBayesPortObject) modelObject).getModel();
    exec.setMessage("Classifying rows...");
    if (model == null) {
        throw new Exception("Node not properly configured. " + "No Naive Bayes Model available.");
    }
    final double laplaceCorrector = m_laplaceCorrector.getDoubleValue();
    PredictorHelper predictorHelper = PredictorHelper.getInstance();
    String classColumnName = model.getClassColumnName();
    String predictionColName = m_overridePredicted.getBooleanValue() ? m_predictionColumnName.getStringValue() : predictorHelper.computePredictionDefault(classColumnName);
    final NaiveBayesCellFactory appender = new NaiveBayesCellFactory(model, predictionColName, data.getDataTableSpec(), m_inclProbVals.getBooleanValue(), laplaceCorrector, m_probabilitySuffix.getStringValue());
    final ColumnRearranger rearranger = new ColumnRearranger(data.getDataTableSpec());
    rearranger.append(appender);
    final BufferedDataTable returnVal = exec.createColumnRearrangeTable(data, rearranger, exec);
    LOGGER.debug("Exiting execute(inData, exec) of class " + "NaiveBayesPredictorNodeModel.");
    return new PortObject[] { returnVal };
}
Also used : PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) NaiveBayesPortObject(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) NaiveBayesModel(org.knime.base.node.mine.bayes.naivebayes.datamodel.NaiveBayesModel) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) NaiveBayesPortObject(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject) PortObject(org.knime.core.node.port.PortObject) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Aggregations

NaiveBayesModel (org.knime.base.node.mine.bayes.naivebayes.datamodel.NaiveBayesModel)3 NaiveBayesPortObject (org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject)3 BufferedDataTable (org.knime.core.node.BufferedDataTable)3 PortObject (org.knime.core.node.port.PortObject)3 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2 PredictorHelper (org.knime.base.node.mine.util.PredictorHelper)1