ValueError: setting an array element with a sequence.
时间: 2023-09-23 09:07:24 浏览: 92
python3.7解决最小二乘遇到ValueError:Expected 2D array, got 1D array instead: array=[5.].关于reshape和predict
This error occurs when you try to assign a sequence (such as a list or tuple) to an element in a numpy array. Numpy arrays are meant to hold elements of a single data type, so if you try to assign a sequence that contains elements of different data types, you will get this error.
For example, if you create a numpy array of integers:
```
import numpy as np
arr = np.array([1, 2, 3])
```
And then try to assign a list to one of the elements:
```
arr[0] = [4, 5, 6]
```
You will get the "ValueError: setting an array element with a sequence" error.
To fix this error, you should make sure that the elements you are assigning to the numpy array are of the same data type as the array itself. If you need to store sequences in a numpy array, you can use numpy's structured arrays, which allow you to specify the data type of each element in the array.
阅读全文