docstr: k-Nearest Neighbors (kNN) outlier detector.
  
  The kNN detector is a non-parametric method for outlier detection. The detector scores each instance
  based on the distance to its neighbors. Instances with a large distance to their neighbors are more
  likely to be outliers.
  
  The detector can be initialized with `k` a single value or an array of values. If `k` is a single value then
  the outlier score is the distance/kernel similarity to the k-th nearest neighbor. If `k` is an array of
  values then the outlier score is the distance/kernel similarity to each of the specified `k` neighbors.
  In the latter case, an `aggregator` must be specified to aggregate the scores.
  
  Note that, in the multiple k case, a normalizer can be provided. If a normalizer is passed then it is fit in
  the `infer_threshold` method and so this method must be called before the `predict` method. If this is not
  done an exception is raised. If `k` is a single value then the predict method can be called without first
  calling `infer_threshold` but only scores will be returned and not outlier predictions.
  
  
  Parameters
  ----------
  k
      Number of nearest neighbors to compute distance to. `k` can be a single value or
      an array of integers. If an array is passed, an aggregator is required to aggregate
      the scores. If `k` is a single value the outlier score is the distance/kernel
      similarity to the `k`-th nearest neighbor. If `k` is a list then it returns the
      distance/kernel similarity to each of the specified `k` neighbors.
  kernel
      Kernel function to use for outlier detection. If ``None``, `torch.cdist` is used.
      Otherwise if a kernel is specified then instead of using `torch.cdist` the kernel
      defines the k nearest neighbor distance.
  normalizer
      Normalizer to use for outlier detection. If ``None``, no normalization is applied.
      For a list of available normalizers, see :mod:`alibi_detect.od.pytorch.ensemble`.
  aggregator
      Aggregator to use for outlier detection. Can be set to ``None`` if `k` is a single
      value. For a list of available aggregators, see :mod:`alibi_detect.od.pytorch.ensemble`.
  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
  ------
  ValueError
      If `k` is an array and `aggregator` is None.
  NotImplementedError
      If choice of `backend` is not implemented.
