docstr: Principal Component Analysis (PCA) outlier detector.
  
  The detector is based on the Principal Component Analysis (PCA) algorithm. There are two variants of PCA:
  linear PCA and kernel PCA. Linear PCA computes the eigenvectors of the covariance matrix of the data. Kernel
  PCA computes the eigenvectors of the kernel matrix of the data.
  
  When scoring a test instance using the linear variant compute the distance to the principal subspace spanned
  by the first `n_components` eigenvectors.
  
  When scoring a test instance using the kernel variant we project it onto the largest eigenvectors and
  compute its score using the L2 norm.
  
  If a threshold is fitted we use this to determine whether the instance is an outlier or not.
  
  Parameters
  ----------
  n_components:
      The number of dimensions in the principal subspace. For linear pca should have
      ``1 <= n_components < dim(data)``. For kernel pca should have ``1 <= n_components < len(data)``.
  kernel
      Kernel function to use for outlier detection. If ``None``, linear PCA is used instead of the
      kernel variant.
  backend
      Backend used for outlier detection. Defaults to ``'pytorch'``. Options are ``'pytorch'``.
  device
      Device type used. The default tries to use the GPU and falls back on CPU if needed.
      Can be specified by passing either ``'cuda'``, ``'gpu'``, ``'cpu'`` or an instance of
      ``torch.device``.
  
  Raises
  ------
  NotImplementedError
      If choice of `backend` is not implemented.
  ValueError
      If `n_components` is less than 1.
docstr_tok: ["Principal","Component","Analysis","(","PCA",")","outlier","detector",".","The","detector","is","based","on","the","Principal","Component","Analysis","(","PCA",")","algorithm",".","There","are","two","variants","of","PCA",":","linear","PCA","and","kernel","PCA",".","Linear","PCA","computes","the","eigenvectors","of","the","covariance","matrix","of","the","data",".","Kernel","PCA","computes","the","eigenvectors","of","the","kernel","matrix","of","the","data",".","When","scoring","a","test","instance","using","the","linear","variant","compute","the","distance","to","the","principal","subspace","spanned","by","the","first","`","n_components","`","eigenvectors",".","When","scoring","a","test","instance","using","the","kernel","variant","we","project","it","onto","the","largest","eigenvectors","and","compute","its","score","using","the","L2","norm",".","If","a","threshold","is","fitted","we","use","this","to","determine","whether","the","instance","is","an","outlier","or","not",".","Parameters","--","--","--","--","--","n_components",":","The","number","of","dimensions","in","the","principal","subspace",".","For","linear","pca","should","have","``","1","<","=","n_components","<","dim","(","data",")","``",".","For","kernel","pca","should","have","``","1","<","=","n_components","<","len","(","data",")","``",".","kernel","Kernel","function","to","use","for","outlier","detection",".","If","``","None","``",",","linear","PCA","is","used","instead","of","the","kernel","variant",".","backend","Backend","used","for","outlier","detection",".","Defaults","to","``","'pytorch","'","``",".","Options","are","``","'pytorch","'","``",".","device","Device","type","used",".","The","default","tries","to","use","the","GPU","and","falls","back","on","CPU","if","needed",".","Can","be","specified","by","passing","either","``","'cuda","'","``",",","``","'gpu","'","``",",","``","'cpu","'","``","or","an","instance","of","``","torch.device","``",".","Raises","--","--","--","NotImplementedError","If","choice","of","`","backend","`","is","not","implemented",".","ValueError","If","`","n_components","`","is","less","than","1","."]
code: def __init__(
      self,
      n_components: int,
      kernel: Optional[Callable] = None,
      backend: Literal["pytorch"] = "pytorch",
      device: TorchDeviceType = None,
  ) -> None:
      """Principal Component Analysis (PCA) outlier detector.
  
      The detector is based on the Principal Component Analysis (PCA) algorithm. There are two variants of PCA:
      linear PCA and kernel PCA. Linear PCA computes the eigenvectors of the covariance matrix of the data. Kernel
      PCA computes the eigenvectors of the kernel matrix of the data.
  
      When scoring a test instance using the linear variant compute the distance to the principal subspace spanned
      by the first `n_components` eigenvectors.
  
      When scoring a test instance using the kernel variant we project it onto the largest eigenvectors and
      compute its score using the L2 norm.
  
      If a threshold is fitted we use this to determine whether the instance is an outlier or not.
  
      Parameters
      ----------
      n_components:
          The number of dimensions in the principal subspace. For linear pca should have
          ``1 <= n_components < dim(data)``. For kernel pca should have ``1 <= n_components < len(data)``.
      kernel
          Kernel function to use for outlier detection. If ``None``, linear PCA is used instead of the
          kernel variant.
      backend
          Backend used for outlier detection. Defaults to ``'pytorch'``. Options are ``'pytorch'``.
      device
          Device type used. The default tries to use the GPU and falls back on CPU if needed.
          Can be specified by passing either ``'cuda'``, ``'gpu'``, ``'cpu'`` or an instance of
          ``torch.device``.
  
      Raises
      ------
      NotImplementedError
          If choice of `backend` is not implemented.
      ValueError
          If `n_components` is less than 1.
      """
      super().__init__()
  
      backend_str: str = backend.lower()
      BackendValidator(
          backend_options={"pytorch": ["pytorch"]},
          construct_name=self.__class__.__name__,
      ).verify_backend(backend_str)
  
      kernel_backend_cls, linear_backend_cls = backends[backend]
  
      self.backend: Union[KernelPCATorch, LinearPCATorch]
      if kernel is not None:
          self.backend = kernel_backend_cls(
              n_components=n_components, device=device, kernel=kernel
          )
      else:
          self.backend = linear_backend_cls(
              n_components=n_components,
              device=device,
          )
  
      # set metadata
      self.meta["detector_type"] = "outlier"
      self.meta["data_type"] = "numeric"
      self.meta["online"] = False
  
file_path: alibi-detect/alibi_detect/od/_pca.py
imports: from typing import Union, Optional, Callable, Dict, Any
  from typing_extensions import Literal
  import numpy
  from alibi_detect.base import outlier_prediction_dict
  from alibi_detect.base import BaseDetector, ThresholdMixin, FitMixin
  from alibi_detect.od.pytorch import KernelPCATorch, LinearPCATorch
  from alibi_detect.utils.frameworks import BackendValidator
  from alibi_detect.version import __version__
  from alibi_detect.exceptions import _catch_error
  from alibi_detect.utils._types import TorchDeviceType
label_desc: Use image node_id 1 to create a new PCA object from inherited base classes: BaseDetector, ThresholdMixin, FitMixin with example: obj = PCA(n_components, kernel, backend, device)