IndexError: index out of bounds
IndexError: index out of bounds
$ python -c "import numpy as np; a=np.zeros(3); print(a[5])"
Traceback (most recent call last):
File "<string>", line 1, in <module>
IndexError: index 5 is out of bounds for axis 0 with size 3
Why this happens
You tried to access an index that does not exist for the given axis. NumPy arrays have fixed sizes per dimension.
Fix
Check shapes and index ranges. Use valid indices or safely guard access (e.g., bounds checks) or use slicing.
Wrong code
import numpy as np
a = np.zeros(3)
print(a[5])
Fixed code
import numpy as np
a = np.zeros(3)
idx = 2
if 0 <= idx < a.shape[0]:
print(a[idx])
else:
print('index out of range')