Search in sources :

Example 1 with PredictorHelper

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;
}
Also used : PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataCell(org.knime.core.data.DataCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 2 with PredictorHelper

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() };
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) DoubleCell(org.knime.core.data.def.DoubleCell) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator)

Example 3 with PredictorHelper

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;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) PMMLSVMTranslator(org.knime.base.node.mine.svm.PMMLSVMTranslator) Node(org.w3c.dom.Node) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) Svm(org.knime.base.node.mine.svm.Svm) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Vector(java.util.Vector)

Example 4 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 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]);
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) ArrayList(java.util.ArrayList)

Example 5 with PredictorHelper

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;
}
Also used : PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) PMMLNaiveBayesModelTranslator(org.knime.base.node.mine.bayes.naivebayes.datamodel2.PMMLNaiveBayesModelTranslator) NaiveBayesModel(org.knime.base.node.mine.bayes.naivebayes.datamodel2.NaiveBayesModel) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

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