numpy的`ValueError错误:操作数无法与形状一起播...` [英] Numpy `ValueError: operands could not be broadcast together with shape ...`
问题描述
即时通讯使用Python 2.7版,并在尝试从1.00000000为3.0000000008一些随机数据的测报。有在我的数组约196项目,我得到的错误
ValueError错误:操作数无法与形状一起播出(2)(50)
我似乎并没有能够解决我自己的这个问题。任何帮助或链接到相关的文件将是极大的AP preciated。
下面是code我使用的产生这个错误
= NSample个50
SIG = 0.25
X1 = np.linspace(0,20,NSample个)
X = np.c_ [X1,np.sin(X1),(x1-5)** 2,np.ones(NSample个)]
公测= masterAverageList
y_true =((X,β))
Y = y_true + SIG * np.random.normal(大小= NSample个)
如果 X
和测试版
没有外形一样在你的上线(即 NSample个
)的RHS的第二个任期,那么你会得到这种类型的错误。到阵列添加到阵列的一个元组,它们都必须是相同的形状。
我会建议看 numpy的广播规则。
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)
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.
这篇关于numpy的`ValueError错误:操作数无法与形状一起播...`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!