PCA centered projections
Quick answer
Principal component analysis (PCA) compresses numeric observations into orthogonal directions of maximum training variance. Center features before projecting; a small number of components can approximate the original rows. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Center and project a row
x=[5,4]; mu=[2,1]; w=[1,0]z=sum((v-m)*a for v,m,a in zip(x,mu,w))print(z,[m+z*a for m,a in zip(mu,w)])Observed local output:
3 [5, 1]The coordinate removed by projection is replaced with its training mean.
How it works
For an n by d matrix X, compute the training column mean μ and centered matrix A = X − μ. Its thin singular value decomposition is A = U S Vᵀ. With k right singular vectors as rows of W (k by d), the scores are Z = A Wᵀ (n by k). Reconstruct with Z W + μ. For orthonormal W, the residual lies perpendicular to the retained subspace. PCA chooses the k-dimensional linear subspace minimizing squared reconstruction error on centered training data; it is not guaranteed to preserve labels or meaningful low-variance signals. The covariance AᵀA/(n−1) has eigenvectors V and eigenvalues s²/(n−1). For a point (5,4), mean (2,1) and direction (1,0), the score is 3 and reconstruction (5,1), leaving residual (0,3). Signs of singular vectors can flip without changing the subspace; equal singular values allow rotations. PCA centers but does not scale features. When units differ, choose whether to standardize based on the problem; if used, fit scaling on training rows and invert both transforms for raw-unit reconstruction. The full solver is practical for small matrices; large dense decompositions can be expensive. In supervised evaluation, fit PCA inside each training fold, never on validation or test rows.
Fit PCA on training rows
import numpy as npfrom sklearn.decomposition import PCAX=np.array([[0,1],[2,1],[4,1]],dtype=float)p=PCA(n_components=1,svd_solver="full").fit(X)T=np.array([[6,3.]])print(p.mean_.tolist(),p.inverse_transform(p.transform(T)).round(6).tolist())Observed local output:
[2.0, 1.0] [[6.0, 1.0]]The held-out second coordinate is not used to estimate the training mean.
Common mistakes
Do not project uncentered data with centered PCA directions, add the mean twice, or compare the signs of individual components across implementations. Unsupervised fitting can still leak test distribution information.
Practice and next steps
Project (3,7) with mean (1,2) onto the second coordinate axis. Reconstruct and calculate squared residual error. Repeat with the first axis and compare. Complete the knowledge check and staged exercise. Next: Explained variance and whitening.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
PCA centered projections — knowledge check
Apply pca centered projections to new scenarios.
What does PCA optimize on centered training data?
Why save the training mean?
A component changes sign across solvers. What follows?
Where should PCA be fitted in cross-validation?
Which feature might PCA discard?