docstr: Least-squares density difference (LSDD) data drift detector using a permutation test.
  
  Parameters
  ----------
  x_ref
      Data used as reference distribution.
  backend
      Backend used for the LSDD implementation.
  p_val
      p-value used for the significance of the permutation test.
  x_ref_preprocessed
      Whether the given reference data `x_ref` has been preprocessed yet. If `x_ref_preprocessed=True`, only
      the test data `x` will be preprocessed at prediction time. If `x_ref_preprocessed=False`, the reference
      data will also be preprocessed.
  preprocess_at_init
      Whether to preprocess the reference data when the detector is instantiated. Otherwise, the reference
      data will be preprocessed at prediction time. Only applies if `x_ref_preprocessed=False`.
  update_x_ref
      Reference data can optionally be updated to the last n instances seen by the detector
      or via reservoir sampling with size n. For the former, the parameter equals {'last': n} while
      for reservoir sampling {'reservoir_sampling': n} is passed.
  preprocess_fn
      Function to preprocess the data before computing the data drift metrics.
  sigma
      Optionally set the bandwidth of the Gaussian kernel used in estimating the LSDD. Can also pass multiple
      bandwidth values as an array. The kernel evaluation is then averaged over those bandwidths. If `sigma`
      is not specified, the 'median heuristic' is adopted whereby `sigma` is set as the median pairwise distance
      between reference samples.
  n_permutations
      Number of permutations used in the permutation test.
  n_kernel_centers
      The number of reference samples to use as centers in the Gaussian kernel model used to estimate LSDD.
      Defaults to 1/20th of the reference data.
  lambda_rd_max
      The maximum relative difference between two estimates of LSDD that the regularization parameter
      lambda is allowed to cause. Defaults to 0.2 as in the paper.
  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``. Only relevant for 'pytorch' backend.
  input_shape
      Shape of input data.
  data_type
      Optionally specify the data type (tabular, image or time-series). Added to metadata.
