Project data onto its top principal components
Medium16 min13 hidden tests
A factory logs a few sensor readings per machine every hour, and the team wants a 2-D plot of all machines. Write pca_project(X, k) that centers the data, finds its top k principal components and returns each row's coordinates along them. Eigenvectors have no fixed sign, so flip each component until its largest entry, by absolute value, is positive. That way two people who run the code get the same plot.
Turn the axis to catch the most spread
project the centered rows onto PC1Along this axis the projections have variance 5. PC1 = (0.836, 0.549) catches the most, 5.985 of the total 8.7 (69%). On PC1 the five machines sit at −2.043, −3.166, 1.275, 2.398, 1.537.
Variance along the axis 5, most possible 5.985.
Examples
Five machines, two sensors, one component
pca_project([[2.0, 0.0], [0.0, 1.0], [4.0, 3.0], [6.0, 2.0], [3.0, 5.0]], 1)
returns [[-2.04333], [-3.16641], [1.275], [2.39807], [1.53668]]
With one sensor the projection is the centered reading
pca_project([[1.0], [4.0], [2.0], [7.0]], 1)
returns [[-2.5], [0.5], [-1.5], [3.5]]
Requirements
Xis a 2-D list or array of shape (n, d) with n >= 2, andkis an int from 1 to d. The top k variances are all different.- Center each column by subtracting its mean. The components are the eigenvectors of the covariance matrix Xc.T @ Xc / (n - 1), sorted by eigenvalue from largest to smallest.
- Sign rule: flip a component if its entry with the largest absolute value is negative.
- Return the projection Xc @ W as a float array of shape (n, k), where W holds the k components as columns.
The idea behind it
Principal component analysis (PCA) finds the directions along which data varies most. The first principal component is the unit vector with the largest variance of the data projected onto it. The second has the largest variance among directions perpendicular to the first, and so on. They are the eigenvectors of the covariance matrix, and each eigenvalue is the variance along its vector. The data must be centered first, or the top direction just points at the mean. Projecting onto the top k components turns d readings into k coordinates that keep as much spread as possible. Teams use this to plot high-dimensional data, compress features, and remove noise. One catch: if v is an eigenvector, so is -v. Libraries fix a sign rule so results are reproducible, and this problem uses one.