rapids_singlecell.pp.pca#
- rapids_singlecell.pp.pca(data, n_comps=None, *, use=None, zero_center=True, svd_solver=None, rng=None, mask=adata.var.get('highly_variable'), dtype='float32', chunked=False, chunk_size=None, return_info=False, out=False, key_added=None, layer=None, copy=False, mask_var=None, **kwargs)[source]#
Principal component analysis using GPU acceleration [HMT09, TQOA24].
Uses the following implementations based on data type (defaults for
svd_solverin parentheses):Dense
Sparse
Dask
zero_center=TruecuML PCA (
'full')Custom (
'lanczos'if n_vars > 8k, else'covariance_eigh')Custom (
'covariance_eigh')zero_center=FalsecuML TruncatedSVD (
'full')Custom (
'lanczos'if n_vars > 8k, else'covariance_eigh')Custom (
'covariance_eigh')chunked=TruecuML IncrementalPCA
cuML IncrementalPCA
Not supported
- Parameters:
- data
AnnData|ndarray|csc_matrix|csr_matrix|Array The (annotated) data matrix of shape
n_obs×n_vars. Rows correspond to cells and columns to genes. If a matrix is passed instead of anAnnDataobject, the PCA representation is returned.- n_comps
int|None(default:None) Number of principal components to compute. Defaults to 50, or 1 - minimum dimension size of selected representation.
- use
LayerAcc|MultiAcc|str|None(default:None) Which matrix to run PCA on, e.g.
A.X,A.layers['counts']orA.obsm['x']. Strings are resolved byanndata.acc.AdAcc.resolve(), e.g.'layers.counts'.- zero_center
bool(default:True) If
True, compute standard PCA from covariance matrix. IfFalse, omit zero-centering variables (truncated SVD).- svd_solver
str|None(default:None) SVD solver to use. See table above for which implementation is used based on data type, as well as the default solver when
svd_solver=None.NoneChoose automatically based on data type (see table above).
'covariance_eigh'Eigendecomposition of the covariance matrix. Fast for sparse matrices with fewer than ~8,000 features. Works with Dask arrays.
'lanczos'Lanczos bidiagonalization with implicit restarts. Memory efficient for large sparse matrices (>8,000 features). Best singular value accuracy. Does not support Dask arrays.
'randomized'Randomized SVD (Halko et al. 2009) with CholeskyQR2 orthogonalization (Tomás et al. 2024). Faster than Lanczos but approximate. Does not support Dask arrays.
'full'cuML: Full eigendecomposition of covariance matrix. For dense arrays only.
'jacobi'cuML: Jacobi iterative solver. Faster but less accurate. For dense arrays only.
- rng
int|integer|Sequence[int] |SeedSequence|Generator|BitGenerator|None(default:None) Random seed or
Generatorfor initialization. The supersededrandom_stateargument is still accepted.- mask
NDArray[bool] |AdRef|str|Default|None(default:adata.var.get('highly_variable')) To run only on a certain set of genes, given by a boolean array or a reference like
A.var['selected']. Strings are resolved byanndata.acc.AdAcc.resolve(), e.g.'var.selected'. By default, uses.var['highly_variable']if available, else everything.- dtype
str(default:'float32') Numpy data type string to which to convert the result.
- chunked
bool(default:False) If
True, perform an incremental PCA on segments ofchunk_size. The incremental PCA automatically zero centers and ignores settings ofrngandsvd_solver. IfFalse, perform a full PCA.- chunk_size
int(default:None) Number of observations to include in each chunk. Required if
chunked=Truewas passed.- return_info
bool(default:False) Only relevant when passing a matrix instead of an
AnnData: see “Returns”.- out
MultiAcc|str|PcaOut|bool(default:False) Where to write the outputs. A single accessor writes
embeddingto it and names the others after its keyk, e.g.out=A.obsm['k']writes.varm['k']and.uns['k']. A mapping (PcaOut) writes only the outputs it names, e.g.{'embedding': A.obsm['pca']}; it has to includeembedding, after which the.unsentry is named. IfTrue, they are returned as aPcaResult(without the.unsentry). IfFalse, they are written to.obsm['X_pca']and.varm['PCs']('pca'under the v2 preset). Ifusepoints to.obsm, the loadings are stored in the.unsentry instead.- **kwargs
Additional arguments for specific SVD solvers. For
svd_solver='randomized':n_oversamples: Extra random vectors for better approximation. Higher values improve accuracy. Default is 10.n_iter: Number of power iterations. Higher values improve accuracy for matrices with slowly decaying singular values. Default is 2.
- data
- Return type:
None|AnnData|PcaResult|ndarray|csc_matrix|csr_matrix|Array- Returns:
If a matrix is passed and
return_info=False, the PCA representation is returned. If a matrix is passed andreturn_info=True, a tuple of(X_pca, components, variance_ratio, variance)is returned.If an AnnData object is passed, returns a
PcaResultifout=True, elseNone. By default sets (seeoutfor others):.obsm['X_pca']PCA representation of data.
.varm['PCs']The principal components containing the loadings.
.uns['pca']['variance_ratio']Ratio of explained variance.
.uns['pca']['variance']Explained variance, equivalent to the eigenvalues of the covariance matrix.