开发者

How to extract lines in an array, which contain a certain value? (numpy, scipy)

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 开发者_JAVA技巧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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