ValueError: non-broadcastable output operand with shape (45,1) doesn't match the broadcast shape (45,5)
时间: 2024-05-13 15:20:39 浏览: 118
python3.7解决最小二乘遇到ValueError:Expected 2D array, got 1D array instead: array=[5.].关于reshape和predict
This error occurs when you are trying to perform an operation that requires two arrays to have the same shape, but they do not. Specifically, the error message is telling you that you are trying to assign the result of an operation to an array with shape (45,1), but the operation result has shape (45,5), which cannot be broadcast to match the shape of the output array.
To fix this error, you need to make sure that the arrays you are working with have compatible shapes. One possible solution is to reshape the output array to match the shape of the operation result. For example:
```
import numpy as np
a = np.random.rand(45, 5)
b = np.random.rand(45, 1)
c = a * b.reshape(45, 1)
```
In this example, we reshape the array `b` to have shape (45, 1) so that it can be broadcast with `a`. The result of the multiplication is an array with shape (45, 5), but we can reshape it to have shape (45, 1) by taking the sum of each row:
```
d = c.sum(axis=1, keepdims=True)
```
Now `d` has shape (45, 1) and can be assigned to an array with the same shape.
阅读全文