Search in sources :

Example 31 with DoubleCell

use of org.knime.core.data.def.DoubleCell in project knime-core by knime.

the class LogisticRegressionContent method createTablePortObject.

/**
 * Creates a BufferedDataTable with the
 * @param exec The execution context
 * @return a port object
 */
public BufferedDataTable createTablePortObject(final ExecutionContext exec) {
    DataTableSpec tableOutSpec = new DataTableSpec("Coefficients and Statistics", new String[] { "Logit", "Variable", "Coeff.", "Std. Err.", "z-score", "P>|z|" }, new DataType[] { StringCell.TYPE, StringCell.TYPE, DoubleCell.TYPE, DoubleCell.TYPE, DoubleCell.TYPE, DoubleCell.TYPE });
    BufferedDataContainer dc = exec.createDataContainer(tableOutSpec);
    List<DataCell> logits = this.getLogits();
    List<String> parameters = this.getParameters();
    int c = 0;
    for (DataCell logit : logits) {
        Map<String, Double> coefficients = this.getCoefficients(logit);
        Map<String, Double> stdErrs = this.getStandardErrors(logit);
        Map<String, Double> zScores = this.getZScores(logit);
        Map<String, Double> pValues = this.getPValues(logit);
        for (String parameter : parameters) {
            List<DataCell> cells = new ArrayList<DataCell>();
            cells.add(new StringCell(logit.toString()));
            cells.add(new StringCell(parameter));
            cells.add(new DoubleCell(coefficients.get(parameter)));
            cells.add(new DoubleCell(stdErrs.get(parameter)));
            cells.add(new DoubleCell(zScores.get(parameter)));
            cells.add(new DoubleCell(pValues.get(parameter)));
            c++;
            dc.addRowToTable(new DefaultRow("Row" + c, cells));
        }
        List<DataCell> cells = new ArrayList<DataCell>();
        cells.add(new StringCell(logit.toString()));
        cells.add(new StringCell("Constant"));
        cells.add(new DoubleCell(this.getIntercept(logit)));
        cells.add(new DoubleCell(this.getInterceptStdErr(logit)));
        cells.add(new DoubleCell(this.getInterceptZScore(logit)));
        cells.add(new DoubleCell(this.getInterceptPValue(logit)));
        c++;
        dc.addRowToTable(new DefaultRow("Row" + c, cells));
    }
    dc.close();
    return dc.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 32 with DoubleCell

use of org.knime.core.data.def.DoubleCell 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]) {
                    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 : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleValue(org.knime.core.data.DoubleValue) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataCell(org.knime.core.data.DataCell) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) CellFactory(org.knime.core.data.container.CellFactory) DataRow(org.knime.core.data.DataRow)

Example 33 with DoubleCell

use of org.knime.core.data.def.DoubleCell in project knime-core by knime.

the class RegressionPredictorNodeModel method createRearranger.

private ColumnRearranger createRearranger(final DataTableSpec inSpec, final PMMLPortObjectSpec regModelSpec, final PMMLRegressionTranslator regModel) throws InvalidSettingsException {
    if (regModelSpec == null) {
        throw new InvalidSettingsException("No input");
    }
    // exclude last (response column)
    String targetCol = "Response";
    for (String s : regModelSpec.getTargetFields()) {
        targetCol = s;
        break;
    }
    final List<String> learnFields;
    if (regModel != null) {
        RegressionTable regTable = regModel.getRegressionTable();
        learnFields = new ArrayList<String>();
        for (NumericPredictor p : regTable.getVariables()) {
            learnFields.add(p.getName());
        }
    } else {
        learnFields = new ArrayList<String>(regModelSpec.getLearningFields());
    }
    final int[] colIndices = new int[learnFields.size()];
    int k = 0;
    for (String learnCol : learnFields) {
        int index = inSpec.findColumnIndex(learnCol);
        if (index < 0) {
            throw new InvalidSettingsException("Missing column for " + "regressor variable : \"" + learnCol + "\"");
        }
        DataColumnSpec regressor = inSpec.getColumnSpec(index);
        String name = regressor.getName();
        DataColumnSpec col = inSpec.getColumnSpec(index);
        if (!col.getType().isCompatible(DoubleValue.class)) {
            throw new InvalidSettingsException("Incompatible type of " + "column \"" + name + "\": " + col.getType());
        }
        colIndices[k++] = index;
    }
    // try to use some smart naming scheme for the append column
    String oldName = targetCol;
    if (inSpec.containsName(oldName) && !oldName.toLowerCase().endsWith("(prediction)")) {
        oldName = oldName + " (prediction)";
    }
    String newColName = DataTableSpec.getUniqueColumnName(inSpec, oldName);
    DataColumnSpec newCol = new DataColumnSpecCreator(newColName, DoubleCell.TYPE).createSpec();
    SingleCellFactory fac = new SingleCellFactory(newCol) {

        @Override
        public DataCell getCell(final DataRow row) {
            RegressionTable t = regModel.getRegressionTable();
            int j = 0;
            double result = t.getIntercept();
            for (NumericPredictor p : t.getVariables()) {
                DataCell c = row.getCell(colIndices[j++]);
                if (c.isMissing()) {
                    return DataType.getMissingCell();
                }
                double v = ((DoubleValue) c).getDoubleValue();
                if (p.getExponent() != 1) {
                    v = Math.pow(v, p.getExponent());
                }
                result += p.getCoefficient() * v;
            }
            return new DoubleCell(result);
        }
    };
    ColumnRearranger c = new ColumnRearranger(inSpec);
    c.append(fac);
    return c;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) NumericPredictor(org.knime.base.node.mine.regression.PMMLRegressionTranslator.NumericPredictor) DataRow(org.knime.core.data.DataRow) RegressionTable(org.knime.base.node.mine.regression.PMMLRegressionTranslator.RegressionTable) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 34 with DoubleCell

