Search in sources :

Example 36 with BitmapBackedSelection

use of tech.tablesaw.selection.BitmapBackedSelection in project symja_android_library by axkr.

the class Rows method tail.

public static void tail(int rowsToInclude, Table oldTable, Table newTable) {
    int oldTableSize = oldTable.rowCount();
    int start = oldTableSize - rowsToInclude;
    Selection rows = new BitmapBackedSelection(rowsToInclude);
    for (int i = start; i < oldTableSize; i++) {
        rows.add(i);
    }
    copyRowsToTable(rows, oldTable, newTable);
}
Also used : BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection) Selection(tech.tablesaw.selection.Selection) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection)

Example 37 with BitmapBackedSelection

use of tech.tablesaw.selection.BitmapBackedSelection in project symja_android_library by axkr.

the class StandardTableSliceGroup method splitOn.

/**
 * Splits the sourceTable table into sub-tables, grouping on the columns whose names are given in
 * splitColumnNames
 */
private void splitOn(String... splitColumnNames) {
    Map<ByteArray, Selection> selectionMap = new LinkedHashMap<>();
    Map<ByteArray, String> sliceNameMap = new HashMap<>();
    List<Column<?>> splitColumns = getSourceTable().columns(splitColumnNames);
    if (containsTextColumn(splitColumns)) {
        for (int i = 0; i < getSourceTable().rowCount(); i++) {
            ByteArrayList byteArrayList = new ByteArrayList();
            StringBuilder stringKey = new StringBuilder();
            int count = 0;
            for (Column<?> col : splitColumns) {
                stringKey.append(col.getString(i));
                if (count < splitColumns.size() - 1) {
                    stringKey.append(SPLIT_STRING);
                }
                byteArrayList.addElements(byteArrayList.size(), col.asBytes(i));
                count++;
            }
            // Add to the matching selection.
            ByteArray byteArray = new ByteArray(byteArrayList.toByteArray());
            Selection selection = selectionMap.getOrDefault(byteArray, new BitmapBackedSelection());
            selection.add(i);
            selectionMap.put(byteArray, selection);
            sliceNameMap.put(byteArray, stringKey.toString());
        }
    } else {
        // handle the case where split is on non-text-columns
        int byteSize = getByteSize(splitColumns);
        for (int i = 0; i < getSourceTable().rowCount(); i++) {
            StringBuilder stringKey = new StringBuilder();
            ByteBuffer byteBuffer = ByteBuffer.allocate(byteSize);
            int count = 0;
            for (Column<?> col : splitColumns) {
                stringKey.append(col.getString(i));
                if (count < splitColumns.size() - 1) {
                    stringKey.append(SPLIT_STRING);
                }
                byteBuffer.put(col.asBytes(i));
                count++;
            }
            // Add to the matching selection.
            ByteArray byteArray = new ByteArray(byteBuffer.array());
            Selection selection = selectionMap.getOrDefault(byteArray, new BitmapBackedSelection());
            selection.add(i);
            selectionMap.put(byteArray, selection);
            sliceNameMap.put(byteArray, stringKey.toString());
        }
    }
    // Construct slices for all the values in our maps
    for (Entry<ByteArray, Selection> entry : selectionMap.entrySet()) {
        TableSlice slice = new TableSlice(getSourceTable(), entry.getValue());
        slice.setName(sliceNameMap.get(entry.getKey()));
        addSlice(slice);
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection) Selection(tech.tablesaw.selection.Selection) ByteBuffer(java.nio.ByteBuffer) LinkedHashMap(java.util.LinkedHashMap) Column(tech.tablesaw.columns.Column) CategoricalColumn(tech.tablesaw.api.CategoricalColumn) ByteArrayList(it.unimi.dsi.fastutil.bytes.ByteArrayList) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection)

Example 38 with BitmapBackedSelection

use of tech.tablesaw.selection.BitmapBackedSelection in project symja_android_library by axkr.

the class RollingColumn method calc.

