Search in sources :

Example 46 with DoubleValue

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

the class PolyRegLearnerNodeModel method getCellFactory.

private CellFactory getCellFactory(final int dependentIndex) {
    final int degree = m_settings.getDegree();
    return new CellFactory() {

        @Override
        public DataCell[] getCells(final DataRow row) {
            double sum = m_betas[0];
            int betaCount = 1;
            double y = 0;
            for (int col = 0; col < row.getNumCells(); col++) {
                if ((col == dependentIndex || m_colSelected[col]) && row.getCell(col).isMissing()) {
                    switch(m_settings.getMissingValueHandling()) {
                        case ignore:
                            return new DataCell[] { DataType.getMissingCell(), DataType.getMissingCell() };
                        case fail:
                            throw new IllegalStateException("Should failed earlier!");
                        default:
                            throw new UnsupportedOperationException("Not supported missing handling strategy: " + m_settings.getMissingValueHandling());
                    }
                }
                if ((col != dependentIndex) && m_colSelected[col]) {
                    final double value = ((DoubleValue) row.getCell(col)).getDoubleValue();
                    double poly = 1;
                    for (int d = 1; d <= degree; d++) {
                        poly *= value;
                        sum += m_betas[betaCount++] * poly;
                    }
                } else if (col == dependentIndex) {
                    y = ((DoubleValue) row.getCell(col)).getDoubleValue();
                }
            }
            double err = Math.abs(sum - y);
            m_squaredError += err * err;
            return new DataCell[] { new DoubleCell(sum), new DoubleCell(err) };
        }

        @Override
        public DataColumnSpec[] getColumnSpecs() {
            DataColumnSpecCreator crea = new DataColumnSpecCreator("PolyReg prediction", DoubleCell.TYPE);
            DataColumnSpec col1 = crea.createSpec();
            crea = new DataColumnSpecCreator("Prediction Error", DoubleCell.TYPE);
            DataColumnSpec col2 = crea.createSpec();
            return new DataColumnSpec[] { col1, col2 };
        }

        @Override
        public void setProgress(final int curRowNr, final int rowCount, final RowKey lastKey, final ExecutionMonitor execMon) {
        // do nothing
        }
    };
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) CellFactory(org.knime.core.data.container.CellFactory)

Example 47 with DoubleValue

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

the class FuzzyBasisFunctionPredictorRow method computeActivation.

/**
 * Returns the compute activation of this input vector.
 *
 * @param row input pattern
 * @return membership degree
 */
@Override
public double computeActivation(final DataRow row) {
    assert (m_mem.length == row.getNumCells());
    // sets degree to maximum
    double degree = 1.0;
    // overall cells in the vector
    for (int i = 0; i < m_mem.length; i++) {
        DataCell cell = row.getCell(i);
        if (cell.isMissing()) {
            continue;
        }
        // gets cell at index i
        double value = ((DoubleValue) cell).getDoubleValue();
        // act in current dimension
        double act = m_mem[i].getActivation(value);
        if (i == 0) {
            degree = act;
            continue;
        }
        // calculates the new (minimum) degree using norm index
        degree = Norm.NORMS[m_norm].computeTNorm(degree, act);
        assert (0.0 <= degree && degree <= 1.0);
    }
    // returns membership degree
    return degree;
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell)

Example 48 with DoubleValue

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

the class MinkowskiDist method calcDistance.

/**
 * Calculates the distance between two data rows based on the
 * Minkowski distance.
 *
 * @param firstDataRow the first data row used to calculate the distance
 * @param secondDataRow the second data row used to calculate the distance
 * @param includedCols the columns to include into the calculation
 *
 * @return the distance of the two rows
 */
public double calcDistance(final DataRow firstDataRow, final DataRow secondDataRow, final int[] includedCols) {
    double sumPowDist = 0;
    for (int i : includedCols) {
        // skip not included cells
        DataCell x = firstDataRow.getCell(i);
        DataCell y = secondDataRow.getCell(i);
        if (!x.isMissing() && x instanceof DoubleValue && !y.isMissing() && y instanceof DoubleValue) {
            DoubleValue clusterValue = (DoubleValue) firstDataRow.getCell(i);
            DoubleValue rowValue = (DoubleValue) secondDataRow.getCell(i);
            double dist = Math.abs(clusterValue.getDoubleValue() - rowValue.getDoubleValue());
            sumPowDist += Math.pow(dist, m_p);
        }
    }
    return Math.pow(sumPowDist, (double) 1 / (double) m_p);
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell)

Example 49 with DoubleValue

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

the class ClusterNodeModel method findClosestPrototypeFor.

private int findClosestPrototypeFor(final DataRow row, final double[][] clusters) {
    // find closest cluster center
    // closest cluster so far
    int winner = -1;
    // best distance
    double winnerDistance = Double.MAX_VALUE;
    for (int c = 0; c < m_nrOfClusters.getIntValue(); c++) {
        double distance = 0.0;
        int pos = 0;
        for (int i = 0; i < m_dimension; i++) {
            DataCell currentCell = row.getCell(i);
            if (!m_ignoreColumn[i]) {
                if (!currentCell.isMissing()) {
                    assert currentCell.getType().isCompatible(DoubleValue.class);
                    double d = (clusters[c][pos] - ((DoubleValue) (currentCell)).getDoubleValue());
                    if (!Double.isNaN(d)) {
                        distance += d * d;
                    }
                } else {
                    // missing
                    distance += 0.0;
                }
                pos++;
            }
        }
        if (distance < winnerDistance) {
            // found closer cluster
            // make it new winner
            winner = c;
            winnerDistance = distance;
        }
    }
    // for all clusters (find closest one)
    return winner;
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell)

Example 50 with DoubleValue

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

the class PMMLSimpleSetPredicate method evaluate.

/**
 * {@inheritDoc}
 */
@Override
public Boolean evaluate(final DataRow row, final DataTableSpec spec) {
    cacheSpec(spec);
    assert getPreviousIndex() != -1;
    DataCell cell = row.getCell(getPreviousIndex());
    if (cell.isMissing()) {
        return null;
    }
    if (m_arrayType == PMMLArrayType.STRING) {
        return m_op.evaluate(cell.toString(), m_values);
    } else {
        Double a = ((DoubleValue) cell).getDoubleValue();
        return m_op.evaluate(a, m_doubleValues);
    }
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell)

Aggregations

DoubleValue (org.knime.core.data.DoubleValue)154 DataCell (org.knime.core.data.DataCell)103 DataRow (org.knime.core.data.DataRow)71 DataColumnSpec (org.knime.core.data.DataColumnSpec)38 DataTableSpec (org.knime.core.data.DataTableSpec)38 DoubleCell (org.knime.core.data.def.DoubleCell)32 ArrayList (java.util.ArrayList)26 BufferedDataTable (org.knime.core.node.BufferedDataTable)26 DataType (org.knime.core.data.DataType)23 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)21 LinkedHashMap (java.util.LinkedHashMap)18 IntValue (org.knime.core.data.IntValue)15 HashMap (java.util.HashMap)14 RowIterator (org.knime.core.data.RowIterator)14 RowKey (org.knime.core.data.RowKey)13 DefaultRow (org.knime.core.data.def.DefaultRow)13 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 LongValue (org.knime.core.data.LongValue)10 StringValue (org.knime.core.data.StringValue)10 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)10