C.W.K.
Stream
Lesson 02 of 07 · published

Vectorization & Broadcasting — The Mental Shift

~14 min · numpy, broadcasting, vectorization

Level 0Curious Reader
0 XP0/47 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Stop writing loops

The deepest habit you bring from non-array languages is writing element-wise operations as loops: for i in range(len(x)): result[i] = x[i] * 2. NumPy lets you say result = x * 2 and produces the same answer, faster, with less code, and (this is the underrated part) with less room for off-by-one errors.

The harder shift is two-array operations. With Python lists you'd zip(a, b) and write a comprehension. With NumPy you write a * b — and as long as the shapes are compatible, NumPy figures out the rest.

Broadcasting in one paragraph

Broadcasting is NumPy's set of rules for combining arrays of different shapes. The rules: align trailing dimensions; a dimension of size 1 stretches to match; missing dimensions are treated as size 1. The reason you care: it lets you say "add this row vector to every row of this matrix" or "subtract this scalar from every element" without writing a single loop, and without allocating intermediate copies.

Code

Vectorization — element-wise ops with no loops·python
import numpy as np

x = np.arange(10)            # 0..9

doubled = x * 2              # element-wise scalar multiply
shifted = x + 100            # element-wise scalar add
squared = x ** 2             # element-wise power
evens   = x[x % 2 == 0]      # boolean mask filter

y = np.linspace(0, 1, 10)    # 10 floats from 0 to 1
weighted_sum = (x * y).sum() # vectorized multiply, then reduce
Broadcasting — different shapes, same operation·python
import numpy as np

# A 2D matrix and a 1D row vector
matrix = np.arange(12).reshape(3, 4)        # shape (3, 4)
row    = np.array([10, 20, 30, 40])         # shape (4,)

# Broadcasting: the (4,) is treated as (1, 4) and stretched to (3, 4)
result = matrix + row
# every row of `matrix` gets row added to it

# A column vector — note the explicit (3, 1) shape
col = np.array([100, 200, 300]).reshape(3, 1)  # shape (3, 1)
result_col = matrix + col
# every column of `matrix` gets col added to it

# Z-score normalization — broadcasting in two directions
data = np.random.default_rng(0).normal(size=(1000, 5))
z = (data - data.mean(axis=0)) / data.std(axis=0)   # subtract per-column mean, divide by per-column std

External links

Exercise

Build a (1000, 3) array of random points in 3D, then compute each point's distance to the origin without writing a Python loop. Use broadcasting and np.sqrt. Then compute the index of the closest and farthest points with argmin / argmax.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.