Search in sources :

Example 11 with PredictorHelper

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

the class DecTreePredictorNodeModel method createOutTableSpec.

private DataTableSpec createOutTableSpec(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    List<DataCell> predValues = null;
    if (m_showDistribution.getBooleanValue()) {
        predValues = getPredictionValues((PMMLPortObjectSpec) inSpecs[INMODELPORT]);
        if (predValues == null) {
            // no out spec can be determined
            return null;
        }
    }
    int numCols = (predValues == null ? 0 : predValues.size()) + 1;
    DataTableSpec inSpec = (DataTableSpec) inSpecs[INDATAPORT];
    DataColumnSpec[] newCols = new DataColumnSpec[numCols];
    /* Set bar renderer and domain [0,1] as default for the double cells
         * containing the distribution */
    // DataColumnProperties propsRendering = new DataColumnProperties(
    // Collections.singletonMap(
    // DataValueRenderer.PROPERTY_PREFERRED_RENDERER,
    // DoubleBarRenderer.DESCRIPTION));
    DataColumnDomain domain = new DataColumnDomainCreator(new DoubleCell(0.0), new DoubleCell(1.0)).createDomain();
    PredictorHelper predictorHelper = PredictorHelper.getInstance();
    String trainingColumnName = ((PMMLPortObjectSpec) inSpecs[INMODELPORT]).getTargetFields().iterator().next();
    // add all distribution columns
    for (int i = 0; i < numCols - 1; i++) {
        assert predValues != null;
        DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(predictorHelper.probabilityColumnName(trainingColumnName, predValues.get(i).toString(), m_probabilitySuffix.getStringValue()), DoubleCell.TYPE);
        // colSpecCreator.setProperties(propsRendering);
        colSpecCreator.setDomain(domain);
        newCols[i] = colSpecCreator.createSpec();
    }
    // add the prediction column
    String predictionColumnName = predictorHelper.computePredictionColumnName(m_predictionColumn.getStringValue(), m_overridePrediction.getBooleanValue(), trainingColumnName);
    newCols[numCols - 1] = new DataColumnSpecCreator(predictionColumnName, StringCell.TYPE).createSpec();
    DataTableSpec newColSpec = new DataTableSpec(newCols);
    return new DataTableSpec(inSpec, newColSpec);
}
Also used : PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) DataTableSpec(org.knime.core.data.DataTableSpec) PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnDomain(org.knime.core.data.DataColumnDomain) DataCell(org.knime.core.data.DataCell)

Example 12 with PredictorHelper

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

the class BasisFunctionPredictor2CellFactory method predict.

/**
 * Predicts an unknown row to the given model.
 * @param row the row to predict
 * @param model a list of rules
 * @return mapping class label to array of assigned class degrees
 */
protected DataCell[] predict(final DataRow row, final Map<DataCell, List<BasisFunctionPredictorRow>> model) {
    // maps class to activation
    Map<DataCell, Double> map = new LinkedHashMap<DataCell, Double>();
    // overall basisfunctions in the model
    for (DataCell key : model.keySet()) {
        for (BasisFunctionPredictorRow bf : model.get(key)) {
            DataCell classInfo = bf.getClassLabel();
            double act;
            if (map.containsKey(classInfo)) {
                act = bf.compose(row, map.get(classInfo));
            } else {
                act = bf.compose(row, 0.0);
            }
            map.put(classInfo, act);
        }
    }
    // hash column specs
    DataTableSpec hash = new DataTableSpec(m_specs);
    // find best class activation index
    DataCell best = DataType.getMissingCell();
    // set default highest activation, not yet set
    double hact = -1.0;
    double sumAct = 0.0;
    Double[] act = new Double[m_specs.length];
    PredictorHelper predictorHelper = PredictorHelper.getInstance();
    for (DataCell cell : map.keySet()) {
        Double d = map.get(cell);
        if (d > hact || (d == hact && best.isMissing())) {
            hact = d;
            best = cell;
        }
        String colName = predictorHelper.probabilityColumnName(m_trainingColumnName, cell.toString(), m_suffix);
        int idx = hash.findColumnIndex(colName);
        if (idx >= 0) {
            act[idx] = d;
            sumAct += d;
        }
    }
    // all class values
    DataCell[] res = new DataCell[act.length];
    // skip last column which is the winner
    for (int i = 0; i < res.length - 1; i++) {
        if (act[i] == null) {
            res[i] = new DoubleCell(0.0);
        } else {
            if (m_normClass && sumAct > 0) {
                res[i] = new DoubleCell(act[i] / sumAct);
            } else {
                res[i] = new DoubleCell(act[i]);
            }
        }
    }
    // insert class label
    if (hact == 0.0 || hact < m_dontKnowClass) {
        res[res.length - 1] = DataType.getMissingCell();
    } else {
        res[res.length - 1] = best;
    }
    return res;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) DoubleCell(org.knime.core.data.def.DoubleCell) LinkedHashMap(java.util.LinkedHashMap) DataCell(org.knime.core.data.DataCell)

Example 13 with PredictorHelper

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

the class BasisFunctionPredictor2NodeModel method createSpec.

/**
 * Creates the output model spec.
 * @param dataSpec input data spec
 * @param modelSpec input model spec
 * @param modelClassIdx index with reflects the class column
 * @return the new model spec
 */
public final DataColumnSpec[] createSpec(final DataTableSpec dataSpec, final DataTableSpec modelSpec, final int modelClassIdx) {
    DataColumnSpec trainingClass = modelSpec.getColumnSpec(modelClassIdx);
    Set<DataCell> possClasses = trainingClass.getDomain().getValues();
    final PredictorHelper predictorHelper = PredictorHelper.getInstance();
    String trainingClassName = trainingClass.getName();
    final DataColumnSpec[] specs;
    if (possClasses == null) {
        if (m_appendClassProps) {
            return null;
        } else {
            specs = new DataColumnSpec[1];
        }
    } else {
        specs = new DataColumnSpec[possClasses.size() + 1];
        Iterator<DataCell> it = possClasses.iterator();
        for (int i = 0; i < possClasses.size(); i++) {
            String colName = predictorHelper.probabilityColumnName(trainingClassName, it.next().toString(), m_suffix.getStringValue());
            specs[i] = new DataColumnSpecCreator(colName, DoubleCell.TYPE).createSpec();
        }
    }
    DataColumnSpecCreator newTargetSpec = new DataColumnSpecCreator(trainingClass);
    String applyColumn = predictorHelper.computePredictionColumnName(m_predictionColumn.getStringValue(), m_overridePrediction.getBooleanValue(), trainingClassName);
    newTargetSpec.setName(applyColumn);
    specs[specs.length - 1] = newTargetSpec.createSpec();
    return specs;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) PredictorHelper(org.knime.base.node.mine.util.PredictorHelper) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataCell(org.knime.core.data.DataCell) 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