Report the explained variance ratio of PCA
Why it matters Teams pick how many dimensions to keep for vector indexes by reading this curve.
Before shrinking embeddings with PCA, you want to know how much variance each component keeps.
Write explained_variance_ratio(X). Center the columns of X, take the singular values s of the centered matrix, and return s^2 / sum(s^2) in descending order.
def explained_variance_ratio(X)Example
X = [[1, 0], [-1, 0], [0, 0]] gives [1.0, 0.0].
Understand the idea
PCA finds new axes along which the data spreads out the most. The explained variance ratio tells you what share of the total spread each axis keeps, which is how you decide how many components you can safely drop. Worked example: X = [[2, 0], [0, 1], [-2, 0], [0, -1]]. Each column already has mean 0. The columns are orthogonal, so the squared singular values are just the column sums of squares: 4 + 4 = 8 for the first and 1 + 1 = 2 for the second. The ratios are 8 / 10 = 0.8 and 2 / 10 = 0.2. Rule: subtract each column's mean, take the SVD of the centered matrix to get singular values s, and return s^2 / sum(s^2) in descending order. Centering is essential; without it the mean leaks into the first component and inflates it. Used in: shrinking embedding vectors before indexing, noise reduction, and quick plots of high-dimensional features.