Reputation: 723
Could somebody please help on explaining the meaning of this error message?
There is this part of lines of code that I am trying to understand. So I experimented myself with an easier example.
I have
a = array([[0, 1],
[2, 3],
[4, 5]])
and
b = [1,3,5,7]
I wrote
a[b[0:2]]
and there is an error comment :
index 3 is out of bounds for axis 0 with size 3
I understand what does b[0:2]
means, it means you take the element of b
with index 0 to index 1, so you get [1,3]
. But I don't quite understand what does it mean when you pass it to a
?
I am trying to understand what is the code a[b[0:2]]
is actually trying to do.
Could somebody please lend a help? Thanks!
Upvotes: 0
Views: 6010
Reputation: 10450
>>> a = np.array([[0, 1], [2,3],[4,5]])
>>> a2 = np.array([[0, 1], [2,3],[4,5],[6,7]])
Two arrays:
a - with elements number 0 to 2
a2 - with elements number 0 to 3
>>> b[0:2]
[1, 3]
b[0:2] - means two elements (#1 and #3)
>>> a[b[0:2]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index 3 is out of bounds for axis 0 with size 3
means - you try to get elements #1 and #3 from a. But you don't have element #3 in a
>>> a2[b[0:2]]
array([[2, 3],
[6, 7]])
>>>
means - you try to get elements #1 and #3 from a2. the result is: two elements #1 is [2,3] and #3 is [6,7]
Upvotes: 3