Array Operations and Broadcasting
Perform vectorized arithmetic and understand broadcasting rules.
Vectorised Arithmetic
NumPy applies arithmetic element-wise across entire arrays without Python loops — executed in optimised C code.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(a + b) # [5 7 9]
print(a * b) # [4 10 18]
print(a ** 2) # [1 4 9]Universal Functions (ufuncs)
NumPy's ufuncs apply element-wise C-level functions: np.sqrt, np.exp, np.log, np.sin, etc.
import numpy as np
x = np.linspace(0, np.pi, 5)
print(np.sin(x)) # [0. 0.707 1. 0.707 0.]
print(np.exp([1.0, 2.0, 3.0])) # [2.718 7.389 20.086]All lessons in this course
- NumPy Arrays and dtypes
- Array Operations and Broadcasting
- Indexing, Slicing, and Fancy Indexing
- Linear Algebra with NumPy