frege1234
frege1234

Reputation: 13

Indexing in Numpy

In the following example, why does the first numpy index gives [1], but the second numpy index gives [ ]? Thanks in advance for the help!

a = np.array([1,2,3])
print(a[a<2])
>>> [1]
print(a[True, False, False])
>>> []

Upvotes: 0

Views: 58

Answers (2)

Soufiane Afandi
Soufiane Afandi

Reputation: 21

here you told the numpy array a to select the elements which are less than 2 and there is only one element it is the element "1" which has as index 0 in your array.

look at this illustrative example:

print(a[a<1])
>>> []
print(a[a<2])
>>> [1]
print(a[a<3])
>>> [1,2]
print(a[a<4])
>>> [1,2,3]

try

print(a[[True, False, False]])

instead of

print(a[True, False, False])

Upvotes: 0

shtse8
shtse8

Reputation: 1365

for the first one, you are selecting index: a[a<2] = a[[True, False, False]] which is different to your second. a[True, False, False]

for your second, you are selecting True, False, False index, which doesn't exists in the array. so nothing returns.

Upvotes: 1

Related Questions