code: def fit_pca(
      embeddings: NDArray[np.float32],
      n_components: int = 2,
      fraction: Optional[float] = None,
  ) -> PCA:
      """Fits PCA to randomly selected subset of embeddings.
  
      For large datasets, it can be unfeasible to perform PCA on the whole data.
      This method can fit a PCA on a fraction of the embeddings in order to save
      computational resources.
  
      Args:
          embeddings:
              Datapoints stored in numpy array of size n x d.
          n_components:
              Number of principal components to keep.
          fraction:
              Fraction of the dataset to fit PCA on.
  
      Returns:
          A transformer which can be used to transform embeddings
          to lower dimensions.
  
      Raises:
          ValueError: If fraction < 0 or fraction > 1.
  
      """
      if fraction is not None:
          if fraction < 0.0 or fraction > 1.0:
              msg = f"fraction must be in [0, 1] but was {fraction}."
              raise ValueError(msg)
  
      N = embeddings.shape[0]
      n = N if fraction is None else min(N, int(N * fraction))
      X = embeddings[np.random.permutation(N)][:n]
      return PCA(n_components=n_components).fit(X)
  
file_path: lightly/lightly/utils/embeddings_2d.py
imports: from __future__ import annotations
  from typing import Optional, Tuple
  import numpy
  from numpy.typing import NDArray
label_desc: Use image node_id 1 for calling a global function with example usage: fit_pca(embeddings, n_components, fraction) and returns: PCA