Vectorized Covariance Matrix Calculation
import numpy as np
def fast_covariance(X):
# X is an (n_samples, n_features) matrix
n = X.shape[0]
X_centered = X - X.mean(axis=0)
# Using the dot product is significantly faster than np.cov for large matrices
return (X_centered.T @ X_centered) / (n - 1)
Why it matters: Center once, multiply once. Large feature banks compute faster when you skip Python loops and lean on vectorized math.