Search in sources :

Example 41 with DoubleValue

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

the class BasisFunctionLearnerNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
public PortObjectSpec[] configure(final PortObjectSpec[] ins) throws InvalidSettingsException {
    DataTableSpec inSpec = (DataTableSpec) ins[0];
    // check if target column available
    if (m_targetColumns == null) {
        // find first non-numeric column
        for (int i = 0; i < inSpec.getNumColumns(); i++) {
            DataColumnSpec cspec = inSpec.getColumnSpec(i);
            if (!cspec.getType().isCompatible(DoubleValue.class)) {
                m_targetColumns = new String[] { cspec.getName() };
                super.setWarningMessage("Target column guessed as \"" + cspec.getName() + "\"");
                break;
            }
        }
        if (m_targetColumns == null) {
            throw new InvalidSettingsException("Target columns not available.");
        }
    } else {
        for (String target : m_targetColumns) {
            if (!inSpec.containsName(target)) {
                throw new InvalidSettingsException("Target \"" + target + "\" column not available.");
            }
            if (m_targetColumns.length > 1) {
                if (!inSpec.getColumnSpec(target).getType().isCompatible(DoubleValue.class)) {
                    throw new InvalidSettingsException("Target \"" + target + "\" column not of type DoubleValue.");
                }
            }
        }
    }
    // check if double type column available
    if (!inSpec.containsCompatibleType(DoubleValue.class)) {
        throw new InvalidSettingsException("No data column of type DoubleValue found.");
    }
    List<String> targetHash = Arrays.asList(m_targetColumns);
    // if only one double type column, check if not the target column
    for (int i = 0; i < inSpec.getNumColumns(); i++) {
        DataColumnSpec cspec = inSpec.getColumnSpec(i);
        if (cspec.getType().isCompatible(DoubleValue.class)) {
            if (!targetHash.contains(cspec.getName())) {
                break;
            }
        }
        // if last column was tested
        if (i + 1 == inSpec.getNumColumns()) {
            throw new InvalidSettingsException("Found only one column of" + " type DoubleValue: " + Arrays.toString(m_targetColumns));
        }
    }
    // if no data columns are found, use all numeric columns
    String[] dataCols = BasisFunctionFactory.findDataColumns(inSpec, targetHash);
    DataTableSpec modelSpec = BasisFunctionFactory.createModelSpec(inSpec, dataCols, m_targetColumns, getModelType());
    return new DataTableSpec[] { modelSpec, modelSpec };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) DoubleValue(org.knime.core.data.DoubleValue) InvalidSettingsException(org.knime.core.node.InvalidSettingsException)

Example 42 with DoubleValue

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

the class Distance method compute.

/**
 * Computes the Euclidean distance between two normalized vectors.
 *
 * @param x an array of double cells
 * @param y a row
 * @return the Euclidean distance between <code>x</code> and
 *         <code>y</code>
 * @throws NullPointerException if one of the given rows is
 *             <code>null</code>
 */
public final double compute(final DoubleValue[] x, final DataRow y) {
    assert (x.length == y.getNumCells());
    final int length = x.length;
    double result = 0.0;
    for (int i = 0; i < length; i++) {
        DataCell yCell = y.getCell(i);
        if (yCell.isMissing()) {
            continue;
        }
        double xd = x[i].getDoubleValue();
        if (Double.isNaN(xd)) {
            continue;
        }
        double yd = ((DoubleValue) yCell).getDoubleValue();
        if (Double.isNaN(yd)) {
            continue;
        }
        double diff = xd - yd;
        result += (diff * diff);
    }
    assert result >= 0.0 : "result=" + result;
    return Math.sqrt(result);
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell)

Example 43 with DoubleValue

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

the class Distance method compute.

/**
 * Computes the Euclidean distance between two normalized rows.
 *
 * @param x a row
 * @param y another row
 * @return Euclidean distance between <code>x</code> and <code>y</code>.
 * @throws NullPointerException if one of the given rows is
 *             <code>null</code>
 */
public final double compute(final DataRow x, final DataRow y) {
    assert (x.getNumCells() == y.getNumCells());
    final int length = x.getNumCells();
    double result = 0.0;
    for (int i = 0; i < length; i++) {
        DataCell xCell = x.getCell(i);
        DataCell yCell = y.getCell(i);
        if (xCell.isMissing() || yCell.isMissing()) {
            continue;
        }
        double xd = ((DoubleValue) xCell).getDoubleValue();
        if (Double.isNaN(xd)) {
            continue;
        }
        double yd = ((DoubleValue) yCell).getDoubleValue();
        if (Double.isNaN(yd)) {
            continue;
        }
        double diff = xd - yd;
        result += (diff * diff);
    }
    assert result >= 0.0 : "result=" + result;
    return Math.sqrt(result);
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell)

