Before you start
Implement PCA from the covariance matrix.
X by subtracting the column means.C = X_centered^T @ X_centered / (n - 1).n_components and project the centred data onto them.C is symmetric, so use np.linalg.eigh rather than eig. It returns real
eigenvalues in ascending order, so you have to reverse them.
Eigenvectors are only defined up to sign, and eigh may hand you either
direction. To make the result deterministic, flip each component so that its
largest-magnitude entry is positive.
Return (components, projected) with shapes (n_components, d) and
(n, n_components). Each row of components is one unit-length eigenvector.
def pca(X, n_components):
Input
X = np.array([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]])
pca(X, 1)
Output
(array([[0.70710678, 0.70710678]]), array([[-1.41421356],
[ 0. ],
[ 1.41421356]]))
Reasoning
All points lie on the line y = x, so the first component is [1, 1] / sqrt(2). The centred points are (-1, -1), (0, 0) and (1, 1), which project to -sqrt(2), 0 and sqrt(2).
np.linalg.eigh and reverse its order.(n_components, d) and (n, n_components).Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell