Language: English Last updated: 2026-05-02 Switch: Chinese
PCA estimates an orthonormal low-dimensional basis that captures the largest variance directions of centered dense data. It supports CPU, CuPy/CUDA, and Torch CUDA backends.
from statgpu.unsupervised import PCAFor centered data X_c = X - mean(X), PCA solves:
Keeping k components is also the best rank-k squared-error reconstruction among orthonormal projections:
The two objectives are equivalent because total variance is fixed after centering.
-
svd_solver="covariance"computes $$ \Sigma = \frac{X_c^\top X_c}{n - 1} $$ and solves $$ \Sigma v_j = \lambda_j v_j $$ witheigh. -
svd_solver="full"computes $$ X_c = U S V^\top $$ and uses rows ofV.Tas components. -
svd_solver="auto"uses covariance/eigh whenn_samples >= n_features, otherwise full SVD. -
svd_solver="randomized"draws a random projection, performs power iterations, factorizes the smaller projected matrix, and keeps the leading right singular vectors. - Explained variance is computed as $$ \operatorname{explained_variance}_j = \frac{s_j^2}{n - 1}. $$
-
explained_variance_ratio_divides each retained variance by total centered variance.
n_components: number of principal components to keep;Nonekeeps all feasible components.svd_solver:"auto","full","covariance", or"randomized".whiten: ifTrue, transformed coordinates are divided bysqrt(explained_variance_).random_state,n_oversamples,iterated_power: randomized solver controls.device:"auto","cpu","cuda", or"torch".
import numpy as np
from statgpu.unsupervised import PCA
X = np.random.default_rng(0).normal(size=(2000, 50))
pca_cpu = PCA(n_components=10, svd_solver="covariance", device="cpu")
Z_cpu = pca_cpu.fit_transform(X)
pca_gpu = PCA(n_components=10, svd_solver="covariance", device="cuda")
Z_gpu = pca_gpu.fit_transform(X)PCA has no statistical strict inference mode. Exactness refers to the decomposition:
fullandcovarianceare exact dense solvers up to floating-point error.randomizedis approximate and controlled byrandom_state,n_oversamples, anditerated_power.- Component signs are not identifiable;
vand-vdescribe the same component.
components_mean_explained_variance_explained_variance_ratio_singular_values_n_components_n_features_in_
Why do components differ by sign from sklearn? Eigenvectors and singular vectors are sign-indeterminate. Validation must compare subspaces or use sign-aware comparisons.
What does whitening do?
It scales transformed scores by 1 / sqrt(explained_variance_), producing unit-variance component scores under the fitted model.
- Tests:
dev/tests/test_unsupervised_pca.py. - Benchmark:
dev/benchmarks/benchmark_unsupervised.py. - Baselines: sklearn PCA, statsmodels/R PCA comparisons from the earlier unsupervised matrix where available.
- Latest Phase 2 artifact summary:
results/unsupervised_phase2_verify_summary_20260502_210000.md.
- Pearson, K. (1901). On lines and planes of closest fit to systems of points in space. The London, Edinburgh, and Dublin Philosophical Magazine and Journal of Science, Series 6, 2(11), 559-572. https://doi.org/10.1080/14786440109462720
- Jolliffe, I. T. (2002). Principal Component Analysis (2nd ed.). Springer Series in Statistics. Springer. https://doi.org/10.1007/b98835
- Halko, N., Martinsson, P. G., & Tropp, J. A. (2011). Finding structure with randomness: Probabilistic algorithms for constructing approximate matrix decompositions. SIAM Review, 53(2), 217-288. https://doi.org/10.1137/090771806