Challenge: Whitening via Eigenvalue Decomposition
Opgave
Swipe to start coding
You are given a dataset X (2D NumPy array) with correlated features.
Your goal is to perform feature whitening — transforming the data so that features become uncorrelated and have unit variance, using eigenvalue decomposition of the covariance matrix.
Steps:
- Center the data (subtract column means).
- Compute the covariance matrix
cov_matrixusingnp.cov(X_centered, rowvar=False). - Perform eigenvalue decomposition with
np.linalg.eigh. - Compute a regularized whitening matrix:
Theeps = 1e-10 eig_vals_safe = np.where(eig_vals < eps, eps, eig_vals) whitening_matrix = eig_vecs @ np.diag(1.0 / np.sqrt(eig_vals_safe)) @ eig_vecs.Tepsprevents division by zero for near-zero eigenvalues (rank-deficient data). - Compute the whitened data:
X_whitened = X_centered @ whitening_matrix - Verify that the covariance of
X_whitenedis close to the identity matrix in the nonzero subspace.
Løsning
Var alt klart?
Tak for dine kommentarer!
Sektion 3. Kapitel 4
single
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Awesome!
Completion rate improved to 5.26
Challenge: Whitening via Eigenvalue Decomposition
Stryg for at vise menuen
Opgave
Swipe to start coding
You are given a dataset X (2D NumPy array) with correlated features.
Your goal is to perform feature whitening — transforming the data so that features become uncorrelated and have unit variance, using eigenvalue decomposition of the covariance matrix.
Steps:
- Center the data (subtract column means).
- Compute the covariance matrix
cov_matrixusingnp.cov(X_centered, rowvar=False). - Perform eigenvalue decomposition with
np.linalg.eigh. - Compute a regularized whitening matrix:
Theeps = 1e-10 eig_vals_safe = np.where(eig_vals < eps, eps, eig_vals) whitening_matrix = eig_vecs @ np.diag(1.0 / np.sqrt(eig_vals_safe)) @ eig_vecs.Tepsprevents division by zero for near-zero eigenvalues (rank-deficient data). - Compute the whitened data:
X_whitened = X_centered @ whitening_matrix - Verify that the covariance of
X_whitenedis close to the identity matrix in the nonzero subspace.
Løsning
Var alt klart?
Tak for dine kommentarer!
Sektion 3. Kapitel 4
single