Search in sources :

Example 6 with PredictorHelper

use of org.knime.base.node.mine.util.PredictorHelper in project knime-core by knime.

the class NaiveBayesPredictorNodeModel2 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 PMMLPortObjectSpec)) {
        throw new IllegalArgumentException("Invalid input data");
    }
    final PMMLPortObjectSpec pmmlSpec = (PMMLPortObjectSpec) modelObject;
    final DataTableSpec trainingSpec = pmmlSpec.getDataTableSpec();
    if (trainingSpec == null) {
        throw new InvalidSettingsException("No model spec available");
    }
    final List<DataColumnSpec> targetCols = pmmlSpec.getTargetCols();
    if (targetCols.size() != 1) {
        throw new InvalidSettingsException("No valid class column found");
    }
    final DataColumnSpec classColumn = targetCols.get(0);
    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);
    warningMessage("The following input columns are unknown and will be skipped: ", unknownCols);
    // check if the learned model contains columns which are not in the
    // input data
    final List<String> missingInputCols = check4MissingCols(trainingSpec, classColumn.getName(), spec);
    warningMessage("The following attributes are missing in the input data: ", missingInputCols);
    final PredictorHelper predictorHelper = PredictorHelper.getInstance();
    final DataColumnSpec resultColSpecs = NaiveBayesCellFactory.createResultColSpecs(predictorHelper.checkedComputePredictionColumnName(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 : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) 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) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 7 with PredictorHelper

use of org.knime.base.node.mine.util.PredictorHelper 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)

Example 8 with PredictorHelper

use of org.knime.base.node.mine.util.PredictorHelper 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)

Example 9 with PredictorHelper

use of org.knime.base.node.mine.util.PredictorHelper in project knime-core by knime.

the class NaiveBayesCellFactory method createResultColSpecs.

/**
 * Creates the column specification of the result columns and returns
 * them in the order they should be appended to the original table
 * specification.
 * @param model the {@link NaiveBayesModel} to use
 * @param columnName the name of the prediction column
 * @param inSpec the <code>DataTableSpec</code> of the input data to check
 * if the winner column name already exists
 * @param inclClassProbVals if the probability values should be displayed
 * @param suffix the suffix for the probability columns
 * @return <code>DataColumnSpec[]</code> with the column specifications
 * of the result columns
 */
public static DataColumnSpec[] createResultColSpecs(final NaiveBayesModel model, final String columnName, final DataTableSpec inSpec, final boolean inclClassProbVals, final String suffix) {
    final DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(columnName, model.getClassColumnDataType());
    final DataColumnSpec classColSpec = colSpecCreator.createSpec();
    if (!inclClassProbVals) {
        return new DataColumnSpec[] { classColSpec };
    }
    final List<String> classValues = model.getSortedClassValues();
    final Collection<DataColumnSpec> colSpecs = new ArrayList<>(classValues.size() + 1);
    colSpecCreator.setType(DoubleCell.TYPE);
    final PredictorHelper predictorHelper = PredictorHelper.getInstance();
    for (final String classVal : classValues) {
        colSpecCreator.setName(predictorHelper.probabilityColumnName(model.getClassColumnName(), classVal, suffix));
        colSpecs.add(colSpecCreator.createSpec());
    }
    colSpecs.add(classColSpec);
    return colSpecs.toArray(new DataColumnSpec[0]);
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) ArrayList(java.util.ArrayList)

Example 10 with PredictorHelper

use of org.knime.base.node.mine.util.PredictorHelper in project knime-core by knime.

the class SVMPredictorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    DataTableSpec testSpec = (DataTableSpec) inSpecs[1];
    PMMLPortObjectSpec trainingSpec = (PMMLPortObjectSpec) inSpecs[0];
    // try to find all columns (except the class column)
    Vector<Integer> colindices = new Vector<Integer>();
    for (DataColumnSpec colspec : trainingSpec.getLearningCols()) {
        if (colspec.getType().isCompatible(DoubleValue.class)) {
            int colindex = testSpec.findColumnIndex(colspec.getName());
            if (colindex < 0) {
                throw new InvalidSettingsException("Column " + "\'" + colspec.getName() + "\' not found" + " in test data");
            }
            colindices.add(colindex);
        }
    }
    final PredictorHelper predictorHelper = PredictorHelper.getInstance();
    return new DataTableSpec[] { predictorHelper.createOutTableSpec(testSpec, trainingSpec, m_addProbabilities.getBooleanValue(), m_predictionColumn.getStringValue(), m_overridePrediction.getBooleanValue(), m_suffix.getStringValue()) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Vector(java.util.Vector)

Aggregations

PredictorHelper (org.knime.base.node.mine.util.PredictorHelper)13 DataColumnSpec (org.knime.core.data.DataColumnSpec)10 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)8 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)6 DataTableSpec (org.knime.core.data.DataTableSpec)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)5 DataCell (org.knime.core.data.DataCell)4 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)4 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)3 DoubleCell (org.knime.core.data.def.DoubleCell)3 ArrayList (java.util.ArrayList)2 Vector (java.util.Vector)2 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)2 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)2 LinkedHashMap (java.util.LinkedHashMap)1 NaiveBayesModel (org.knime.base.node.mine.bayes.naivebayes.datamodel.NaiveBayesModel)1 NaiveBayesModel (org.knime.base.node.mine.bayes.naivebayes.datamodel2.NaiveBayesModel)1 PMMLNaiveBayesModelTranslator (org.knime.base.node.mine.bayes.naivebayes.datamodel2.PMMLNaiveBayesModelTranslator)1 NaiveBayesPortObject (org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObject)1 NaiveBayesPortObjectSpec (org.knime.base.node.mine.bayes.naivebayes.port.NaiveBayesPortObjectSpec)1