Search in sources :

Example 76 with DataCell

use of org.knime.core.data.DataCell in project knime-core by knime.

the class SVMPredictor method getCells.

/**
 * {@inheritDoc}
 */
@Override
public DataCell[] getCells(final DataRow row) {
    ArrayList<Double> values = new ArrayList<Double>();
    for (int i = 0; i < m_colindices.length; i++) {
        if (row.getCell(m_colindices[i]).isMissing()) {
            return new DataCell[] { DataType.getMissingCell() };
        }
        DoubleValue dv = (DoubleValue) row.getCell(m_colindices[i]);
        values.add(dv.getDoubleValue());
    }
    String classvalue = doPredict(values);
    return new DataCell[] { new StringCell(classvalue) };
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) StringCell(org.knime.core.data.def.StringCell) ArrayList(java.util.ArrayList) DataCell(org.knime.core.data.DataCell)

Example 77 with DataCell

use of org.knime.core.data.DataCell in project knime-core by knime.

the class AutoBinner method createEdgesFromQuantiles.

private static double[] createEdgesFromQuantiles(final BufferedDataTable data, final ExecutionContext exec, final double[] sampleQuantiles) throws CanceledExecutionException {
    double[] edges = new double[sampleQuantiles.length];
    long n = data.size();
    long c = 0;
    int cc = 0;
    RowIterator iter = data.iterator();
    DataRow rowQ = null;
    DataRow rowQ1 = null;
    if (iter.hasNext()) {
        rowQ1 = iter.next();
        rowQ = rowQ1;
    }
    for (double p : sampleQuantiles) {
        double h = (n - 1) * p + 1;
        int q = (int) Math.floor(h);
        while ((1.0 == p || c < q) && iter.hasNext()) {
            rowQ = rowQ1;
            rowQ1 = iter.next();
            c++;
            exec.setProgress(c / (double) n);
            exec.checkCanceled();
        }
        rowQ = 1.0 != p ? rowQ : rowQ1;
        final DataCell xqCell = rowQ.getCell(0);
        final DataCell xq1Cell = rowQ1.getCell(0);
        // data first?)
        if (xqCell.isMissing() || xq1Cell.isMissing()) {
            throw new RuntimeException("Missing values not support for " + "quantile calculation (error in row \"" + rowQ1.getKey() + "\")");
        }
        // for quantile calculation see also
        // http://en.wikipedia.org/wiki/
        // Quantile#Estimating_the_quantiles_of_a_population.
        // this implements R-7
        double xq = ((DoubleValue) xqCell).getDoubleValue();
        double xq1 = ((DoubleValue) xq1Cell).getDoubleValue();
        double quantile = xq + (h - q) * (xq1 - xq);
        edges[cc] = quantile;
        cc++;
    }
    return edges;
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow)

Example 78 with DataCell

use of org.knime.core.data.DataCell in project knime-core by knime.

the class ColCombineNodeModel method createColumnRearranger.

private ColumnRearranger createColumnRearranger(final DataTableSpec spec) {
    ColumnRearranger result = new ColumnRearranger(spec);
    DataColumnSpec append = new DataColumnSpecCreator(m_newColName, StringCell.TYPE).createSpec();
    final int[] indices = new int[m_columns.length];
    List<String> colNames = Arrays.asList(m_columns);
    int j = 0;
    for (int k = 0; k < spec.getNumColumns(); k++) {
        DataColumnSpec cs = spec.getColumnSpec(k);
        if (colNames.contains(cs.getName())) {
            indices[j] = k;
            j++;
        }
    }
    // ", " -> ","
    // "  " -> "  " (do not let the resulting string be empty)
    // " bla bla " -> "bla bla"
    final String delimTrim = trimDelimString(m_delimString);
    result.append(new SingleCellFactory(append) {

        @Override
        public DataCell getCell(final DataRow row) {
            String[] cellContents = new String[indices.length];
            for (int i = 0; i < indices.length; i++) {
                DataCell c = row.getCell(indices[i]);
                String s = c instanceof StringValue ? ((StringValue) c).getStringValue() : c.toString();
                cellContents[i] = s;
            }
            return new StringCell(handleContent(cellContents, delimTrim));
        }
    });
    return result;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataRow(org.knime.core.data.DataRow) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) StringValue(org.knime.core.data.StringValue) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 79 with DataCell

use of org.knime.core.data.DataCell in project knime-core by knime.

the class AutoBinner method createEdgesFromQuantiles.

@SuppressWarnings("null")
private static double[] createEdgesFromQuantiles(final BufferedDataTable data, final ExecutionContext exec, final double[] sampleQuantiles) throws CanceledExecutionException {
    double[] edges = new double[sampleQuantiles.length];
    long n = data.size();
    long c = 0;
    int cc = 0;
    RowIterator iter = data.iterator();
    DataRow rowQ = null;
    DataRow rowQ1 = null;
    if (iter.hasNext()) {
        rowQ1 = iter.next();
        rowQ = rowQ1;
    }
    for (double p : sampleQuantiles) {
        double h = (n - 1) * p + 1;
        int q = (int) Math.floor(h);
        while ((1.0 == p || c < q) && iter.hasNext()) {
            rowQ = rowQ1;
            rowQ1 = iter.next();
            c++;
            exec.setProgress(c / (double) n);
            exec.checkCanceled();
        }
        rowQ = 1.0 != p ? rowQ : rowQ1;
        final DataCell xqCell = rowQ.getCell(0);
        final DataCell xq1Cell = rowQ1.getCell(0);
        // data first?)
        if (xqCell.isMissing() || xq1Cell.isMissing()) {
            throw new RuntimeException("Missing values not support for " + "quantile calculation (error in row \"" + rowQ1.getKey() + "\")");
        }
        // for quantile calculation see also
        // http://en.wikipedia.org/wiki/
        // Quantile#Estimating_the_quantiles_of_a_population.
        // this implements R-7
        double xq = ((DoubleValue) xqCell).getDoubleValue();
        double xq1 = ((DoubleValue) xq1Cell).getDoubleValue();
        double quantile = xq + (h - q) * (xq1 - xq);
        edges[cc] = quantile;
        cc++;
    }
    return edges;
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow)

