上面的代码提示错误non-broadcastable output operand with shape (98,1) doesn't match the broadcast shape (98,17)
时间: 2024-03-03 17:52:46 浏览: 39
这个错误提示是因为 `pred_Y` 的形状是 `(样本数量, 1)`,而 `scaler.inverse_transform()` 方法需要的是一个形状为 `(样本数量, 特征数量)` 的数组。因此,我们需要将 `pred_Y` 的形状从 `(样本数量, 1)` 转换为 `(样本数量, 特征数量)`。
假设原始数据集共有 17 个特征,可以使用 `np.ones()` 方法创建一个形状为 `(样本数量, 17)` 的数组,并将 `pred_Y` 的值复制到新数组的第一列中,如下所示:
```python
dataset = np.loadtxt(r'D:\python-learn\testdata.csv', delimiter=",",skiprows=1)
X = dataset[:,0:17]
scaler = MinMaxScaler(feature_range=(0, 1))
X = scaler.fit_transform(X)
pred_Y = model.predict(X)
pred_Y = scaler.inverse_transform(np.hstack((pred_Y, np.ones((pred_Y.shape[0], 16))))[:, 0:17]) # 修改这一行
print("Predicted value:", pred_Y)
```
这样就可以正确地反归一化了。
相关问题
non-broadcastable output operand with shape (50,1) doesn t match the broadcast shape (50,5)
This error occurs when you are trying to perform a broadcast operation on two arrays with incompatible shapes. In this case, the output operand has a shape of (50,1) which means it has 50 rows and 1 column, while the broadcast shape has a shape of (50,5) which means it has 50 rows and 5 columns.
Broadcasting is a way to perform operations on arrays with different shapes by stretching or duplicating one or more of the arrays to match the shape of the other. However, in this case, the arrays cannot be broadcasted because they have different numbers of columns.
To fix this error, you need to reshape one of the arrays to have the same number of columns as the other. You can use the numpy.reshape() function to do this. For example, if you want to reshape the output operand to have the same shape as the broadcast shape, you can use the following code:
```
import numpy as np
# create two arrays with incompatible shapes
a = np.ones((50,1))
b = np.ones((50,5))
# reshape the output operand to have the same shape as the broadcast shape
a = np.reshape(a, (50,5))
# perform the broadcast operation
c = a + b
```
This code will reshape the output operand to have the same shape as the broadcast shape, and then perform the broadcast operation without any errors.
non-broadcastable output operand with shape (10,1) doesn't match the broadcast shape (10,29)
This error message suggests that there is an issue with the shapes of the arrays being used in a broadcast operation. Specifically, there is a mismatch between the shape of the output array and the shape that is being broadcasted.
In this case, the output array has a shape of (10,1), which means it has 10 rows and 1 column. However, the shape that is being broadcasted has a shape of (10,29), which means it has 10 rows and 29 columns. These shapes are not compatible because the number of columns in the output array does not match the number of columns being broadcasted.
To resolve this error, you should check the arrays being used in the broadcast operation and make sure they have compatible shapes. You may need to reshape one or both of the arrays to ensure they have the same number of columns. Alternatively, you may need to adjust the broadcasting axis to match the shape of the output array.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)