matchifang
matchifang

Reputation: 5450

numpy.where() returns empty list, when item satisfy condition

I have an array with values from 0-5, and I want to use numpy.where() to get the indices of where the item equals to 1, but it returns an empty array.

Code:

hf = h5py.File(PATH+'/data.h5', 'r')
data = hf['data']                    #dtype int32 [0,1,2,3,4,2,3,1,1,1,4,5,6]
print(data[1] == 1)                  #prints True
indices = np.where(data == 1)[0]     #ERROR here - returns empty array

Upvotes: 0

Views: 636

Answers (1)

hpaulj
hpaulj

Reputation: 231385

You have to download the dataset to perform tests like this on it.

Using a test file I have hanging around:

In [318]: f = h5py.File('data.h5')
In [319]: list(f.keys())
Out[319]: ['dset', 'dset1', 'vset']
In [320]: f['dset']
Out[320]: <HDF5 dataset "dset": shape (3, 5), type "<f8">

I can index and test a single item, or slice of the dataset

In [321]: f['dset'][1]
Out[321]: array([ 1.,  1.,  1.,  1.,  1.])
In [322]: f['dset'].shape
Out[322]: (3, 5)
In [323]: f['dset'][...]
Out[323]: 
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])

But a boolean test on the dataset does not work:

In [324]: f['dset']>0
...
TypeError: unorderable types: Dataset() > int()

==1 works, but compares the dataset objects with 1, and inevitably returns False. That's why where gives you an empty result:

In [325]: f['dset']==1
Out[325]: False

To do the element by element test I have to 'index' the dataset:

In [326]: f['dset'][...]>0
Out[326]: 
array([[ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True]], dtype=bool)

Upvotes: 1

Related Questions