Page3/12
Broadcasting & Linear Algebra · Page 1 of 1
Broadcasting Rules
Broadcasting & Linear Algebra
What is Broadcasting?
Broadcasting is NumPy's ability to apply operations on arrays of different shapes without making explicit copies.
Rule of Thumb
NumPy compares shapes element-wise from the trailing dimensions forward. Two dimensions are compatible if they are equal or one of them is 1.
# Shape (3, 3) + Shape (3,) → Works!
matrix = np.ones((3, 3))
row = np.array([1, 2, 3])
result = matrix + row # row is "broadcast" to all 3 rows
Essential Math Operations
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.add(a, b) # Element-wise addition
np.multiply(a, b) # Element-wise multiplication (Hadamard)
np.dot(a, b) # Dot product (1*4 + 2*5 + 3*6 = 32)
⚠️ Never use
*for matrix multiplication. Always usenp.dot()or the@operator.
main.py
Loading...
OUTPUT
▶Click "Run Code" to execute…