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

12

214

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.

Pesthole answered 3/7, 2014 at 17:52 Comment(4)
What is the "the original question"? X*y shouldn't work (and it doesn't), but np.dot(X,y) and X.dot(y)) should work (and for me they do).Plaster
* isn't matrix multiplication for ndarray objects.Julejulee
I got into the same problem when solving w.T * X, when this should be np.dot(w.T,X)Tiddly
X*y does element wise multiplicationBrownstone
B
138

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)
Ber answered 3/7, 2014 at 22:8 Comment(0)
V
43

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]])
Verein answered 3/7, 2014 at 22:10 Comment(0)
L
27

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

Landlordism answered 29/11, 2019 at 9:3 Comment(0)
F
14

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.

Feudalism answered 27/4, 2017 at 15:2 Comment(1)
The error message is indeed misleading, as this seems to appear when your matrix dimensions are wrong for the element-wise multiplication.Crashland
A
12

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

Accidence answered 13/11, 2018 at 9:32 Comment(0)
T
1

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

Ternary answered 4/5, 2020 at 14:26 Comment(0)
M
1

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

X = np.matrix(X)

y = np.matrix(y)

X*y
Mortar answered 3/2, 2021 at 5:32 Comment(0)
J
0
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
Jew answered 14/7, 2021 at 3:50 Comment(2)
please do not post only code , Explain more to help othersPsychogenic
sorry sir, I'm new in this platform. Will you help me? What can I do better?Jew
S
0

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.

Sherwin answered 24/9, 2021 at 7:9 Comment(0)
D
0

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

Derr answered 30/12, 2021 at 13:6 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Dashpot
The OP gives no indication that X is a np.matrix. (n,m) and (n,1) are both valid shapes for numpy arrays, and both are 2d.Martyrology
C
0

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!!

Corrales answered 17/8, 2022 at 3:28 Comment(0)
S
0

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.])
Sleepwalk answered 23/6, 2023 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.