# [Solved] Numpy `ValueError: operands could not be broadcast together with shape …`

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Numpy `ValueError: operands could not be broadcast together with shape ...` in python. So Here I am Explain to you all the possible solutions here.

## How Numpy `ValueError: operands could not be broadcast together with shape ...` Error Occurs?

Today I get the following error Numpy `ValueError: operands could not be broadcast together with shape ...` in python.

## How To Solve Numpy `ValueError: operands could not be broadcast together with shape ...` Error ?

1. How To Solve Numpy `ValueError: operands could not be broadcast together with shape ...` Error ?

To Solve Numpy `ValueError: operands could not be broadcast together with shape ...` Error 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:

2. Numpy `ValueError: operands could not be broadcast together with shape ...`

To Solve Numpy `ValueError: operands could not be broadcast together with shape ...` Error 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:

## Solution 1

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 2

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

## Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.