. Advertisement .
..3..
. Advertisement .
..4..
I get the error message:
Traceback (most recent call last):
File "main.py", line 5, in <module>
print(x[0,3])
IndexError: too many indices
The problem appears when I have declared a single dimensional array using numpy library array:
# Code with error
# Importing the numpy library
import numpy as np
# Declaring and Initializing the one Dimension Array
x = np.array([212,312,2,12,124,142,12])
# Printing the result
print(x[0,3])
I’ve tried to fix it for some days but have not resolved it yet. Can someone suggest a relevant solution to fix this error?
The cause: This error happens when you try to access the elements of a one-dimensional numpy array as a two dimensional array.
The solution: To fix this problem, double-check the dimensions you’re using, as well as the dimensions you’re declaring and initializing as the following:
Ouput: