use of org.knime.core.data.def.IntCell in project knime-core by knime.
the class RegressionTreeModel method createLearnAttributeRow.
public DataRow createLearnAttributeRow(final DataRow learnRow, final DataTableSpec learnSpec) {
final TreeType type = getType();
switch(type) {
case Ordinary:
return learnRow;
case BitVector:
DataCell c = learnRow.getCell(0);
if (c.isMissing()) {
return null;
}
BitVectorValue bv = (BitVectorValue) c;
final long length = bv.length();
int nrAttributes = getMetaData().getNrAttributes();
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:
DataCell cell = learnRow.getCell(0);
if (cell.isMissing()) {
return null;
}
ByteVectorValue byteVector = (ByteVectorValue) cell;
final long bvLength = byteVector.length();
int nrAttr = getMetaData().getNrAttributes();
if (bvLength != nrAttr) {
return null;
}
DataCell[] bvCells = new DataCell[nrAttr];
for (int i = 0; i < nrAttr; i++) {
bvCells[i] = new IntCell(byteVector.get(i));
}
return new DefaultRow(learnRow.getKey(), bvCells);
default:
throw new IllegalStateException("Type unknown (not implemented): " + type);
}
}
use of org.knime.core.data.def.IntCell in project knime-core by knime.
the class TreeEnsembleModel method createLearnAttributeRow.
public DataRow createLearnAttributeRow(final DataRow learnRow, final DataTableSpec learnSpec) {
final TreeType type = getType();
switch(type) {
case Ordinary:
return learnRow;
case BitVector:
DataCell c = learnRow.getCell(0);
if (c.isMissing()) {
return null;
}
BitVectorValue bv = (BitVectorValue) c;
final long length = bv.length();
int nrAttributes = getMetaData().getNrAttributes();
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:
DataCell cell = learnRow.getCell(0);
if (cell.isMissing()) {
return null;
}
ByteVectorValue byteVector = (ByteVectorValue) cell;
final long bvLength = byteVector.length();
int nrAttr = getMetaData().getNrAttributes();
if (bvLength != nrAttr) {
return null;
}
DataCell[] bvCells = new DataCell[nrAttr];
for (int i = 0; i < nrAttr; i++) {
bvCells[i] = new IntCell(byteVector.get(i));
}
return new DefaultRow(learnRow.getKey(), bvCells);
default:
throw new IllegalStateException("Type unknown (not implemented): " + type);
}
}
use of org.knime.core.data.def.IntCell 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;
}
OccurrenceCounter<String> counter = new OccurrenceCounter<String>();
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);
String majorityClassName = match.getMajorityClassName();
counter.add(majorityClassName);
nrValidModels += 1;
}
}
String bestValue = counter.getMostFrequent();
int index = 0;
if (bestValue == null) {
assert nrValidModels == 0;
Arrays.fill(result, DataType.getMissingCell());
index = size - 1;
} else {
result[index++] = m_targetValueMap.get(bestValue);
if (appendConfidence) {
final int freqValue = counter.getFrequency(bestValue);
result[index++] = new DoubleCell(freqValue / (double) nrValidModels);
}
if (appendClassConfidences) {
for (String key : m_targetValueMap.keySet()) {
int frequency = counter.getFrequency(key);
double ratio = frequency / (double) nrValidModels;
result[index++] = new DoubleCell(ratio);
}
}
}
if (appendModelCount) {
result[index++] = new IntCell(nrValidModels);
}
return result;
}
use of org.knime.core.data.def.IntCell in project knime-core by knime.
the class JoinedTableTest method getRandomRow.
private static final DataRow getRandomRow(final String id) {
DataCell[] cells = new DataCell[3];
cells[0] = new StringCell(id + "-" + RAND.nextInt(100));
cells[1] = new IntCell(RAND.nextInt());
cells[2] = new DoubleCell(RAND.nextDouble());
return new DefaultRow(id, cells);
}
use of org.knime.core.data.def.IntCell in project knime-core by knime.
the class SorterNodeModelTest method testExecuteBufferedDataTableArrayExecutionContext.
/**
* Test method for {@link org.knime.base.node.preproc.sorter.SorterNodeModel#execute(org.knime.core.node.BufferedDataTable[], org.knime.core.node.ExecutionContext)}.
* @throws Exception
* @throws CanceledExecutionException
*/
@Test
public final void testExecuteBufferedDataTableArrayExecutionContext() throws CanceledExecutionException, Exception {
// try to sort a table with 1 entry
String[] columnNames = { "col1", "col2", "col3", "col4" };
DataType[] columnTypes = { DoubleCell.TYPE, StringCell.TYPE, IntCell.TYPE, DoubleCell.TYPE };
DataRow[] rows = new DataRow[1];
DataCell[] myRow = new DataCell[4];
myRow[0] = new DoubleCell(2.4325);
myRow[1] = new StringCell("Test");
myRow[2] = new IntCell(7);
myRow[3] = new DoubleCell(32432.324);
rows[0] = new DefaultRow(Integer.toString(1), myRow);
DataTable[] inputTable = { new DefaultTable(rows, columnNames, columnTypes) };
DataTable[] resultTable = { new DefaultTable(rows, columnNames, columnTypes) };
// set settings
String[] includeCols = { "col1", "col2", "col3", "col4" };
m_settings.addStringArray(SorterNodeModel.INCLUDELIST_KEY, includeCols);
boolean[] sortorder = { true, true, true, true };
m_settings.addBooleanArray(SorterNodeModel.SORTORDER_KEY, sortorder);
m_snm.loadValidatedSettingsFrom(m_settings);
resultTable = m_snm.execute(EXEC_CONTEXT.createBufferedDataTables(inputTable, EXEC_CONTEXT), EXEC_CONTEXT);
// test output
RowIterator rowIt = resultTable[0].iterator();
Assert.assertTrue(rowIt.hasNext());
Assert.assertEquals(rows[0], rowIt.next());
Assert.assertFalse(rowIt.hasNext());
m_snm.reset();
// *********************************************//
// try to sort a large array of DataRows
// In this case we generate a unit matrix
// *********************************************//
// start with a little one
int dimension = 50;
// *********************************************//
// set settings
includeCols = new String[dimension];
for (int i = 0; i < dimension; i++) {
includeCols[i] = "col" + i;
}
m_settings.addStringArray(SorterNodeModel.INCLUDELIST_KEY, includeCols);
sortorder = new boolean[dimension];
for (int i = 0; i < dimension; i++) {
sortorder[i] = true;
}
m_settings.addBooleanArray(SorterNodeModel.SORTORDER_KEY, sortorder);
DataTable[] inputTable2 = { generateUnitMatrixTable(dimension) };
m_snm.loadValidatedSettingsFrom(m_settings);
resultTable = m_snm.execute(EXEC_CONTEXT.createBufferedDataTables(inputTable2, EXEC_CONTEXT), EXEC_CONTEXT);
// test output (should have sorted all rows in reverse order)
rowIt = resultTable[0].iterator();
Assert.assertTrue(rowIt.hasNext());
int k = dimension - 1;
while (rowIt.hasNext()) {
RowKey rk = rowIt.next().getKey();
int ic = Integer.parseInt(rk.getString());
Assert.assertEquals(k, ic);
k--;
}
Assert.assertFalse(rowIt.hasNext());
m_snm.reset();
// *********************************************//
// try to sort a very large array of DataRows
// In this case we generate a unit matrix
// *********************************************//
// dimension 300 => 15,8 secs.
// dimension 500 => 49,7 secs.
dimension = 100;
// *********************************************//
// set settings
includeCols = new String[dimension];
for (int i = 0; i < dimension; i++) {
includeCols[i] = "col" + i;
}
m_settings.addStringArray(SorterNodeModel.INCLUDELIST_KEY, includeCols);
sortorder = new boolean[dimension];
for (int i = 0; i < dimension; i++) {
sortorder[i] = true;
}
m_settings.addBooleanArray(SorterNodeModel.SORTORDER_KEY, sortorder);
DataTable[] inputTable3 = { generateUnitMatrixTable(dimension) };
m_snm.loadValidatedSettingsFrom(m_settings);
resultTable = m_snm.execute(EXEC_CONTEXT.createBufferedDataTables(inputTable3, EXEC_CONTEXT), EXEC_CONTEXT);
// test output (should have sorted all rows in reverse order)
rowIt = resultTable[0].iterator();
Assert.assertTrue(rowIt.hasNext());
k = dimension - 1;
while (rowIt.hasNext()) {
RowKey rk = rowIt.next().getKey();
int ic = Integer.parseInt(rk.getString());
Assert.assertEquals(k, ic);
k--;
}
Assert.assertFalse(rowIt.hasNext());
m_snm.reset();
}
Aggregations