Dmitry  Proshutinski
Dmitry Proshutinski

Reputation: 45

np.where return an empty array with numpy array in it

I have this:

import numpy as np

mol= np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20], [21], [22], [23], [24], [25], [26], [27]])
i = np.where(mol == 7)
print(i)

But it's return

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

Also, if i do

i = np.where(mol == 7)

It's return the same

What's the problem? Thank you!

Upvotes: 2

Views: 2479

Answers (1)

user3483203
user3483203

Reputation: 51155

When you create a numpy array with jagged lists, the resulting numpy array will be of dtype object and contain lists.

>>> x = np.array([[1], [1,2]])
>>> x
array([list([1]), list([1, 2])], dtype=object)

You can clearly see the same results with your input list:

array([list([0, 1, 2, 3, 4]), list([5, 6, 7, 8, 9]),
       list([10, 11, 12, 13, 14]), list([15, 16, 17, 18, 19]), list([20]),
       list([21]), list([22]), list([23]), list([24]), list([25]),
       list([26]), list([27])], dtype=object)

This is why np.where doesn't find your values, you can't search lists using np.where. Compare this to a non-jagged array that doesn't contain lists:

x = np.arange(28).reshape(7, -1)

In [21]: np.where(x==7)
Out[21]: (array([1]), array([3]))

If you want to get around this, you can either not use jagged arrays, which are usually a hassle anyways, or you can pad your array with something like -1:

top = max([len(i) for i in mol])
mol = np.asarray([np.pad(i, (0, top-len(i)), 'constant', constant_values=-1) for i in mol])

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, -1, -1, -1, -1],
       [21, -1, -1, -1, -1],
       [22, -1, -1, -1, -1],
       [23, -1, -1, -1, -1],
       [24, -1, -1, -1, -1],
       [25, -1, -1, -1, -1],
       [26, -1, -1, -1, -1],
       [27, -1, -1, -1, -1]])

Which will enable you to again use np.where

In [40]: np.where(mol==7)
Out[40]: (array([1]), array([2]))

Upvotes: 2

Related Questions