TGViewer
Data science/ML/AI Data science/ML/AI @datascience_bds · 14K subscribers
Post #1372 126
🧮 Why does NumPy let you add a number to an entire matrix?

Look at this:
import numpy as np

x = np.array([
[10, 20, 30],
[40, 50, 60]
])

x + 5

You get:
[[15, 25, 35],
[45, 55, 65]]

But where did the 5 go? You didn't write a loop. You didn't copy 5 six times.

This is broadcasting.
NumPy automatically treats the smaller object as if it were expanded to match the larger array's shape.

So conceptually:
[[10, 20, 30],       [[5, 5, 5],
[40, 50, 60]] + [5, 5, 5]]

Then it adds them element by element.

And it works with arrays too:
x + np.array([1, 2, 3])

Conceptually:
[[10, 20, 30],       [[1, 2, 3],
[40, 50, 60]] + [1, 2, 3]]

Result:
[[11, 22, 33],
[41, 52, 63]]


The important part:
Broadcasting doesn't literally copy the data into a bigger array.
NumPy applies the operation as if the smaller array had been expanded, while avoiding unnecessary copies.

That's why this:
x + 5

can work even though x is 2D and 5 is just a scalar.
More from @datascience_bds
  1. Oct 10, 2026document post
  2. Oct 9, 20268 RAG architectures for AI Engineers, visually explained:
  3. Oct 8, 2026document post
  4. Oct 7, 2026🧮 NumPy: Why axis=0 and axis=1 Feel Backwards You've probably seen: np.mean(X, axis=0) an…
  5. Oct 6, 2026document post
  6. Oct 5, 2026📊 Pandas Cheatsheet Every Data Analyst Should Save Pandas is one of the most important to…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →