Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get indices of elements that are greater than a threshold in 2D numpy array

Tags:

python

numpy

I have a 2D numpy array:

x = [[  1.92043482e-04   0.00000000e+00   0.00000000e+00   0.00000000e+00     0.00000000e+00   0.00000000e+00   2.41005634e-03   0.00000000e+00     7.19330120e-04   0.00000000e+00   0.00000000e+00   1.42886875e-04     0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00     0.00000000e+00   9.79279411e-05   7.88888657e-04   0.00000000e+00     0.00000000e+00   1.40425916e-01   0.00000000e+00   1.13955893e-02     7.36868947e-03   3.67091988e-04   0.00000000e+00   0.00000000e+00     0.00000000e+00   0.00000000e+00   1.72037105e-03   1.72377961e-03     0.00000000e+00   0.00000000e+00   1.19532061e-01   0.00000000e+00     0.00000000e+00   0.00000000e+00   0.00000000e+00   3.37249481e-04     0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00     0.00000000e+00   0.00000000e+00   1.75111492e-03   0.00000000e+00     0.00000000e+00   1.12639313e-02]  [  0.00000000e+00   0.00000000e+00   1.10271735e-04   5.98736562e-04     6.77961628e-04   7.49569659e-04   0.00000000e+00   0.00000000e+00     2.91697850e-03   0.00000000e+00   0.00000000e+00   0.00000000e+00     0.00000000e+00   0.00000000e+00   3.30257021e-04   2.46629275e-04     0.00000000e+00   1.87586441e-02   6.49103144e-04   0.00000000e+00     1.19046355e-04   0.00000000e+00   0.00000000e+00   2.69499898e-03     1.48525386e-02   0.00000000e+00   0.00000000e+00   0.00000000e+00     0.00000000e+00   0.00000000e+00   0.00000000e+00   1.18803119e-03     3.93100829e-04   0.00000000e+00   3.76245304e-04   2.79537738e-02     0.00000000e+00   1.20738457e-03   9.74669064e-06   7.18680093e-04     1.61546793e-02   3.49360861e-04   0.00000000e+00   0.00000000e+00     0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00     0.00000000e+00   0.00000000e+00]] 

How do I get indices of the elements that are greater than 0.01?

Right now, I'm doing t = np.argmax(x, axis=1) to get the index of the maximum value from each and the result of it is: [21 35]. How do I achieve the above?

like image 685
Arman Avatar asked May 31 '17 23:05

Arman


People also ask

How do you find the index of the maximum value in a 2D array Python?

You can use argmax() to get the index of your maximum value.

How do you find the indices of N maximum values in a NumPy array?

In order to get the indices of N maximum values in a NumPy array, we can use the argsort() function.

How do you find the index of an element in a 2D NumPy array?

Index of element in 2D array We can also use the np. where() function to find the position/index of occurrences of elements in a two-dimensional or multidimensional array. For a 2D array, the returned tuple will contain two numpy arrays one for the rows and the other for the columns.

How do I compare values in two NumPy arrays?

To check if two NumPy arrays A and B are equal: Use a comparison operator (==) to form a comparison array. Check if all the elements in the comparison array are True.


1 Answers

You can use np.argwhere to return the indices of all the entries in an array matching a boolean condition:

>>> x = np.array([[0,0.2,0.5],[0.05,0.01,0]])  >>> np.argwhere(x > 0.01) array([[0, 1],        [0, 2],        [1, 0]])     
like image 110
maxymoo Avatar answered Sep 20 '22 05:09

maxymoo