Example 44 with DoubleValue

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

the class FuzzyBasisFunctionLearnerRow method shrinkIt.

/**
 * If <code>shrinkIt</code> is <code>true</code> the shrink will be
 * executed otherwise the shrink value is only returned.
 *
 * @param row the input pattern for shrinking @return 0 if no shrink needed
 * otherwise a value greater zero
 */
private double shrinkIt(final DataRow row, final boolean shrinkIt) {
    assert (m_predRow.getNrMemships() == row.getNumCells());
    // init support loss with maximum and index with dft -1
    double suppLoss = Double.MAX_VALUE;
    int suppK = -1;
    // init core loss with maximum and index with dft -1
    double coreLoss = Double.MAX_VALUE;
    int coreK = -1;
    // overall cells in the vector
    for (int i = 0; i < m_predRow.getNrMemships(); i++) {
        // gets current cell in the vector
        DataCell cell = row.getCell(i);
        if (cell.isMissing()) {
            continue;
        }
        // get cell value
        double value = ((DoubleValue) cell).getDoubleValue();
        // if missing dimension continue
        if (m_predRow.getMemship(i).isMissingIntern()) {
            continue;
        }
        // retrieve anchor value
        double a = m_predRow.getMemship(i).getAnchor();
        // if value on the left side of the anchor
        if (value < a) {
            // if left side is unconst. or value truly is in support region
            if (m_predRow.getMemship(i).isSuppLeftMax() || value > m_predRow.getMemship(i).getMinSupport()) {
                // if the value is in the core region
                if (value >= m_predRow.getMemship(i).getMinCore()) {
                    // gets the loss in the core region
                    double loss = Shrink.SHRINKS[m_shrink].leftCoreLoss(value, m_predRow.getMemship(i));
                    assert (0.0 <= loss && loss <= 1.0) : loss;
                    // if the new loss is less than the current
                    if (loss < coreLoss) {
                        coreLoss = loss;
                        coreK = i;
                    }
                } else {
                    // if value is not in the core
                    if (value >= m_predRow.getMemship(i).getMinSupport()) {
                        // gets loss in the support region
                        double loss = Shrink.SHRINKS[m_shrink].leftSuppLoss(value, m_predRow.getMemship(i));
                        assert (0 <= loss && loss <= 1.0) : loss;
                        // if new value less that the current value
                        if (loss < suppLoss) {
                            suppLoss = loss;
                            suppK = i;
                        }
                    }
                }
            } else {
                // return false, no shrink needed
                return 0.0;
            }
        } else {
            // id value is on the right side of the anchor
            if (value > a) {
                // value is in support region
                if (m_predRow.getMemship(i).isSuppRightMax() || value < m_predRow.getMemship(i).getMaxSupport()) {
                    // if the value is in the core region
                    if (value <= m_predRow.getMemship(i).getMaxCore()) {
                        // gets loss value
                        double loss = Shrink.SHRINKS[m_shrink].rightCoreLoss(value, m_predRow.getMemship(i));
                        assert (0.0 <= loss && loss <= 1.0) : loss;
                        // if the new loss is less than the current one
                        if (loss < coreLoss) {
                            coreLoss = loss;
                            coreK = i;
                        }
                    } else {
                        // if value is not in core region,
                        if (value <= m_predRow.getMemship(i).getMaxSupport()) {
                            double loss = Shrink.SHRINKS[m_shrink].rightSuppLoss(value, m_predRow.getMemship(i));
                            assert (0.0 <= loss && loss <= 1.0) : loss;
                            // if new loss is less than current
                            if (loss < suppLoss) {
                                suppLoss = loss;
                                suppK = i;
                            }
                        }
                    }
                } else {
                    // ... no shrink needed, returns false
                    return 0.0;
                }
            } else {
                // if value equal to the anchor
                assert (value == a);
                // no shrink possible, try next dimension
                continue;
            }
        }
    }
    assert suppK < 0 || (suppK >= 0 && 0.0 <= suppLoss && suppLoss <= 1.0);
    assert coreK < 0 || (coreK >= 0 && 0.0 <= coreLoss && coreLoss <= 1.0);
    // if shrink in the support region is possible and needed
    if (suppK > -1) {
        // get value to shrink support
        double value = ((DoubleValue) row.getCell(suppK)).getDoubleValue();
        // id value left of the anchor
        if (value < m_predRow.getMemship(suppK).getAnchor()) {
            if (shrinkIt) {
                // shrink support
                m_predRow.getMemship(suppK).setSuppLeft(value);
            }
            // shrink was made
            return suppLoss;
        } else {
            // if value is on the right side of the anchor
            if (value > m_predRow.getMemship(suppK).getAnchor()) {
                if (shrinkIt) {
                    // shrink support
                    m_predRow.getMemship(suppK).setSuppRight(value);
                }
                // shrink was made
                return suppLoss;
            } else {
                // conflict, all values are equal to the anchor
                assert (value == m_predRow.getMemship(suppK).getAnchor());
                // shrink not possible
                return 0.0;
            }
        }
    } else {
        // if shrink in the core regions is only possible
        if (coreK > -1) {
            // get value to shrink core
            double value = ((DoubleValue) row.getCell(coreK)).getDoubleValue();
            // if of the left anchor side
            if (value < m_predRow.getMemship(coreK).getAnchor()) {
                if (shrinkIt) {
                    // shrink left core and support
                    m_predRow.getMemship(coreK).setCoreLeft(value);
                    m_predRow.getMemship(coreK).setSuppLeft(value);
                }
                // shrink was made
                return coreLoss;
            } else {
                // if on the right anchor side
                if (value > m_predRow.getMemship(coreK).getAnchor()) {
                    if (shrinkIt) {
                        // shrink right core and support
                        m_predRow.getMemship(coreK).setCoreRight(value);
                        m_predRow.getMemship(coreK).setSuppRight(value);
                    }
                    // shrink was made
                    return coreLoss;
                } else {
                    // conflict, all values are equal to the anchor
                    assert (value == m_predRow.getMemship(coreK).getAnchor());
                    // shrink not possible
                    return 0.0;
                }
            }
        } else {
            assert (suppK == -1 && coreK == -1);
            return 0.0;
        }
    }
}
Also used : DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell)

