Search in sources :

Example 1 with NaiveBayesPortObjectSpec

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

the class NaiveBayesPredictorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    // check the input data
    assert (inSpecs != null && inSpecs.length == 2 && inSpecs[DATA_IN_PORT] != null && inSpecs[MODEL_IN_PORT] != null);
    final PortObjectSpec modelObject = inSpecs[MODEL_IN_PORT];
    if (!(modelObject instanceof NaiveBayesPortObjectSpec)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final DataTableSpec trainingSpec = ((NaiveBayesPortObjectSpec) modelObject).getTableSpec();
    final DataColumnSpec classColumn = ((NaiveBayesPortObjectSpec) modelObject).getClassColumn();
    if (trainingSpec == null) {
        throw new InvalidSettingsException("No model spec available");
    }
    final PortObjectSpec inSpec = inSpecs[DATA_IN_PORT];
    if (!(inSpec instanceof DataTableSpec)) {
        throw new IllegalArgumentException("TableSpec must not be null");
    }
    final DataTableSpec spec = (DataTableSpec) inSpec;
    // check the input data for columns with the wrong name or wrong type
    final List<String> unknownCols = check4UnknownCols(trainingSpec, spec);
    if (unknownCols.size() >= spec.getNumColumns()) {
        setWarningMessage("No known attribute columns found use " + "class prior probability to predict the class membership");
    } else if (unknownCols.size() == 1) {
        setWarningMessage("Input column " + unknownCols.get(0) + " is unknown and will be skipped.");
    } else if (unknownCols.size() > 1) {
        final StringBuilder buf = new StringBuilder();
        buf.append("The following input columns are unknown and " + "will be skipped: ");
        for (int i = 0, length = unknownCols.size(); i < length; i++) {
            if (i != 0) {
                buf.append(", ");
            }
            if (i > 3) {
                buf.append("...");
                break;
            }
            buf.append(unknownCols.get(i));
        }
        setWarningMessage(buf.toString());
    }
    // check if the learned model contains columns which are not in the
    // input data
    final List<String> missingInputCols = check4MissingCols(trainingSpec, classColumn.getName(), spec);
    if (missingInputCols.size() == 1) {
        setWarningMessage("Attribute " + missingInputCols.get(0) + " is missing in the input data");
    } else if (missingInputCols.size() > 1) {
        final StringBuilder buf = new StringBuilder();
        buf.append("The following attributes are missing in " + "the input data: ");
        for (int i = 0, length = missingInputCols.size(); i < length; i++) {
            if (i != 0) {
                buf.append(", ");
            }
            if (i > 3) {
                buf.append("...");
                break;
            }
            buf.append(missingInputCols.get(i));
        }
        setWarningMessage(buf.toString());
    }
    final DataColumnSpec resultColSpecs = NaiveBayesCellFactory.createResultColSpecs(classColumn, spec, m_inclProbVals.getBooleanValue());
    if (resultColSpecs != null) {
        return new PortObjectSpec[] { AppendedColumnTable.getTableSpec(spec, resultColSpecs) };
    }
    return null;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec)

Example 2 with NaiveBayesPortObjectSpec

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

the class NaiveBayesLearnerNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    // check the internal variables if they are valid
    final String classColumn = m_classifyColumnName.getStringValue();
    if (classColumn == null || classColumn.length() < 1) {
        throw new InvalidSettingsException("Please define the classification column");
    }
    final PortObjectSpec inSpec = inSpecs[TRAINING_DATA_PORT];
    if (!(inSpec instanceof DataTableSpec)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final DataTableSpec tableSpec = (DataTableSpec) inSpec;
    if (tableSpec.findColumnIndex(classColumn) < 0) {
        throw new InvalidSettingsException("Please define the classification column");
    }
    if (tableSpec.getNumColumns() < 2) {
        throw new InvalidSettingsException("Input table should contain at least 2 columns");
    }
    final int maxNoOfNominalVals = m_maxNoOfNominalVals.getIntValue();
    // check if the table contains at least one nominal column
    // and check each nominal column with a valid domain
    // if it contains more values than allowed
    boolean containsNominalCol = false;
    final List<String> toBigNominalColumns = new ArrayList<>();
    for (int i = 0, length = tableSpec.getNumColumns(); i < length; i++) {
        final DataColumnSpec colSpec = tableSpec.getColumnSpec(i);
        if (colSpec.getType().isCompatible(NominalValue.class)) {
            containsNominalCol = true;
            final DataColumnDomain domain = colSpec.getDomain();
            if (domain != null && domain.getValues() != null) {
                if (domain.getValues().size() > maxNoOfNominalVals) {
                    // unique values
                    if (colSpec.getName().equals(classColumn)) {
                        // contains too many unique values
                        throw new InvalidSettingsException("Class column domain contains too many unique values" + " (" + domain.getValues().size() + ")");
                    }
                    toBigNominalColumns.add(colSpec.getName() + " (" + domain.getValues().size() + ")");
                }
            }
        }
    }
    if (!containsNominalCol) {
        throw new InvalidSettingsException("No possible class attribute found in input table");
    }
    if (toBigNominalColumns.size() == 1) {
        setWarningMessage("Column " + toBigNominalColumns.get(0) + " will possibly be skipped.");
    } else if (toBigNominalColumns.size() > 1) {
        final StringBuilder buf = new StringBuilder();
        buf.append("The following columns will possibly be skipped: ");
        for (int i = 0, length = toBigNominalColumns.size(); i < length; i++) {
            if (i != 0) {
                buf.append(", ");
            }
            if (i > 3) {
                buf.append("...");
                break;
            }
            buf.append(toBigNominalColumns.get(i));
        }
        setWarningMessage(buf.toString());
    }
    if (tableSpec.getNumColumns() - toBigNominalColumns.size() < 1) {
        throw new InvalidSettingsException("Not enough valid columns");
    }
    return new PortObjectSpec[] { new NaiveBayesPortObjectSpec(tableSpec, tableSpec.getColumnSpec(classColumn)) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) ArrayList(java.util.ArrayList) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec)

