use of org.knime.base.node.mine.util.PredictorHelper in project knime-core by knime.
the class SotaPredictorNodeModel method createOutColSpecs.
/**
* @param sotaSpec
* @return
* @throws InvalidSettingsException
*/
private DataColumnSpec[] createOutColSpecs(final SotaPortObjectSpec sotaSpec) throws InvalidSettingsException {
final PredictorHelper ph = PredictorHelper.getInstance();
final DataColumnSpec classColumnSpec = sotaSpec.getClassColumnSpec();
String trainingColumnName = classColumnSpec == null ? "No class" : classColumnSpec.getName();
final String predColName = ph.checkedComputePredictionColumnName(m_customPrediction.getStringValue(), m_changePrediction.getBooleanValue(), trainingColumnName);
final DataColumnSpec[] colSpecs;
if (m_appendProbs.getBooleanValue() && sotaSpec.hasClassColumn()) {
assert classColumnSpec != null;
@SuppressWarnings("null") Set<DataCell> values = classColumnSpec.getDomain().getValues();
if (values != null) {
colSpecs = new DataColumnSpec[values.size() + 1];
int idx = 0;
for (DataCell dataCell : values) {
colSpecs[idx++] = new DataColumnSpecCreator(ph.probabilityColumnName(trainingColumnName, dataCell.toString(), m_probSuffix.getStringValue()), DoubleCell.TYPE).createSpec();
}
} else {
colSpecs = new DataColumnSpec[1];
}
} else {
colSpecs = new DataColumnSpec[1];
}
colSpecs[colSpecs.length - 1] = new DataColumnSpecCreator(predColName, StringCell.TYPE).createSpec();
return colSpecs;
}
use of org.knime.base.node.mine.util.PredictorHelper in project knime-core by knime.
the class SVMPredictor method getColumnSpecs.
/**
* {@inheritDoc}
*/
@Override
public DataColumnSpec[] getColumnSpecs() {
DataColumnSpecCreator colspeccreator = new DataColumnSpecCreator(m_predictionColumnName, StringCell.TYPE);
if (m_appendProbabilities) {
final DataColumnSpec[] ret = new DataColumnSpec[m_svms.length + 1];
PredictorHelper ph = PredictorHelper.getInstance();
final DataColumnSpecCreator creator = new DataColumnSpecCreator("Dummy", DoubleCell.TYPE);
creator.setDomain(new DataColumnDomainCreator(new DoubleCell(0), new DoubleCell(1)).createDomain());
for (int i = m_svms.length; i-- > 0; ) {
String name = ph.probabilityColumnName(m_trainingColumn, m_svms[i].getPositive(), m_suffix);
creator.setName(name);
ret[i] = creator.createSpec();
}
ret[m_svms.length] = colspeccreator.createSpec();
return ret;
}
return new DataColumnSpec[] { colspeccreator.createSpec() };
}
use of org.knime.base.node.mine.util.PredictorHelper in project knime-core by knime.
the class SVMPredictorNodeModel method createColumnRearranger.
private ColumnRearranger createColumnRearranger(final PMMLPortObject pmmlModel, final DataTableSpec inSpec) throws InvalidSettingsException {
List<Node> models = pmmlModel.getPMMLValue().getModels(PMMLModelType.SupportVectorMachineModel);
if (models.isEmpty()) {
String msg = "SVM evaluation failed: " + "No support vector machine model found.";
LOGGER.error(msg);
throw new RuntimeException(msg);
}
PMMLSVMTranslator trans = new PMMLSVMTranslator();
pmmlModel.initializeModelTranslator(trans);
List<Svm> svms = trans.getSVMs();
m_svms = svms.toArray(new Svm[svms.size()]);
if (m_addProbabilities.getBooleanValue() == pmmlModel.getSpec().getTargetCols().size() > 0) {
adjustOrder(pmmlModel.getSpec().getTargetCols().get(0));
}
DataTableSpec testSpec = inSpec;
PMMLPortObjectSpec pmmlSpec = pmmlModel.getSpec();
DataTableSpec trainingSpec = pmmlSpec.getDataTableSpec();
// try to find all columns (except the class column)
Vector<Integer> colindices = new Vector<Integer>();
for (DataColumnSpec colspec : trainingSpec) {
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);
}
}
m_colindices = new int[colindices.size()];
for (int i = 0; i < m_colindices.length; i++) {
m_colindices[i] = colindices.get(i);
}
final PredictorHelper predictorHelper = PredictorHelper.getInstance();
final String targetCol = pmmlSpec.getTargetFields().iterator().next();
SVMPredictor svmpredict = new SVMPredictor(targetCol, m_svms, m_colindices, predictorHelper.computePredictionColumnName(m_predictionColumn.getStringValue(), m_overridePrediction.getBooleanValue(), targetCol), m_addProbabilities.getBooleanValue(), m_suffix.getStringValue());
ColumnRearranger colre = new ColumnRearranger(testSpec);
colre.append(svmpredict);
return colre;
}
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 predictionColName 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
*/
private static DataColumnSpec[] createResultColSpecs(final NaiveBayesModel model, final String predictionColName, final DataTableSpec inSpec, final boolean inclClassProbVals, final String suffix) {
final DataColumnSpec classColSpec = createPredictedClassColSpec(predictionColName, model.getClassColumnDataType(), inSpec);
if (!inclClassProbVals) {
return new DataColumnSpec[] { classColSpec };
}
final List<String> classValues = model.getSortedClassValues();
final Collection<DataColumnSpec> colSpecs = new ArrayList<>(classValues.size() + 1);
final DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator("dummy", 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 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