@SuppressWarnings({ "unchecked" })
public <INCOL extends Column<?>, OUT> Column<?> calc(AggregateFunction<INCOL, OUT> function) {
    // TODO: the subset operation copies the array. creating a view would likely be more efficient
    Column<?> result = function.returnType().create(generateNewColumnName(function));
    for (int i = 0; i < window - 1; i++) {
        result.appendMissing();
    }
    for (int origColIndex = 0; origColIndex < column.size() - window + 1; origColIndex++) {
        Selection selection = new BitmapBackedSelection();
        selection.addRange(origColIndex, origColIndex + window);
        INCOL subsetCol = (INCOL) column.subset(selection.toArray());
        OUT answer = function.summarize(subsetCol);
        if (answer instanceof Number) {
            Number number = (Number) answer;
            ((DoubleColumn) result).append(number.doubleValue());
        } else {
            result.appendObj(answer);
        }
    }
    return result;
}
Also used : DoubleColumn(tech.tablesaw.api.DoubleColumn) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection) Selection(tech.tablesaw.selection.Selection) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection)

Example 39 with BitmapBackedSelection

use of tech.tablesaw.selection.BitmapBackedSelection in project symja_android_library by axkr.

the class Table method dropWhere.

public Table dropWhere(Selection selection) {
    Selection opposite = new BitmapBackedSelection();
    opposite.addRange(0, rowCount());
    opposite.andNot(selection);
    Table newTable = this.emptyCopy(opposite.size());
    Rows.copyRowsToTable(opposite, this, newTable);
    return newTable;
}
Also used : PivotTable(tech.tablesaw.aggregate.PivotTable) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection) Selection(tech.tablesaw.selection.Selection) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection)

Example 40 with BitmapBackedSelection

use of tech.tablesaw.selection.BitmapBackedSelection in project symja_android_library by axkr.

the class Table method sampleSplit.

/**
 * Splits the table into two, randomly assigning records to each according to the proportion given
 * in trainingProportion
 *
 * @param table1Proportion The proportion to go in the first table
 * @return An array two tables, with the first table having the proportion specified in the method
 *     parameter, and the second table having the balance of the rows
 */
public Table[] sampleSplit(double table1Proportion) {
    Table[] tables = new Table[2];
    int table1Count = (int) Math.round(rowCount() * table1Proportion);
    Selection table2Selection = new BitmapBackedSelection();
    for (int i = 0; i < rowCount(); i++) {
        table2Selection.add(i);
    }
    Selection table1Selection = new BitmapBackedSelection();
    Selection table1Records = selectNRowsAtRandom(table1Count, rowCount());
    for (int table1Record : table1Records) {
        table1Selection.add(table1Record);
    }
    table2Selection.andNot(table1Selection);
    tables[0] = where(table1Selection);
    tables[1] = where(table2Selection);
    return tables;
}
Also used : PivotTable(tech.tablesaw.aggregate.PivotTable) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection) Selection(tech.tablesaw.selection.Selection) BitmapBackedSelection(tech.tablesaw.selection.BitmapBackedSelection)

Aggregations

BitmapBackedSelection (tech.tablesaw.selection.BitmapBackedSelection)79 Selection (tech.tablesaw.selection.Selection)79 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)31 PivotTable (tech.tablesaw.aggregate.PivotTable)3 ByteOpenHashSet (it.unimi.dsi.fastutil.bytes.ByteOpenHashSet)2 IntIterator (it.unimi.dsi.fastutil.ints.IntIterator)2 IntOpenHashSet (it.unimi.dsi.fastutil.ints.IntOpenHashSet)2 ShortOpenHashSet (it.unimi.dsi.fastutil.shorts.ShortOpenHashSet)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 ByteArrayList (it.unimi.dsi.fastutil.bytes.ByteArrayList)1 ByteIterator (it.unimi.dsi.fastutil.bytes.ByteIterator)1 IntRBTreeSet (it.unimi.dsi.fastutil.ints.IntRBTreeSet)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 CategoricalColumn (tech.tablesaw.api.CategoricalColumn)1 DoubleColumn (tech.tablesaw.api.DoubleColumn)1 Column (tech.tablesaw.columns.Column)1