Understanding broadcasting prevents unnecessary loops and heavy memory duplication in vectorized computations. For related shape manipulation and alignment mechanics, review shape and reshape().
Show Table of ContentsBroadcasting lets NumPy perform arithmetic between arrays of different but compatible shapes without copying data. A smaller array is virtually stretched along dimensions of size 1 to match the larger array.
equal or either is 1.1, broadcasting fails with a ValueError.# Example A: (3, 4) +/- (4,) -> OK
# Align from right: (3, 4)
# ( 4) <-- 1D treated as (1,4), row-wise
# Result: (3,4)
# Example B: (3, 1) + (1, 4) -> OK
# Align: (3, 1)
# (1, 4)
# Result: (3,4) (each 1 expands)
# Example C: (2, 3) + (2,) -> FAIL
# Align: (2, 3)
# ( 2)
# 3 != 2 and neither is 1 -> ValueError
import numpy as np
A = np.arange(12).reshape(3, 4)
row = np.array([10, 20, 30, 40]) # shape (4,)
col = np.array([[100], [200], [300]]) # shape (3,1)
print(A + row) # adds to each row
print(A + col) # adds to each column group
assert (A + row).shape == (3, 4)
NumPy 1D arrays have shape (N,) with no distinct row or column orientation. Use np.newaxis or reshape to define explicit 2D orientations:
v = np.array([1, 2, 3, 4]) # shape (4,)
row_v = v[np.newaxis, :] # (1,4) row vector
col_v = v[:, np.newaxis] # (4,1) column vector
M = np.arange(8).reshape(2, 4)
print(M + row_v) # OK: (2,4) + (1,4) -> (2,4)
print(M + col_v[:2]) # OK: (2,4) + (2,1) -> (2,4)
assert (M + row_v).shape == (2, 4)
T = np.arange(2*3*4).reshape(2, 3, 4) # (depth=2, rows=3, cols=4)
bias_row = np.array([1, 2, 3, 4]) # (4,)
bias_depth = np.array([100, 200]) # (2,)
print(T + bias_row) # (2,3,4) + (4,) -> (2,3,4)
print(T + bias_depth[:, None, None]) # (2,3,4) + (2,1,1) -> (2,3,4)
assert (T + bias_row).shape == (2, 3, 4)
print(arr.shape), then align dimensions using np.newaxis or reshape.(1, N) or (N, 1) depending on whether row-wise or column-wise broadcasting is intended.# Standardize columns: (N, D) array -> subtract column means, divide by std
X = np.array([
[1.0, 2.0, 3.0],
[2.0, 3.0, 4.0],
[3.0, 4.0, 5.0]
])
mu = X.mean(axis=0) # (D,)
sigma = X.std(axis=0) # (D,)
Xz = (X - mu) / sigma # (N,D) - (D,) -> (N,D)
print("Standardized:
", np.round(Xz, 2))
assert Xz.shape == (3, 3)
a = np.arange(6).reshape(2, 3) # (2,3)
b = np.array([10, 20]) # (2,)
# a + b -> ValueError (because 3 != 2, neither is 1)
# Fix: treat b as column vector (2,1)
b_col = b[:, np.newaxis]
print(a + b_col) # (2,3) + (2,1) -> (2,3)
assert (a + b_col).shape == (2, 3)
Test broadcasting rules, np.newaxis orientation, tensor alignment, and run verification test suites in our interactive Google Colab companion notebook:
Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:
View on GitHubContinue exploring related NumPy operations: Math & ufuncs, shape, reshape(), Indexing & Slicing, where(), and Axis Reduction.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.