Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 214
Alex Brashear Avatar asked Jun 05 '13 21:06

Alex Brashear


People also ask

How do you fix Valueerror shape mismatch objects Cannot be broadcast to a single shape?

The error is occurring because you are trying to compute differently shaped variables. Adjusting the shapes of the variables before using them in a mathematical expression can help you in resolving the error. You should ensure that the lengths of the vectors are the same before plotting them to solve the above error.

What is shape mismatch?

In this article we will see Python code to resolve valueerror: shape mismatch: objects cannot be broadcast to a single shape. This error occurs when 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).


1 Answers

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.

like image 195
AMacK Avatar answered Sep 22 '22 23:09

AMacK