[python] ValueError: shape mismatch: objects cannot be broadcast to a single shape

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.

This question is related to python valueerror pearson

The answer is


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.