Lesson 10 of 60 · python
Mathematical Operations with NumPy
Duration: 25 minutes
Mathematical Operations
NumPy provides a rich set of universal functions (ufuncs) for element‑wise math.
Basic arithmetic
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
print(x + y) # [5 7 9]
print(x * y) # [4 10 18]
print(x ** 2) # [1 4 9]
Trigonometric functions
angles = np.deg2rad(np.array([0, 30, 45, 60, 90]))
print(np.sin(angles))
Aggregations
arr = np.random.randn(1000)
print("Mean:", arr.mean())
print("Std:", arr.std())
print("Median:", np.median(arr))
Linear Algebra utilities
A = np.array([[1, 2], [3, 4]])
B = np.linalg.inv(A)
print("Inverse of A:\n", B)
print("Determinant:", np.linalg.det(A))
Random sampling
rng = np.random.default_rng(seed=42)
samples = rng.normal(loc=0, scale=1, size=5)
print(samples)