Example 45 with DoubleValue

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

the class LinReg2Predictor method getCells.

/**
 * {@inheritDoc}
 */
@Override
public DataCell[] getCells(final DataRow row) {
    if (hasMissingValues(row)) {
        return createMissingOutput();
    }
    DataCell[] cells = new DataCell[1];
    // column vector
    RealMatrix x = MatrixUtils.createRealMatrix(1, m_parameters.size());
    for (int i = 0; i < m_parameters.size(); i++) {
        String parameter = m_parameters.get(i);
        String predictor = null;
        String value = null;
        boolean rowIsEmpty = true;
        for (Iterator<String> iter = m_predictors.iterator(); iter.hasNext(); ) {
            predictor = iter.next();
            value = m_ppMatrix.getValue(parameter, predictor, null);
            if (null != value) {
                rowIsEmpty = false;
                break;
            }
        }
        if (rowIsEmpty) {
            x.setEntry(0, i, 1);
        } else {
            if (m_factors.contains(predictor)) {
                List<DataCell> values = m_values.get(predictor);
                DataCell cell = row.getCell(m_parameterI.get(parameter));
                int index = values.indexOf(cell);
                /* When building a general regression model, for each
                    categorical fields, there is one category used as the
                    default baseline and therefore it didn't show in the
                    ParameterList in PMML. This design for the training is fine,
                    but in the prediction, when the input of Employment is
                    the default baseline, the parameters should all be 0.
                    See the commit message for an example and more details.
                    */
                if (index > 0) {
                    x.setEntry(0, i + index - 1, 1);
                    i += values.size() - 2;
                }
            } else {
                DataCell cell = row.getCell(m_parameterI.get(parameter));
                double radix = ((DoubleValue) cell).getDoubleValue();
                double exponent = Integer.valueOf(value);
                x.setEntry(0, i, Math.pow(radix, exponent));
            }
        }
    }
    // column vector
    RealMatrix r = x.multiply(m_beta);
    double estimate = r.getEntry(0, 0);
    if (m_content.getOffsetValue() != null) {
        estimate = estimate + m_content.getOffsetValue();
    }
    cells[0] = new DoubleCell(estimate);
    return cells;
}
Also used : RealMatrix(org.apache.commons.math3.linear.RealMatrix) DoubleValue(org.knime.core.data.DoubleValue) DoubleCell(org.knime.core.data.def.DoubleCell) 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