Example 80 with DataCell

use of org.knime.core.data.DataCell in project knime-core by knime.

the class TreeEnsembleClassificationPredictorCellFactory method getCells.

/**
 * {@inheritDoc}
 */
@Override
public DataCell[] getCells(final DataRow row) {
    TreeEnsembleModelPortObject modelObject = m_predictor.getModelObject();
    TreeEnsemblePredictorConfiguration cfg = m_predictor.getConfiguration();
    final TreeEnsembleModel ensembleModel = modelObject.getEnsembleModel();
    int size = 1;
    final boolean appendConfidence = cfg.isAppendPredictionConfidence();
    if (appendConfidence) {
        size += 1;
    }
    final boolean appendClassConfidences = cfg.isAppendClassConfidences();
    if (appendClassConfidences) {
        size += m_targetValueMap.size();
    }
    final boolean appendModelCount = cfg.isAppendModelCount();
    if (appendModelCount) {
        size += 1;
    }
    final boolean hasOutOfBagFilter = m_predictor.hasOutOfBagFilter();
    DataCell[] result = new DataCell[size];
    DataRow filterRow = new FilterColumnRow(row, m_learnColumnInRealDataIndices);
    PredictorRecord record = ensembleModel.createPredictorRecord(filterRow, m_learnSpec);
    if (record == null) {
        // missing value
        Arrays.fill(result, DataType.getMissingCell());
        return result;
    }
    final Voting voting = m_votingFactory.createVoting();
    final int nrModels = ensembleModel.getNrModels();
    int nrValidModels = 0;
    for (int i = 0; i < nrModels; i++) {
        if (hasOutOfBagFilter && m_predictor.isRowPartOfTrainingData(row.getKey(), i)) {
        // ignore, row was used to train the model
        } else {
            TreeModelClassification m = ensembleModel.getTreeModelClassification(i);
            TreeNodeClassification match = m.findMatchingNode(record);
            voting.addVote(match);
            nrValidModels += 1;
        }
    }
    final NominalValueRepresentation[] targetVals = ((TreeTargetNominalColumnMetaData) ensembleModel.getMetaData().getTargetMetaData()).getValues();
    String majorityClass = voting.getMajorityClass();
    int index = 0;
    if (majorityClass == null) {
        assert nrValidModels == 0;
        Arrays.fill(result, DataType.getMissingCell());
        index = size - 1;
    } else {
        result[index++] = m_targetValueMap.get(majorityClass);
        // final float[] distribution = voting.getClassProbabilities();
        if (appendConfidence) {
            result[index++] = new DoubleCell(voting.getClassProbabilityForClass(majorityClass));
        }
        if (appendClassConfidences) {
            for (String targetValue : m_targetValueMap.keySet()) {
                result[index++] = new DoubleCell(voting.getClassProbabilityForClass(targetValue));
            }
        }
    }
    if (appendModelCount) {
        result[index++] = new IntCell(voting.getNrVotes());
    }
    return result;
}
Also used : TreeNodeClassification(org.knime.base.node.mine.treeensemble2.model.TreeNodeClassification) TreeEnsembleModel(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModel) TreeTargetNominalColumnMetaData(org.knime.base.node.mine.treeensemble2.data.TreeTargetNominalColumnMetaData) DoubleCell(org.knime.core.data.def.DoubleCell) TreeEnsemblePredictorConfiguration(org.knime.base.node.mine.treeensemble2.node.predictor.TreeEnsemblePredictorConfiguration) NominalValueRepresentation(org.knime.base.node.mine.treeensemble2.data.NominalValueRepresentation) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) TreeEnsembleModelPortObject(org.knime.base.node.mine.treeensemble2.model.TreeEnsembleModelPortObject) PredictorRecord(org.knime.base.node.mine.treeensemble2.data.PredictorRecord) DataCell(org.knime.core.data.DataCell) FilterColumnRow(org.knime.base.data.filter.column.FilterColumnRow) TreeModelClassification(org.knime.base.node.mine.treeensemble2.model.TreeModelClassification)

Aggregations

DataCell (org.knime.core.data.DataCell)780 DataRow (org.knime.core.data.DataRow)268 DataTableSpec (org.knime.core.data.DataTableSpec)175 DataColumnSpec (org.knime.core.data.DataColumnSpec)170 DefaultRow (org.knime.core.data.def.DefaultRow)169 ArrayList (java.util.ArrayList)141 StringCell (org.knime.core.data.def.StringCell)131 DoubleCell (org.knime.core.data.def.DoubleCell)129 DoubleValue (org.knime.core.data.DoubleValue)111 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)109 DataType (org.knime.core.data.DataType)97 RowKey (org.knime.core.data.RowKey)94 BufferedDataTable (org.knime.core.node.BufferedDataTable)93 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)91 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)84 LinkedHashMap (java.util.LinkedHashMap)81 IntCell (org.knime.core.data.def.IntCell)79 HashMap (java.util.HashMap)60 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)57 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)56