Question

Given a matrix A with shape (1000000,6) I have figured out how to get the minimum rightmost value for each row and implemented it in this function:

def calculate_row_minima_indices(h): # h is the given matrix.
    """Returns the indices of the rightmost minimum per row for matrix h."""
    flipped = numpy.fliplr(h) # flip the matrix to get the rightmost minimum.
    flipped_indices = numpy.argmin(flipped, axis=1)
    indices = numpy.array([2]*dim) - flipped_indices
    return indices

indices = calculate_row_minima_indices(h)
for col, row in enumerate(indices):
    print col, row, h[col][row] # col_index, row_index and value of minimum which should be removed.

Each row has a minimum. So what I need know is to remove the entry with the minimum and shrink the Matrix with shape (1000000,6) to a matrix with shape (1000000,5).

I would generate a new matrix with lower dimension and populate it with the values I want it to carry using a for loop, but I am afraid of the runtime. So is there some builtin way or some trick to shrink the matrix by the minima per row?

Perhaps this information is of use: The values are all greater or equal to 0.0.

Was it helpful?

Solution

Assuming you have enough memory to hold a boolean mask the shape of your original array as well as the new array, here's one way to do it:

import numpy as np

def main():
    np.random.seed(1) # For reproducibility
    data = generate_data((10, 6))

    indices = rightmost_min_col(data)
    new_data = pop_col(data, indices)

    print 'Original data...'
    print data
    print 'Modified data...'
    print new_data

def generate_data(shape):
    return np.random.randint(0, 10, shape)

def rightmost_min_col(data):
    nrows, ncols = data.shape[:2]
    min_indices = np.fliplr(data).argmin(axis=1)
    min_indices = (ncols - 1) - min_indices
    return min_indices

def pop_col(data, col_indices):
    nrows, ncols = data.shape[:2]
    col_indices = col_indices[:, np.newaxis]
    row_indices = np.arange(ncols)[np.newaxis, :]
    mask = col_indices != row_indices
    return data[mask].reshape((nrows, ncols-1))

if __name__ == '__main__':
    main()

This yields:

Original data...
[[5 8 9 5 0 0]
 [1 7 6 9 2 4]
 [5 2 4 2 4 7]
 [7 9 1 7 0 6]
 [9 9 7 6 9 1]
 [0 1 8 8 3 9]
 [8 7 3 6 5 1]
 [9 3 4 8 1 4]
 [0 3 9 2 0 4]
 [9 2 7 7 9 8]]
Modified data...
[[5 8 9 5 0]
 [7 6 9 2 4]
 [5 2 4 4 7]
 [7 9 1 7 6]
 [9 9 7 6 9]
 [1 8 8 3 9]
 [8 7 3 6 5]
 [9 3 4 8 4]
 [0 3 9 2 4]
 [9 7 7 9 8]]

One of the less readable tricks I'm using here is exploiting numpy's broadcasting during array comparisons. As a quick example, consider the following:

import numpy as np
a = np.array([[1, 2, 3]])
b = np.array([[1],[2],[3]])
print a == b

This yields:

array([[ True, False, False],
       [False,  True, False],
       [False, False,  True]], dtype=bool)

So, if we know the column index of the item we want removed, we can vectorize the operation for an array of column indices, which is what pop_col does.

OTHER TIPS

you can use a bool mask array to do the selection, but the memory useage is a little large.

import numpy

h = numpy.random.randint(0, 10, (20, 6))

flipped = numpy.fliplr(h) # flip the matrix to get the rightmost minimum.
flipped_indices = numpy.argmin(flipped, axis=1)
indices = 5 - flipped_indices

mask = numpy.ones(h.shape, numpy.bool)

mask[numpy.arange(h.shape[0]), indices] = False

result = h[mask].reshape(-1, 5)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top