Numpy`ValueError:操作数不能与形状一起播放......`

时间:2022-01-07 21:23:27

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

我正在使用python 2.7,我正在尝试预测从1.00000000到3.0000000008的一些随机数据。我的阵列中有大约196个项目,我收到错误

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)

1 个解决方案

#1


17  

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.

如果X和beta与最后一行(即nsample)的rhs中的第二项形状不同,那么您将得到此类错误。要将数组添加到数组元组,它们都必须是相同的形状。

I would recommend looking at the numpy broadcasting rules.

我建议看看numpy广播规则。

#1


17  

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.

如果X和beta与最后一行(即nsample)的rhs中的第二项形状不同,那么您将得到此类错误。要将数组添加到数组元组,它们都必须是相同的形状。

I would recommend looking at the numpy broadcasting rules.

我建议看看numpy广播规则。