use of org.knime.base.node.mine.bayes.naivebayes.datamodel2.NaiveBayesModel in project knime-core by knime.
the class NaiveBayesModel method updateModel.
/**
* Updates the current {@link NaiveBayesModel} with the values from the
* given {@link DataRow}.
* @param row DataRow with values for update
* @param tableSpec underlying DataTableSpec
* @param classColIdx the index of the class column
* @throws InvalidSettingsException if missing values occur in class column
* or an attribute has too many values.
*/
public void updateModel(final DataRow row, final DataTableSpec tableSpec, final int classColIdx) throws InvalidSettingsException {
if (row == null) {
throw new NullPointerException("Row must not be null");
}
if (tableSpec == null) {
throw new NullPointerException("TableSpec must not be null");
}
final DataCell classCell = row.getCell(classColIdx);
if (classCell.isMissing()) {
if (m_skipMissingVals) {
return;
}
// check if the class value is missing
throw new InvalidSettingsException("Missing class value found in row " + row.getKey() + " to skip missing values tick the box in the dialog");
}
final String classVal = classCell.toString();
final int numColumns = tableSpec.getNumColumns();
for (int i = 0; i < numColumns; i++) {
final AttributeModel model = m_modelByAttrName.get(tableSpec.getColumnSpec(i).getName());
if (model != null) {
final DataCell cell = row.getCell(i);
try {
model.addValue(classVal, cell);
} catch (final TooManyValuesException e) {
if (model instanceof ClassAttributeModel) {
throw new InvalidSettingsException("Class attribute has too many unique values. " + "To avoid this exception increase the " + "maximum number of allowed nominal " + "values in the node dialog");
}
// delete the model if it contains too many unique values
m_modelByAttrName.remove(model.getAttributeName());
model.setInvalidCause("Too many values");
m_skippedAttributes.add(model);
}
}
}
m_noOfRecs++;
}
use of org.knime.base.node.mine.bayes.naivebayes.datamodel2.NaiveBayesModel 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() };
}
use of org.knime.base.node.mine.bayes.naivebayes.datamodel2.NaiveBayesModel in project knime-core by knime.
the class NaiveBayesLearnerNodeModel2 method loadInternals.
/**
* {@inheritDoc}
*/
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException {
final File modelFile = new File(nodeInternDir, CFG_DATA);
final FileInputStream modelIn = new FileInputStream(modelFile);
// because the loadFromXML method returns the content of the root tag
// we don't need to ask for the content of the root tag
final ModelContentRO myModel = ModelContent.loadFromXML(modelIn);
try {
m_model = new NaiveBayesModel(myModel);
} catch (final Exception e) {
throw new IOException(e.getMessage());
}
}
use of org.knime.base.node.mine.bayes.naivebayes.datamodel2.NaiveBayesModel 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;
}
Aggregations