docstr_tok: ["Least-squares","density","difference","(","LSDD",")","data","drift","detector","using","a","permutation","test",".","Parameters","--","--","--","--","--","x_ref","Data","used","as","reference","distribution",".","backend","Backend","used","for","the","LSDD","implementation",".","p_val","p-value","used","for","the","significance","of","the","permutation","test",".","x_ref_preprocessed","Whether","the","given","reference","data","`","x_ref","`","has","been","preprocessed","yet",".","If","`","x_ref_preprocessed=True","`",",","only","the","test","data","`","x","`","will","be","preprocessed","at","prediction","time",".","If","`","x_ref_preprocessed=False","`",",","the","reference","data","will","also","be","preprocessed",".","preprocess_at_init","Whether","to","preprocess","the","reference","data","when","the","detector","is","instantiated",".","Otherwise",",","the","reference","data","will","be","preprocessed","at","prediction","time",".","Only","applies","if","`","x_ref_preprocessed=False","`",".","update_x_ref","Reference","data","can","optionally","be","updated","to","the","last","n","instances","seen","by","the","detector","or","via","reservoir","sampling","with","size","n.","For","the","former",",","the","parameter","equals","{","'last","'",":","n","}","while","for","reservoir","sampling","{","'reservoir_sampling","'",":","n","}","is","passed",".","preprocess_fn","Function","to","preprocess","the","data","before","computing","the","data","drift","metrics",".","sigma","Optionally","set","the","bandwidth","of","the","Gaussian","kernel","used","in","estimating","the","LSDD",".","Can","also","pass","multiple","bandwidth","values","as","an","array",".","The","kernel","evaluation","is","then","averaged","over","those","bandwidths",".","If","`","sigma","`","is","not","specified",",","the","'median","heuristic","'","is","adopted","whereby","`","sigma","`","is","set","as","the","median","pairwise","distance","between","reference","samples",".","n_permutations","Number","of","permutations","used","in","the","permutation","test",".","n_kernel_centers","The","number","of","reference","samples","to","use","as","centers","in","the","Gaussian","kernel","model","used","to","estimate","LSDD",".","Defaults","to","1\/20th","of","the","reference","data",".","lambda_rd_max","The","maximum","relative","difference","between","two","estimates","of","LSDD","that","the","regularization","parameter","lambda","is","allowed","to","cause",".","Defaults","to","0.2","as","in","the","paper",".","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","``",".","Only","relevant","for","'pytorch","'","backend",".","input_shape","Shape","of","input","data",".","data_type","Optionally","specify","the","data","type","(","tabular",",","image","or","time-series",")",".","Added","to","metadata","."]
code: def __init__(
      self,
      x_ref: Union[np.ndarray, list],
      backend: str = "tensorflow",
      p_val: float = 0.05,
      x_ref_preprocessed: bool = False,
      preprocess_at_init: bool = True,
      update_x_ref: Optional[Dict[str, int]] = None,
      preprocess_fn: Optional[Callable] = None,
      sigma: Optional[np.ndarray] = None,
      n_permutations: int = 100,
      n_kernel_centers: Optional[int] = None,
      lambda_rd_max: float = 0.2,
      device: TorchDeviceType = None,
      input_shape: Optional[tuple] = None,
      data_type: Optional[str] = None,
  ) -> None:
      """
      Least-squares density difference (LSDD) data drift detector using a permutation test.
  
      Parameters
      ----------
      x_ref
          Data used as reference distribution.
      backend
          Backend used for the LSDD implementation.
      p_val
          p-value used for the significance of the permutation test.
      x_ref_preprocessed
          Whether the given reference data `x_ref` has been preprocessed yet. If `x_ref_preprocessed=True`, only
          the test data `x` will be preprocessed at prediction time. If `x_ref_preprocessed=False`, the reference
          data will also be preprocessed.
      preprocess_at_init
          Whether to preprocess the reference data when the detector is instantiated. Otherwise, the reference
          data will be preprocessed at prediction time. Only applies if `x_ref_preprocessed=False`.
      update_x_ref
          Reference data can optionally be updated to the last n instances seen by the detector
          or via reservoir sampling with size n. For the former, the parameter equals {'last': n} while
          for reservoir sampling {'reservoir_sampling': n} is passed.
      preprocess_fn
          Function to preprocess the data before computing the data drift metrics.
      sigma
          Optionally set the bandwidth of the Gaussian kernel used in estimating the LSDD. Can also pass multiple
          bandwidth values as an array. The kernel evaluation is then averaged over those bandwidths. If `sigma`
          is not specified, the 'median heuristic' is adopted whereby `sigma` is set as the median pairwise distance
          between reference samples.
      n_permutations
          Number of permutations used in the permutation test.
      n_kernel_centers
          The number of reference samples to use as centers in the Gaussian kernel model used to estimate LSDD.
          Defaults to 1/20th of the reference data.
      lambda_rd_max
          The maximum relative difference between two estimates of LSDD that the regularization parameter
          lambda is allowed to cause. Defaults to 0.2 as in the paper.
      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``. Only relevant for 'pytorch' backend.
      input_shape
          Shape of input data.
      data_type
          Optionally specify the data type (tabular, image or time-series). Added to metadata.
      """
      super().__init__()
  
      # Set config
      self._set_config(locals())
  
      backend = backend.lower()
      BackendValidator(
          backend_options={
              Framework.TENSORFLOW: [Framework.TENSORFLOW],
              Framework.PYTORCH: [Framework.PYTORCH],
          },
          construct_name=self.__class__.__name__,
      ).verify_backend(backend)
  
      kwargs = locals()
      args = [kwargs["x_ref"]]
      pop_kwargs = ["self", "x_ref", "backend", "__class__"]
      [kwargs.pop(k, None) for k in pop_kwargs]
  
      if backend == Framework.TENSORFLOW:
          kwargs.pop("device", None)
          self._detector = LSDDDriftTF(*args, **kwargs)
      else:
          self._detector = LSDDDriftTorch(*args, **kwargs)
      self.meta = self._detector.meta
  
file_path: alibi-detect/alibi_detect/cd/lsdd.py
imports: import numpy
  from typing import Callable, Dict, Optional, Union, Tuple
  from alibi_detect.utils.frameworks import has_pytorch, has_tensorflow, BackendValidator, Framework
  from alibi_detect.utils.warnings import deprecated_alias
  from alibi_detect.base import DriftConfigMixin
  from alibi_detect.utils._types import TorchDeviceType
label_desc: Use image node_id 1 to create a new LSDDDrift object from inherited base classes: DriftConfigMixin with example: obj = LSDDDrift(x_ref, backend, p_val, x_ref_preprocessed, preprocess_at_init, update_x_ref, preprocess_fn, sigma, n_permutations, n_kernel_centers, lambda_rd_max, device, input_shape, data_type)