yayu
yayu

Reputation: 8098

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

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.

Upvotes: 217

Views: 1251760

Answers (12)

Patrick Cree
Patrick Cree

Reputation: 39

While the question is quite old, I didn't see this answer here yet, hence posting it.

The main difference is array multiplication vs matrix multiplication. For matrix multiplications you should use np.matmul which has the output you requested which is [n,m] * [m,l] = [n,l].

See the example below:

x1 = np.arange(12.0).reshape((4, 3))
x2 = np.arange(3).reshape((3))

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

x2
array([0, 1, 2])

x1 * x2
array([[ 0.,  1.,  4.],
       [ 0.,  4., 10.],
       [ 0.,  7., 16.],
       [ 0., 10., 22.]])

np.multiply(x1, x2)
array([[ 0.,  1.,  4.],
       [ 0.,  4., 10.],
       [ 0.,  7., 16.],
       [ 0., 10., 22.]])

np.matmul(x1, x2)
array([ 5., 14., 23., 32.])

Upvotes: 0

Ashish Gusian
Ashish Gusian

Reputation: 41

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,

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

Upvotes: 0

user17957342
user17957342

Reputation:

Error: operands could not be broadcast together with shapes (2,3) (2,3,3)

This kind of error occur when the two array does not have the same shape.

to correct this you need reshape one array to match the other.

see example below

a1 = array([1, 2, 3]), shape = (2,3)

a3 =array([[[1., 2., 3.], 
        [2., 3., 2.],
        [2., 4., 5.]],

       [[1., 0., 3.],
        [2., 3., 7.],
        [2., 4., 6.]]])

with shape = (2,3,3)

IF i try to run np.multiply(a2,a3) it will return the error below

Error: operands could not be broadcast together with shapes (2,3) (2,3,3)

to solve this check out the broadcating rules

which state hat Two dimensions are compatible when:
#1.they are equal, or
#2.one of them is 1`

Therefore lets reshape a2.

reshaped = a2.reshape(2,3,1)

Now try to run np.multiply(reshaped,a3)

the multiplication will run SUCCESSFUL!!

Upvotes: 0

Mdy Erfani
Mdy Erfani

Reputation: 1

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

Upvotes: 0

sadra imanyfar
sadra imanyfar

Reputation: 160

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.

Upvotes: 0

Kenan
Kenan

Reputation: 14124

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.

Upvotes: 14

Azzam Radman
Azzam Radman

Reputation: 53

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

X = np.matrix(X)

y = np.matrix(y)

X*y

Upvotes: 1

DrV
DrV

Reputation: 23550

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)

Upvotes: 140

chia yongkang
chia yongkang

Reputation: 782

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

Upvotes: 1

iamanigeeit
iamanigeeit

Reputation: 834

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

Upvotes: 27

user3101695
user3101695

Reputation: 121

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

Upvotes: 12

o-90
o-90

Reputation: 17613

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]])

Upvotes: 44

Related Questions