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;
}
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;
}
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 };
}
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]);
}
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()) };
}
Aggregations