chris
chris

Reputation: 5006

np.where 2D array

I have created a numpy array with this data:

[[-180.          -80.           20.            1.           10.        ]
 [-180.          -80.           30.23255814    1.           10.        ]
 [-180.          -80.           40.46511628    1.           10.        ]
 ..., 
 [  90.           70.          439.53488372    1.           10.        ]
 [  90.           70.          449.76744186    1.           10.        ]
 [  90.           70.          460.            1.           10.        ]]

I then run:

print a[np.where(a[-1])]

I would expect this to print the whole array again, however it only gives me back 5 rows:

[[-180.          -80.           20.            1.           10.        ]
 [-180.          -80.           30.23255814    1.           10.        ]
 [-180.          -80.           40.46511628    1.           10.        ]
 [-180.          -80.           50.69767442    1.           10.        ]
 [-180.          -80.           60.93023256    1.           10.        ]]

What am I missing?

The array was created with this:

x = np.linspace(-180,90,27)
y = np.linspace(-80,70,30)
z = np.linspace(20,460,44)
a = np.vstack(np.meshgrid(x,y,z,[1],[10])).reshape(5,-1).T

EDIT:

The goal here is to identify rows where the last element is greater than 0. Some processing is being done on the data and the last column will be altered.

Upvotes: 2

Views: 5656

Answers (2)

YXD
YXD

Reputation: 32521

To answer the updated question, you can do:

a[a[..., -1] > 0]

Example with the intermediate steps:

>>> a
array([[4, 0, 3],
       [2, 1, 3],
       [3, 3, 3],
       [4, 2, 2],
       [2, 0, 0],
       [0, 2, 2],
       [0, 4, 2],
       [2, 1, 1],
       [0, 3, 1],
       [3, 2, 0]])
>>> a[..., -1]
array([3, 3, 3, 2, 0, 2, 2, 1, 1, 0])
>>> a[..., -1] > 0
array([ True,  True,  True,  True, False,  True,  True,  True,  True, False], dtype=bool)
>>> a[a[..., -1] > 0]
array([[4, 0, 3],
       [2, 1, 3],
       [3, 3, 3],
       [4, 2, 2],
       [0, 2, 2],
       [0, 4, 2],
       [2, 1, 1],
       [0, 3, 1]])
>>>

Answering the original question:

First, a[-1] returns the last row (not the last column):

>>> a[-1]
array([  90.,   70.,  460.,    1.,   10.])

Then np.where returns the indices of the non-zero elements of this row, which in this case is every element:

>>> np.where(a[-1])
(array([0, 1, 2, 3, 4]),)

Indexing as a[indices] returns you the rows corresponding to the values in indices, which in this case are the first five rows.

To return the whole array you currently have to jump through some hoops as the default is for "cartesian" indexing:

a[np.arange(a.shape[0])[..., None], np.arange(a.shape[1])]

or

inds = np.ix_(np.arange(a.shape[0]), np.arange(a.shape[1]))
a[inds]

NumPy may support "orthogonal" indexing in the future (see this pull request and a long discussion on the NumPy-discussion mailing list) which should allow for more flexible indexing.

Upvotes: 4

head7
head7

Reputation: 1433

If the goal is to identify rows where the last column content is superior to 0, then you can try :

a[a[:,-1] > 0] 

or the same with np.where:

a[np.where(a[:, -1] > 0)]

Upvotes: 2

Related Questions