scanpy.get.pca#

scanpy.get.pca(adata, *, use=None)[source]#

Return PCA results as an AnnData indexed by component.

The principal components (not the genes) become the variables, so per-component quantities like the variance ratio become .var columns. Useful for feeding into functions that expect an axis to rank over, e.g. ranking().

Parameters:
adata AnnData

Annotated data matrix with PCA computed, e.g. via pca().

use MultiAcc | str | None (default: None)

Where pca() wrote the embedding, i.e. the out passed to it, e.g. A.obsm['p'] (with its .uns entry 'p'). strs are resolve()d. If None, the default places.

Return type:

AnnData

Returns:

An AnnData with:

.X

the PCA embedding (e.g. adata.obsm['X_pca']), observations × components.

.obs

adata.obs, unchanged.

.var

one row per principal component (named PC1, PC2, …), with variance and variance_ratio columns taken from the .uns entry (e.g. adata.uns['pca']).

Examples

import scanpy as sc
sc.settings.preset = sc.Preset.ScanpyV2Preview
adata = sc.datasets.pbmc68k_reduced()
sc.get.pca(adata)
AnnData object with n_obs × n_vars = 700 × 50
    obs: 'bulk_labels', 'n_genes', 'percent_mito', 'n_counts', 'S_score', 'G2M_score', 'phase', 'louvain'
    var: 'variance', 'variance_ratio'
    layers: None (.X)