Two features that move together carry nearly the same information twice. PCA finds the directions your data actually varies along, and rewrites every point in terms of those directions instead — keeping the few that matter and dropping the rest.
Task: write pca_project(data, n_components) returning each row rewritten in the top n_components directions, every number rounded to 4 decimal places.
The four steps:
n - 1.n_components whose eigenvalues are largest. Those are your directions, biggest-variance first.-v describes the same direction as v, so the sign is arbitrary. To make the answer unique, flip each component so that its largest-magnitude entry is positive.n_components long.np.linalg.eigh — the covariance matrix is symmetric, and eigh exploits that to return real, sorted eigenvalues where the general-purpose eig can hand you complex noise.When two features are perfectly correlated, the second eigenvalue comes out at zero: the data is really one-dimensional, and PCA has just told you so.
Toggle to project every point onto the principal component.
Instead of storing two coordinates, we now store one position along the line. The important pattern remains.
Once your code passes every test, a reference solution shows up here so you can compare it with yours.