Example 3 with NaiveBayesPortObjectSpec

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

the class NaiveBayesPredictorNodeDialog method extractTargetColumn.

/**
 * {@inheritDoc}
 */
@Override
protected void extractTargetColumn(final PortObjectSpec[] specs) {
    if (specs[0] instanceof NaiveBayesPortObjectSpec) {
        NaiveBayesPortObjectSpec spec = (NaiveBayesPortObjectSpec) specs[0];
        setLastTargetColumn(spec.getClassColumn());
    } else {
        throw new IllegalStateException(specs[0].getClass().toString());
    }
}
Also used : NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec)

Example 4 with NaiveBayesPortObjectSpec

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

the class NaiveBayesPredictorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    // check the input data
    assert (inSpecs != null && inSpecs.length == 2 && inSpecs[DATA_IN_PORT] != null && inSpecs[MODEL_IN_PORT] != null);
    final PortObjectSpec modelObject = inSpecs[MODEL_IN_PORT];
    if (!(modelObject instanceof NaiveBayesPortObjectSpec)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final DataTableSpec trainingSpec = ((NaiveBayesPortObjectSpec) modelObject).getTableSpec();
    final DataColumnSpec classColumn = ((NaiveBayesPortObjectSpec) modelObject).getClassColumn();
    if (trainingSpec == null) {
        throw new InvalidSettingsException("No model spec available");
    }
    final PortObjectSpec inSpec = inSpecs[DATA_IN_PORT];
    if (!(inSpec instanceof DataTableSpec)) {
        throw new IllegalArgumentException("TableSpec must not be null");
    }
    final DataTableSpec spec = (DataTableSpec) inSpec;
    // check the input data for columns with the wrong name or wrong type
    final List<String> unknownCols = check4UnknownCols(trainingSpec, spec);
    if (unknownCols.size() >= spec.getNumColumns()) {
        setWarningMessage("No known attribute columns found use " + "class prior probability to predict the class membership");
    } else if (unknownCols.size() == 1) {
        setWarningMessage("Input column " + unknownCols.get(0) + " is unknown and will be skipped.");
    } else if (unknownCols.size() > 1) {
        final StringBuilder buf = new StringBuilder();
        buf.append("The following input columns are unknown and " + "will be skipped: ");
        for (int i = 0, length = unknownCols.size(); i < length; i++) {
            if (i != 0) {
                buf.append(", ");
            }
            if (i > 3) {
                buf.append("...");
                break;
            }
            buf.append(unknownCols.get(i));
        }
        setWarningMessage(buf.toString());
    }
    // check if the learned model contains columns which are not in the
    // input data
    final List<String> missingInputCols = check4MissingCols(trainingSpec, classColumn.getName(), spec);
    if (missingInputCols.size() == 1) {
        setWarningMessage("Attribute " + missingInputCols.get(0) + " is missing in the input data");
    } else if (missingInputCols.size() > 1) {
        final StringBuilder buf = new StringBuilder();
        buf.append("The following attributes are missing in " + "the input data: ");
        for (int i = 0, length = missingInputCols.size(); i < length; i++) {
            if (i != 0) {
                buf.append(", ");
            }
            if (i > 3) {
                buf.append("...");
                break;
            }
            buf.append(missingInputCols.get(i));
        }
        setWarningMessage(buf.toString());
    }
    final PredictorHelper predictorHelper = PredictorHelper.getInstance();
    final DataColumnSpec resultColSpecs = NaiveBayesCellFactory.createResultColSpecs(predictorHelper.computePredictionColumnName(m_predictionColumnName.getStringValue(), m_overridePredicted.getBooleanValue(), classColumn.getName()), classColumn.getType(), spec, m_inclProbVals.getBooleanValue());
    if (resultColSpecs != null) {
        return new PortObjectSpec[] { AppendedColumnTable.getTableSpec(spec, resultColSpecs) };
    }
    return null;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) NaiveBayesPortObjectSpec(org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Aggregations

NaiveBayesPortObjectSpec (org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 DataTableSpec (org.knime.core.data.DataTableSpec)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2 ArrayList (java.util.ArrayList)1 PredictorHelper (org.knime.base.node.mine.util.PredictorHelper)1 DataColumnDomain (org.knime.core.data.DataColumnDomain)1