ValueError: Expected 2D array, got 1D array instead: array=[0. 0. 0. ... 3. 3. 3.]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
时间: 2023-06-27 12:03:07 浏览: 141
这个错误通常是由于传入的数据维度不正确引起的。如果你使用的是 scikit-learn 进行机器学习,可以尝试按照错误提示中给出的方式对数据进行 reshape。
如果你的数据是一维的,可以使用 `array.reshape(-1, 1)` 将其转换为二维数组,其中 -1 表示自动计算数组的长度。如果你的数据是一个样本,可以使用 `array.reshape(1, -1)` 将其转换为二维数组,其中 -1 表示自动计算特征的数量。
相关问题
builtins.ValueError: Expected 2D array, got 1D array instead:
To fix the `ValueError` you encountered, you need to reshape your 1D array into a 2D array. You can use the `reshape` method from NumPy to accomplish this. Here's an example of how to do it:
```python
import numpy as np
# Assuming your 1D array is called 'arr'
arr_2d = np.reshape(arr, (-1, 1))
# Now 'arr_2d' is a 2D array with a single column
```
In this example, `arr` is your 1D array and `arr_2d` is the reshaped 2D array. The `-1` in the `reshape` method's argument means that the size of that dimension will be inferred based on the size of the original array. The `1` specifies that the reshaped array should have a single column.
Make sure to replace `'arr'` with the actual name of your 1D array in your code.
ValueError: Expected 2D array, got 1D array instead: array=[9.].
这个错误通常表示你在使用某些需要输入二维数组的函数时,却传入了一维数组。你需要将这个一维数组转换成二维数组,例如可以使用reshape函数将其转换成一个只有一列的二维数组:
```
import numpy as np
arr_1d = np.array([9.])
arr_2d = arr_1d.reshape(-1, 1)
```
这样就可以将`[9.]`转换成一个只有一列的二维数组`[[9.]]`,然后在使用相关函数时传入`arr_2d`即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)