Question

I have an numpy 2D array and I want it to return coloumn c where (r, c-1) (row r, coloumn c) equals a certain value (int n).

I don't want to iterate over the rows writing something like

for r in len(rows):  
  if array[r, c-1] == 1:
    store array[r,c]

, because there are 4000 of them and this 2D array is just one of 20 i have to look trough.

I found "filter" but don't know how to use it (Found no doc).

Is there an function, that provides such a search?

Was it helpful?

Solution

I hope I understood your question correctly. Let's say you have an array a

a = array(range(7)*3).reshape(7, 3)
print a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 0, 1],
       [2, 3, 4],
       [5, 6, 0],
       [1, 2, 3],
       [4, 5, 6]])

and you want to extract all lines where the first entry is 2. This can be done like this:

print a[a[:,0] == 2]
array([[2, 3, 4]])

a[:,0] denotes the first column of the array, == 2 returns a Boolean array marking the entries that match, and then we use advanced indexing to extract the respective rows.

Of course, NumPy needs to iterate over all entries, but this will be much faster than doing it in Python.

OTHER TIPS

Numpy arrays are not indexed. If you need to perform this specific operation more effeciently than linear in the array size, then you need to use something other than numpy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top