scanpy.get.pca

Contents

scanpy.get.pca#

scanpy.get.pca(adata, *, key_added='pca')[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().

key_added str (default: 'pca')

.obsm, .varm, and .uns key used when running PCA.

Return type:

AnnData

Returns:

An AnnData with:

.X

the PCA embedding (adata.obsm[key_added]), observations × components.

.obs

adata.obs, unchanged.

.var

one row per principal component (named PC1, PC2, …), with variance and variance_ratio columns taken from adata.uns[key_added].

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)