broadcast error 🐍 Python

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

Two NumPy arrays have shapes that can’t be combined element-wise.

Seen on: Python

Meaning

Broadcasting aligns dimensions from the right; each pair must be equal or 1. Mismatched lengths, a missing axis or a transposed matrix cause it.

Common causes

  • Arrays of different lengths
  • Column vs row vector confusion
  • Wrong axis after reshape/transpose

⚡ Quick fix

  1. Print .shape of both arrays
  2. Reshape with x[:, None] or x.reshape(-1, 1)
  3. Fix the data so lengths match

Detailed fix by platform

Python

  1. a = a.reshape(-1, 1) # (3,) -> (3, 1) to broadcast against (1, 4)

How to diagnose

  1. Shapes — Which shapes are shown?
  2. Intent — Element-wise or matrix multiplication (@)?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.