Showing wrong axis in 2D array when value is out of range

125 Views Asked by At

I have a 2D array (row*column)row means axis0 and column mean aixs1.

example:

a=np.array([[10,20,30],[40,50,60]])

now when I am trying to access the value(out of range along axis 0)I am getting below Exception which is correct.

print(a[-3][-1])

    IndexError: index -3 is out of bounds for `axis 0` with size 2

Note: Same when I am trying along the axis1 It is still showing same axis0 which should be axis1.

print(a[-2][5])
IndexError: index 5 is out of bounds for axis 0 with size 3
1

There are 1 best solutions below

0
On

This is because you first subset a to a vector, which is 1 dimensional.

>>> a[-2]
array([10, 20, 30])

>>> np.array([10, 20, 30])[5]
IndexError: index 5 is out of bounds for axis 0 with size 3

However if you slice both dimension at once, you get the expected error:

>>> a[-2, 5]
IndexError: index 5 is out of bounds for axis 1 with size 3