python numpy ValueError: operands could not be broadcast together with shapes

PythonNumpy

Python Problem Overview


In numpy, I have two "arrays", X is (m,n) and y is a vector (n,1)

using

X*y

I am getting the error

ValueError: operands could not be broadcast together with shapes (97,2) (2,1) 

When (97,2)x(2,1) is clearly a legal matrix operation and should give me a (97,1) vector

EDIT:

I have corrected this using X.dot(y) but the original question still remains.

Python Solutions


Solution 1 - Python

dot is matrix multiplication, but * does something else.

We have two arrays:

  • X, shape (97,2)
  • y, shape (2,1)

With Numpy arrays, the operation

X * y

is done element-wise, but one or both of the values can be expanded in one or more dimensions to make them compatible. This operation is called broadcasting. Dimensions, where size is 1 or which are missing, can be used in broadcasting.

In the example above the dimensions are incompatible, because:

97   2
 2   1

Here there are conflicting numbers in the first dimension (97 and 2). That is what the ValueError above is complaining about. The second dimension would be ok, as number 1 does not conflict with anything.

For more information on broadcasting rules: http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html

(Please note that if X and y are of type numpy.matrix, then asterisk can be used as matrix multiplication. My recommendation is to keep away from numpy.matrix, it tends to complicate more than simplifying things.)

Your arrays should be fine with numpy.dot; if you get an error on numpy.dot, you must have some other bug. If the shapes are wrong for numpy.dot, you get a different exception:

ValueError: matrices are not aligned

If you still get this error, please post a minimal example of the problem. An example multiplication with arrays shaped like yours succeeds:

In [1]: import numpy

In [2]: numpy.dot(numpy.ones([97, 2]), numpy.ones([2, 1])).shape
Out[2]: (97, 1)

Solution 2 - Python

Per numpy docs: > When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when: >

  • they are equal, or
  • one of them is 1

In other words, if you are trying to multiply two matrices (in the linear algebra sense) then you want X.dot(y) but if you are trying to broadcast scalars from matrix y onto X then you need to perform X * y.T.

Example:

>>> import numpy as np
>>>
>>> X = np.arange(8).reshape(4, 2)
>>> y = np.arange(2).reshape(1, 2)  # create a 1x2 matrix
>>> X * y
array([[0,1],
       [0,3],
       [0,5],
       [0,7]])

Solution 3 - Python

You are looking for np.matmul(X, y). In Python 3.5+ you can use X @ y.

Solution 4 - Python

It's possible that the error didn't occur in the dot product, but after. For example try this

a = np.random.randn(12,1)
b = np.random.randn(1,5)
c = np.random.randn(5,12)
d = np.dot(a,b) * c

np.dot(a,b) will be fine; however np.dot(a, b) * c is clearly wrong (12x1 X 1x5 = 12x5 which cannot element-wise multiply 5x12) but numpy will give you

ValueError: operands could not be broadcast together with shapes (12,1) (1,5)

The error is misleading; however there is an issue on that line.

Solution 5 - Python

Use np.mat(x) * np.mat(y), that'll work.

Solution 6 - Python

We might confuse ourselves that a * b is a dot product.

But in fact, it is broadcast.

Dot Product : a.dot(b)

Broadcast:

> The term broadcasting refers to how numpy treats arrays with different > dimensions during arithmetic operations which lead to certain > constraints, the smaller array is broadcast across the larger array so > that they have compatible shapes.

(m,n) +-/* (1,n) → (m,n) : the operation will be applied to m rows

Solution 7 - Python

Convert the arrays to matrices, and then perform the multiplication.

X = np.matrix(X)

y = np.matrix(y)

X*y

Solution 8 - Python

ValueError: operands could not be broadcast together with shapes (x ,y) (a ,b)
where  x ,y are variables

Basically this error occurred when value of y (no. of columns) doesn't equal to the number of elements in another multidimensional array.

Now let's go through by ex=>
coding  apart

    {
        import numpy as np 
        arr1= np.arange(12).reshape(3,4)
    }

output of arr1

array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

 

    {   arr2= np.arange(4).reshape(1,4)
          or                   { both are same 1 rows and 4 columns
        arr2= np.arange(4)
    }

ouput of arr2=>

array([0, 1, 2, 3])
 
no of elements in arr2 is equal no of no. of the columns in arr1 it will be excute.

    {
    for x,y in np.nditer([a,b]):
        print(x,y)
    }

output => 0 0
1 1
2 2
3 3
4 0
5 1
6 2
7 3
8 0
9 1
10 2
11 3



Solution 9 - Python

we should consider two points about broadcasting. first: what is possible. second: how much of the possible things is done by numpy.

I know it might look a bit confusing, but I will make it clear by some example.

lets start from the zero level.

suppose we have two matrices. first matrix has three dimensions (named A) and the second has five (named B). numpy tries to match last/trailing dimensions. so numpy does not care about the first two dimensions of B. then numpy compares those trailing dimensions with each other. and if and only if they be equal or one of them be 1, numpy says "O.K. you two match". and if it these conditions don't satisfy, numpy would "sorry...its not my job!".

But I know that you may say comparison was better to be done in way that can handle when they are devisable(4 and 2 / 9 and 3). you might say it could be replicated/broadcasted by a whole number(2/3 in out example). and i am agree with you. and this is the reason I started my discussion with a distinction between what is possible and what is the capability of numpy.

Solution 10 - Python

This is because X and y are not the same types. for example X is a numpy matrix and y is a numpy array!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyayuView Question on Stackoverflow
Solution 1 - PythonDrVView Answer on Stackoverflow
Solution 2 - Pythono-90View Answer on Stackoverflow
Solution 3 - PythoniamanigeeitView Answer on Stackoverflow
Solution 4 - PythonKenanView Answer on Stackoverflow
Solution 5 - Pythonuser3101695View Answer on Stackoverflow
Solution 6 - Pythonchia yongkangView Answer on Stackoverflow
Solution 7 - PythonAzzam RadmanView Answer on Stackoverflow
Solution 8 - PythonAshish GusianView Answer on Stackoverflow
Solution 9 - Pythonsadra imanyfarView Answer on Stackoverflow
Solution 10 - PythonMdy ErfaniView Answer on Stackoverflow