Error database

ValueError: operands could not be broadcast together with shapes

NumPy could not line up two array shapes for an element-wise operation. Print both shapes, then add the missing axis with keepdims or reshape.

The message you saw
ValueError: operands could not be broadcast together with shapes

By Updated

The error

Output
ValueError: operands could not be broadcast together with shapes (1000,3) (1000,)

PyTorch reports the same problem with different words:

Output
RuntimeError: The size of tensor a (3) must match the size of tensor b (1000) at non-singleton dimension 1

What it means

You combined two arrays element by element — with +, -, * or a comparison. Their shapes could not be matched up.

Broadcasting is NumPy's rule for stretching a smaller array across a bigger one. It compares shapes from the right. Two dimensions are compatible when they are equal, or when one of them is 1. A missing dimension counts as 1.

So (1000, 3) and (3,) work: the 3s line up, and the missing axis stretches. But (1000, 3) and (1000,) fail: the rightmost pair is 3 versus 1000.

Why it happens

The most common route is a reduction that dropped an axis. X.mean(axis=1) on a (1000, 3) array returns shape (1000,). Subtracting that from X compares 3 with 1000 and fails.

The second route is mixing row-shaped and column-shaped data. A flat array (N,) and a column (N, 1) behave differently. Combining (N,) with (N, 1) does not error — it broadcasts to (N, N). That is usually a silent bug that shows up later as this error, or as a memory explosion.

How to fix it

1. Print both shapes before the failing line. The message names the shapes, but you need to know which variable is which.

python
print(a.shape, b.shape)

2. Keep the reduced axis with keepdims=True. This is the fix for the mean/sum/max family.

python
X_centered = X - X.mean(axis=1, keepdims=True)   # (1000,3) - (1000,1) works

3. Add the axis yourself with reshape or np.newaxis.

python
b = b.reshape(-1, 1)        # (1000,) -> (1000,1)
b = b[:, np.newaxis]        # same thing

4. Flatten a stray column back to 1D when you meant a flat array.

python
y = y.ravel()               # (1000,1) -> (1000,)

scikit-learn wants targets flat, and this silences its column-vector warning too.

5. In PyTorch, use unsqueeze for the same job.

python
b = b.unsqueeze(1)          # (1000,) -> (1000,1)

How to prevent it

Write the expected shape as a comment next to any line that changes it. Add cheap asserts at function boundaries, like assert X.ndim == 2. Be suspicious the moment (N,) and (N, 1) both exist in the same function — pick one convention and convert at the edges.

The lessons behind this error.

  • Python for AI

    NumPy

    NumPy lets you do one operation to millions of numbers at once instead of one at a time. It is the foundation every AI library in Python is built on.

Back to all errors