docstr_tok: ["k-Nearest","Neighbors","(","kNN",")","outlier","detector",".","The","kNN","detector","is","a","non-parametric","method","for","outlier","detection",".","The","detector","scores","each","instance","based","on","the","distance","to","its","neighbors",".","Instances","with","a","large","distance","to","their","neighbors","are","more","likely","to","be","outliers",".","The","detector","can","be","initialized","with","`","k","`","a","single","value","or","an","array","of","values",".","If","`","k","`","is","a","single","value","then","the","outlier","score","is","the","distance\/kernel","similarity","to","the","k-th","nearest","neighbor",".","If","`","k","`","is","an","array","of","values","then","the","outlier","score","is","the","distance\/kernel","similarity","to","each","of","the","specified","`","k","`","neighbors",".","In","the","latter","case",",","an","`","aggregator","`","must","be","specified","to","aggregate","the","scores",".","Note","that",",","in","the","multiple","k","case",",","a","normalizer","can","be","provided",".","If","a","normalizer","is","passed","then","it","is","fit","in","the","`","infer_threshold","`","method","and","so","this","method","must","be","called","before","the","`","predict","`","method",".","If","this","is","not","done","an","exception","is","raised",".","If","`","k","`","is","a","single","value","then","the","predict","method","can","be","called","without","first","calling","`","infer_threshold","`","but","only","scores","will","be","returned","and","not","outlier","predictions",".","Parameters","--","--","--","--","--","k","Number","of","nearest","neighbors","to","compute","distance","to",".","`","k","`","can","be","a","single","value","or","an","array","of","integers",".","If","an","array","is","passed",",","an","aggregator","is","required","to","aggregate","the","scores",".","If","`","k","`","is","a","single","value","the","outlier","score","is","the","distance\/kernel","similarity","to","the","`","k","`","-th","nearest","neighbor",".","If","`","k","`","is","a","list","then","it","returns","the","distance\/kernel","similarity","to","each","of","the","specified","`","k","`","neighbors",".","kernel","Kernel","function","to","use","for","outlier","detection",".","If","``","None","``",",","`","torch.cdist","`","is","used",".","Otherwise","if","a","kernel","is","specified","then","instead","of","using","`","torch.cdist","`","the","kernel","defines","the","k","nearest","neighbor","distance",".","normalizer","Normalizer","to","use","for","outlier","detection",".","If","``","None","``",",","no","normalization","is","applied",".","For","a","list","of","available","normalizers",",","see",":","mod",":","`","alibi_detect.od.pytorch.ensemble","`",".","aggregator","Aggregator","to","use","for","outlier","detection",".","Can","be","set","to","``","None","``","if","`","k","`","is","a","single","value",".","For","a","list","of","available","aggregators",",","see",":","mod",":","`","alibi_detect.od.pytorch.ensemble","`",".","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","--","--","--","ValueError","If","`","k","`","is","an","array","and","`","aggregator","`","is","None",".","NotImplementedError","If","choice","of","`","backend","`","is","not","implemented","."]
code: def __init__(
      self,
      k: Union[int, np.ndarray, List[int], Tuple[int]],
      kernel: Optional[Callable] = None,
      normalizer: Optional[
          Union[TransformProtocolType, NormalizerLiterals]
      ] = "PValNormalizer",
      aggregator: Union[
          TransformProtocol, AggregatorLiterals
      ] = "AverageAggregator",
      backend: Literal["pytorch"] = "pytorch",
      device: TorchDeviceType = None,
  ) -> None:
      """
      k-Nearest Neighbors (kNN) outlier detector.
  
      The kNN detector is a non-parametric method for outlier detection. The detector scores each instance
      based on the distance to its neighbors. Instances with a large distance to their neighbors are more
      likely to be outliers.
  
      The detector can be initialized with `k` a single value or an array of values. If `k` is a single value then
      the outlier score is the distance/kernel similarity to the k-th nearest neighbor. If `k` is an array of
      values then the outlier score is the distance/kernel similarity to each of the specified `k` neighbors.
      In the latter case, an `aggregator` must be specified to aggregate the scores.
  
      Note that, in the multiple k case, a normalizer can be provided. If a normalizer is passed then it is fit in
      the `infer_threshold` method and so this method must be called before the `predict` method. If this is not
      done an exception is raised. If `k` is a single value then the predict method can be called without first
      calling `infer_threshold` but only scores will be returned and not outlier predictions.
  
  
      Parameters
      ----------
      k
          Number of nearest neighbors to compute distance to. `k` can be a single value or
          an array of integers. If an array is passed, an aggregator is required to aggregate
          the scores. If `k` is a single value the outlier score is the distance/kernel
          similarity to the `k`-th nearest neighbor. If `k` is a list then it returns the
          distance/kernel similarity to each of the specified `k` neighbors.
      kernel
          Kernel function to use for outlier detection. If ``None``, `torch.cdist` is used.
          Otherwise if a kernel is specified then instead of using `torch.cdist` the kernel
          defines the k nearest neighbor distance.
      normalizer
          Normalizer to use for outlier detection. If ``None``, no normalization is applied.
          For a list of available normalizers, see :mod:`alibi_detect.od.pytorch.ensemble`.
      aggregator
          Aggregator to use for outlier detection. Can be set to ``None`` if `k` is a single
          value. For a list of available aggregators, see :mod:`alibi_detect.od.pytorch.ensemble`.
      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
      ------
      ValueError
          If `k` is an array and `aggregator` is None.
      NotImplementedError
          If choice of `backend` is not implemented.
      """
      super().__init__()
  
      backend_str: str = backend.lower()
      BackendValidator(
          backend_options={"pytorch": ["pytorch"]},
          construct_name=self.__class__.__name__,
      ).verify_backend(backend_str)
  
      backend_cls, ensembler_cls = backends[backend]
      ensembler = None
  
      if aggregator is None and isinstance(
          k, (list, np.ndarray, tuple)
      ):
          raise ValueError(
              "If `k` is a `np.ndarray`, `list` or `tuple`, "
              "the `aggregator` argument cannot be ``None``."
          )
  
      if isinstance(k, (list, np.ndarray, tuple)):
          ensembler = ensembler_cls(
              normalizer=get_normalizer(normalizer),
              aggregator=get_aggregator(aggregator),
          )
  
      self.backend = backend_cls(
          k, kernel=kernel, ensembler=ensembler, device=device
      )
  
      # set metadata
      self.meta["detector_type"] = "outlier"
      self.meta["data_type"] = "numeric"
      self.meta["online"] = False
  
imports: from typing import Callable, Union, Optional, Dict, Any, List, Tuple
  from typing_extensions import Literal
  import numpy
  from alibi_detect.base import outlier_prediction_dict
  from alibi_detect.exceptions import _catch_error
  from alibi_detect.od.base import TransformProtocol, TransformProtocolType
  from alibi_detect.base import BaseDetector, FitMixin, ThresholdMixin
  from alibi_detect.od.pytorch import KNNTorch, Ensembler
  from alibi_detect.od.base import get_aggregator, get_normalizer, NormalizerLiterals, AggregatorLiterals
  from alibi_detect.utils.frameworks import BackendValidator
  from alibi_detect.version import __version__
  from alibi_detect.utils._types import TorchDeviceType
label_desc: Use image node_id 1 to create a new KNN object from inherited base classes: BaseDetector, FitMixin, ThresholdMixin with example: obj = KNN(k, kernel, normalizer, aggregator, backend, device)