use of org.knime.core.data.def.DoubleCell in project knime-core by knime.

the class AbstractTreeEnsembleModel method createLearnAttributeRow.

public DataRow createLearnAttributeRow(final DataRow learnRow, final DataTableSpec learnSpec) {
    final TreeType type = getType();
    final DataCell c = learnRow.getCell(0);
    final int nrAttributes = getMetaData().getNrAttributes();
    switch(type) {
        case Ordinary:
            return learnRow;
        case BitVector:
            if (c.isMissing()) {
                return null;
            }
            BitVectorValue bv = (BitVectorValue) c;
            final long length = bv.length();
            if (length != nrAttributes) {
                // TODO indicate error message
                return null;
            }
            DataCell trueCell = new StringCell("1");
            DataCell falseCell = new StringCell("0");
            DataCell[] cells = new DataCell[nrAttributes];
            for (int i = 0; i < nrAttributes; i++) {
                cells[i] = bv.get(i) ? trueCell : falseCell;
            }
            return new DefaultRow(learnRow.getKey(), cells);
        case ByteVector:
            if (c.isMissing()) {
                return null;
            }
            ByteVectorValue byteVector = (ByteVectorValue) c;
            final long bvLength = byteVector.length();
            if (bvLength != nrAttributes) {
                return null;
            }
            DataCell[] bvCells = new DataCell[nrAttributes];
            for (int i = 0; i < nrAttributes; i++) {
                bvCells[i] = new IntCell(byteVector.get(i));
            }
            return new DefaultRow(learnRow.getKey(), bvCells);
        case DoubleVector:
            if (c.isMissing()) {
                return null;
            }
            DoubleVectorValue doubleVector = (DoubleVectorValue) c;
            final int dvLength = doubleVector.getLength();
            if (dvLength != nrAttributes) {
                return null;
            }
            DataCell[] dvCells = new DataCell[nrAttributes];
            for (int i = 0; i < nrAttributes; i++) {
                dvCells[i] = new DoubleCell(doubleVector.getValue(i));
            }
            return new DefaultRow(learnRow.getKey(), dvCells);
        default:
            throw new IllegalStateException("Type unknown (not implemented): " + type);
    }
}
Also used : DoubleVectorValue(org.knime.core.data.vector.doublevector.DoubleVectorValue) DoubleCell(org.knime.core.data.def.DoubleCell) ByteVectorValue(org.knime.core.data.vector.bytevector.ByteVectorValue) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) BitVectorValue(org.knime.core.data.vector.bitvector.BitVectorValue) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 35 with DoubleCell

use of org.knime.core.data.def.DoubleCell in project knime-core by knime.

the class PMMLDataDictionaryTranslator method addColSpecsForDataFields.

/**
 * @param pmmlDoc the PMML document to analyze
 * @param colSpecs the list to add the data column specs to
 */
