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.
This works because b is treated like shape (1, 3) and broadcast across the rows of a.
A shape pair that fails
NumPy raises:
The last dimensions are 3 and 2, and neither is 1, so they cannot be aligned.
Print the shapes first
Check the shapes right before the failing operation:
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:
(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:
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
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:
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).
Related reading
- python pandas apply a function with arguments to a series
- Python Pandas Convert .value_counts output to dataframe
- Python pandas Filtering out nan from a data selection of a column of strings
- Python Pandas Get index of rows where column matches certain value
- Python Sets vs Lists
- python swapped tuple to dict
- Python OCR ignore signatures in documents
- Python operation vs is not
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.