Reputation: 576
I want to find index of element that bigger than 2 from numpy 2d array.
like this
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
# find indices of element that bigger than 2
# result = [[0,2],[[1,0],[1,1],[1,2]]
Upvotes: 2
Views: 1275
Reputation: 107287
You can use np.where()
which will gives you the expected indices in a tuple mode (separate axis):
In [6]: np.where(a>2)
Out[6]: (array([0, 1, 1, 1]), array([2, 0, 1, 2]))
Or directly the np.argwhere()
:
In [5]: np.argwhere(a>2)
Out[5]:
array([[0, 2],
[1, 0],
[1, 1],
[1, 2]])
Upvotes: 3