Broadcasting and Vectorized Operations
How broadcasting works, element-wise operations, avoiding loops with vectorization.
Why Vectorize?
NumPy operates on whole arrays at C speed. Replacing Python loops with array expressions, called vectorization, is often 10 to 100 times faster.
Element-wise Operations
Arithmetic operators apply element by element when shapes match. No loop required.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
print(a + b) # [11 22 33]
print(a * b) # [10 40 90]All lessons in this course
- Array Creation and Properties
- Indexing, Slicing, and Fancy Indexing
- Broadcasting and Vectorized Operations
- Linear Algebra with NumPy