问题描述
我正在使用 python 2.7 并尝试对从 1.00000000 到 3.0000000008 的一些随机数据进行预测.我的数组中有大约 196 个项目,我收到错误
Im using python 2.7 and am attempting a forcasting on some random data from 1.00000000 to 3.0000000008. There are approx 196 items in my array and I get the error
ValueError: operands could not be broadcast together with shape (2) (50)
我自己似乎无法解决这个问题.任何帮助或相关文档的链接将不胜感激.
I do not seem to be able to resolve this issue on my own. Any help or links to relevant documentation would be greatly appreciated.
这是我正在使用的生成此错误的代码
Here is the code I am using that generates this error
nsample = 50
sig = 0.25
x1 = np.linspace(0,20, nsample)
X = np.c_[x1, np.sin(x1), (x1-5)**2, np.ones(nsample)]
beta = masterAverageList
y_true = ((X, beta))
y = y_true + sig * np.random.normal(size=nsample)
推荐答案
如果 X
和 beta
与你的rhs 中的第二项的形状不同最后一行(即nsample
),那么你会得到这种类型的错误.要将数组添加到数组元组中,它们的形状必须相同.
If X
and beta
do not have the same shape as the second term in the rhs of your last line (i.e. nsample
), then you will get this type of error. To add an array to a tuple of arrays, they all must be the same shape.
我建议查看 numpy 广播规则.
这篇关于Numpy`ValueError:操作数无法与形状一起广播......`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!