IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed
时间: 2024-04-29 08:22:40 浏览: 11
This error message indicates that you are trying to access an element in a NumPy array using too many indices.
For example, suppose you have a 2-dimensional array (matrix) with shape (3, 3), and you try to access an element using three indices like this:
```python
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
elem = arr[1, 2, 0] # this will raise an IndexError
```
In this case, the `arr[1, 2, 0]` statement is trying to access an element at row 1, column 2, and depth 0 (which doesn't exist because the array is only 2-dimensional).
To fix this error, make sure you are using the correct number of indices for your array. For a 2-dimensional array, you should only use two indices (one for the row and one for the column).