ValueError: shape mismatch: objects cannot be broadcast to a single shape

无人久伴 提交于 2019-12-09 14:03:41

问题


I am using the SciPy's pearsonr(x,y) method and I cannot figure out why the following error is happening:

ValueError: shape mismatch: objects cannot be broadcast to a single shape

It computes the first two (I am running several thousand of these tests in a loop) and then dies. Does anyone have any ideas about what the problem might be?

r_num = n*(np.add.reduce(xm*ym))

this is the line in the pearsonr method that the error occurs on, any help would be much appreciated.


回答1:


This particular error implies that one of the variables being used in the arithmetic on the line has a shape incompatible with another on the same line (i.e., both different and non-scalar). Since n and the output of np.add.reduce() are both scalars, this implies that the problem lies with xm and ym, the two of which are simply your x and y inputs minus their respective means.

Based on this, my guess is that your x and y inputs have different shapes from one another, making them incompatible for element-wise multiplication.

** Technically, it's not that variables on the same line have incompatible shapes. The only problem is when two variables being added, multiplied, etc., have incompatible shapes, whether the variables are temporary (e.g., function output) or not. Two variables with different shapes on the same line are fine as long as something else corrects the issue before the mathematical expression is evaluated.



来源:https://stackoverflow.com/questions/16950074/valueerror-shape-mismatch-objects-cannot-be-broadcast-to-a-single-shape

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