Introduction
NumPy provides efficient vectorized operations for mathematical computations.
Mathematical Functions
arr = np.array([1, 2, 3, 4, 5])
# Trigonometric
np.sin(arr)
np.cos(arr)
np.tan(arr)
# Exponential and log
np.exp(arr) # e^1, e^2, ...
np.log(arr) # Natural log
np.log10(arr) # Base 10 log
np.log2(arr) # Base 2 log
# Power and roots
np.sqrt(arr) # Square root
np.power(arr, 3) # Cubed
Array Manipulation
arr = np.array([[1, 2], [3, 4]])
# Transpose
arr.T
# Reshape
arr.reshape(4, 1)
# Flatten
arr.flatten()
# Concatenate
np.concatenate([arr, arr], axis=0) # Vertical
np.concatenate([arr, arr], axis=1) # Horizontal
Statistical Functions
arr = np.array([1, 2, 3, 4, 5])
np.min(arr)
np.max(arr)
np.mean(arr)
np.median(arr)
np.std(arr) # Standard deviation
np.var(arr) # Variance
np.percentile(arr, 75) # 75th percentile
Practice Problems
- Normalize array to 0-1 range
- Calculate moving average
- Find correlation between arrays
- Implement matrix multiplication
- Compute covariance matrix