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();
}
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
}
};
}
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;
}
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);
}
}
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);
}
}
Aggregations