Numpy `ValueError: operands could not be broadcast together with shape …`

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

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)

回答1:

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.

I would recommend looking at the numpy broadcasting rules.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!