Python
NumPy
ValueError
Broadcasting
Array Shapes

NumPy ValueError: operands could not be broadcast together with shapes

Codemia Knowledge Hub ·

This error means you asked NumPy for an elementwise operation on arrays whose shapes cannot be aligned under the broadcasting rules. Inspect the shapes, decide which axis should expand, then reshape, transpose, or change the operation so the arrays are actually compatible.

The broadcasting rule

NumPy compares shapes starting from the rightmost dimension. Two dimensions are compatible if they are equal or one of them is 1.

python
1import numpy as np
2
3a = np.array([[1, 2, 3], [4, 5, 6]])      # shape (2, 3)
4b = np.array([10, 20, 30])                 # shape (3,)
5
6print(a + b)

This works because b is treated like shape (1, 3) and broadcast across the rows of a.

A shape pair that fails

python
1import numpy as np
2
3a = np.ones((2, 3))
4b = np.ones((2, 2))
5
6print(a + b)

NumPy raises:

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

The last dimensions are 3 and 2, and neither is 1, so they cannot be aligned.

Check the shapes right before the failing operation:

python
print(a.shape)
print(b.shape)

Many of these bugs come from an earlier step that produced (n, 1) instead of (n,), or swapped rows and columns. Look at the pipeline that built the arrays, not just the failing line.

Fix 1: Reshape the smaller array

If one dimension should expand, give it size 1 explicitly:

python
1import numpy as np
2
3a = np.array([[1, 2, 3], [4, 5, 6]])      # shape (2, 3)
4b = np.array([[10], [20]])                # shape (2, 1)
5
6print(a + b)

(2, 1) broadcasts across the columns to match (2, 3).

Fix 2: Add a new axis

To turn a one-dimensional array into a column vector:

python
1import numpy as np
2
3x = np.array([1, 2])
4y = np.array([[10, 20, 30], [40, 50, 60]])
5
6result = x[:, np.newaxis] + y
7print(result)

x[:, np.newaxis] changes the shape from (2,) to (2, 1), which fixes many row versus column mismatches.

Fix 3: Transpose when the axes are reversed

python
1import numpy as np
2
3a = np.ones((3, 2))
4b = np.array([10, 20])
5
6print(a + b)

This works because the last dimension is 2. If your array is (2, 3) when you meant (3, 2), a transpose is the real fix, not a forced reshape.

Elementwise * is not matrix multiplication

The error often shows up when matrix multiplication was intended but * was written:

python
1import numpy as np
2
3A = np.ones((2, 3))
4B = np.ones((3, 2))
5
6# Elementwise multiply fails here.
7# print(A * B)
8
9print(A @ B)

Use @ or np.matmul for matrix multiplication. Broadcasting applies to elementwise operations, not linear algebra.

Misconceptions that cause the error

  • Arrays with the same total number of elements cannot always be combined. Broadcasting only looks at compatible dimensions, not total size.
  • A vector of shape (3,) behaves differently from a column vector of shape (3, 1).

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design