Challenge: Whitening via Eigenvalue Decomposition
Oppgave
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
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 3. Kapittel 4
single
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 5.26
Challenge: Whitening via Eigenvalue Decomposition
Sveip for å vise menyen
Oppgave
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
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 3. Kapittel 4
single