1

This is a fundamentals-related question so it might appear very dumb for other people but here it goes: From reading the docs and this post, I understand that np.where() returns an empty array for y if the passed argument array is 1D. But how can I check if it is actually an empty array, rather than being None or NaN?

I have checked this post to check for Noneness, but I'm stumped on how to do this if I can't access the returned value in the first place. I tried to access this value (see below code) but I get an IndexError

Moreover, I'm a little confused on the inner workings of numpy.where(). From the docs it says that np.where() returns an ndarray but it returns a tuple if I run it on Jupyter.

The following is the code I used. (numpy version: numpy==1.15.4)

test_array = np.array([4, 5, 6]) # 1-D array
print(type(test_array)) 

>>> <class 'numpy.ndarray'>

thres = 6
result = np.where(test_array > thres)
print(type(result))

>>> <class 'tuple'>

Related to the question above, if I try to access the result values,

result
>>> (array([], dtype=int64),)

result[0]
>>> array([], dtype=int64)

result[1]
>>> IndexError: tuple index out of range

Please let me know if I'm missing something!

Thanks in advance!

  • Did you see the `Note`? You are using `where` in the single-input mode. For that it refers us to `np.nonzero`. I and most SO users are guilty of using `where` in this `nonzero` mode. – hpaulj Aug 29 '19 at 15:37
  • Oh this is interesting - I've definitely glossed over the `Note` and definitely was using `np.nonzero` without realizing it... Thanks for the insight! – PotentialLime Aug 29 '19 at 16:14

2 Answers2

1

Your test array:

In [57]: arr = np.array([4,5,6])                                                                             
In [58]: arr                                                                                                 
Out[58]: array([4, 5, 6])

the test produces a boolean array:

In [59]: arr>6                                                                                               
Out[59]: array([False, False, False])

searching for non-zeros, True, in that array - there are none. As per docs, the result is a tuple, one array per dimension of the input:

In [60]: np.nonzero(arr>6)                                                                                   
Out[60]: (array([], dtype=int64),)
In [61]: _[0]                                                                                                
Out[61]: array([], dtype=int64)

Out[61].size is 0. Out[61].shape is (0,).

A more interesting threshhold:

In [62]: np.where(arr>4)                                                                                     
Out[62]: (array([1, 2]),)
In [63]: np.nonzero(arr>4)                                                                                   
Out[63]: (array([1, 2]),)

This tuple can be used directly to index the original array:

In [64]: arr[_]                                                                                              
Out[64]: array([5, 6])

Out[69] is also a valid indexing tuple.

The tuple nature of the result becomes more interesting, and useful, when we work on a 2 or 3d array.

For example, multiples of 3 in a 2d array:

In [65]: arr = np.arange(12).reshape(3,4)                                                                    
In [66]: arr                                                                                                 
Out[66]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [67]: (arr % 3)==0                                                                                        
Out[67]: 
array([[ True, False, False,  True],
       [False, False,  True, False],
       [False,  True, False, False]])
In [68]: np.nonzero(_)                                                                                       
Out[68]: (array([0, 0, 1, 2]), array([0, 3, 2, 1]))
In [69]: arr[_]                                                                                              
Out[69]: array([0, 3, 6, 9])
hpaulj
  • 221,503
  • 14
  • 230
  • 353
0

I think you need to check the value of result[0].

print(type(result[0]))
ajlaj25
  • 227
  • 2
  • 7
  • I tried this on Jupyter, but for `print(type(result[0]))` it gave me an ndarray as expected but for `print(type(result[1]))` and it still gives me **IndexError: tuple index out of range** – PotentialLime Aug 29 '19 at 16:42