private void addColSpecsForDataFields(final PMMLDocument pmmlDoc, final List<DataColumnSpec> colSpecs) {
    DataDictionary dict = pmmlDoc.getPMML().getDataDictionary();
    for (DataField dataField : dict.getDataFieldArray()) {
        String name = dataField.getName();
        DataType dataType = getKNIMEDataType(dataField.getDataType());
        DataColumnSpecCreator specCreator = new DataColumnSpecCreator(name, dataType);
        DataColumnDomain domain = null;
        if (dataType.isCompatible(NominalValue.class)) {
            Value[] valueArray = dataField.getValueArray();
            DataCell[] cells;
            if (DataType.getType(StringCell.class).equals(dataType)) {
                if (dataField.getIntervalArray().length > 0) {
                    throw new IllegalArgumentException("Intervals cannot be defined for Strings.");
                }
                cells = new StringCell[valueArray.length];
                if (valueArray.length > 0) {
                    for (int j = 0; j < cells.length; j++) {
                        cells[j] = new StringCell(valueArray[j].getValue());
                    }
                }
                domain = new DataColumnDomainCreator(cells).createDomain();
            }
        } else if (dataType.isCompatible(DoubleValue.class)) {
            Double leftMargin = null;
            Double rightMargin = null;
            Interval[] intervalArray = dataField.getIntervalArray();
            if (intervalArray != null && intervalArray.length > 0) {
                Interval interval = dataField.getIntervalArray(0);
                leftMargin = interval.getLeftMargin();
                rightMargin = interval.getRightMargin();
            } else if (dataField.getValueArray() != null && dataField.getValueArray().length > 0) {
                // try to derive the bounds from the values
                Value[] valueArray = dataField.getValueArray();
                List<Double> values = new ArrayList<Double>();
                for (int j = 0; j < valueArray.length; j++) {
                    String value = "";
                    try {
                        value = valueArray[j].getValue();
                        values.add(Double.parseDouble(value));
                    } catch (Exception e) {
                        throw new IllegalArgumentException("Skipping domain calculation. " + "Value \"" + value + "\" cannot be cast to double.");
                    }
                }
                leftMargin = Collections.min(values);
                rightMargin = Collections.max(values);
            }
            if (leftMargin != null && rightMargin != null) {
                // set the bounds of the domain if available
                DataCell lowerBound = null;
                DataCell upperBound = null;
                if (DataType.getType(IntCell.class).equals(dataType)) {
                    lowerBound = new IntCell(leftMargin.intValue());
                    upperBound = new IntCell(rightMargin.intValue());
                } else if (DataType.getType(DoubleCell.class).equals(dataType)) {
                    lowerBound = new DoubleCell(leftMargin);
                    upperBound = new DoubleCell(rightMargin);
                }
                domain = new DataColumnDomainCreator(lowerBound, upperBound).createDomain();
            } else {
                domain = new DataColumnDomainCreator().createDomain();
            }
        }
        specCreator.setDomain(domain);
        colSpecs.add(specCreator.createSpec());
        m_dictFields.add(name);
    }
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) DataDictionary(org.dmg.pmml.DataDictionaryDocument.DataDictionary) IntCell(org.knime.core.data.def.IntCell) DataColumnDomain(org.knime.core.data.DataColumnDomain) DataField(org.dmg.pmml.DataFieldDocument.DataField) StringCell(org.knime.core.data.def.StringCell) DoubleValue(org.knime.core.data.DoubleValue) NominalValue(org.knime.core.data.NominalValue) BooleanValue(org.knime.core.data.BooleanValue) IntValue(org.knime.core.data.IntValue) Value(org.dmg.pmml.ValueDocument.Value) DoubleValue(org.knime.core.data.DoubleValue) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) Interval(org.dmg.pmml.IntervalDocument.Interval)

Aggregations

DoubleCell (org.knime.core.data.def.DoubleCell)189 DataCell (org.knime.core.data.DataCell)129 IntCell (org.knime.core.data.def.IntCell)67 DefaultRow (org.knime.core.data.def.DefaultRow)66 StringCell (org.knime.core.data.def.StringCell)65 DataRow (org.knime.core.data.DataRow)57 DataTableSpec (org.knime.core.data.DataTableSpec)55 ArrayList (java.util.ArrayList)42 DataColumnSpec (org.knime.core.data.DataColumnSpec)42 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)41 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)39 RowKey (org.knime.core.data.RowKey)37 DoubleValue (org.knime.core.data.DoubleValue)35 BufferedDataTable (org.knime.core.node.BufferedDataTable)28 DataColumnDomainCreator (org.knime.core.data.DataColumnDomainCreator)26 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)22 DataType (org.knime.core.data.DataType)20 LinkedHashMap (java.util.LinkedHashMap)17 HashMap (java.util.HashMap)13 Point (java.awt.Point)12