docstr: If categorical variables are present, then transform those to numerical values.
  This step is not necessary in the absence of categorical variables.
  
  Parameters
  ----------
  X
      Batch of instances used to infer distances between categories from.
  y
      Model class predictions or ground truth labels for X.
      Used for 'mvdm' and 'abdm-mvdm' pairwise distance metrics.
      Note that this is only compatible with classification problems. For regression problems,
      use the 'abdm' distance metric.
  d_type
      Pairwise distance metric used for categorical variables. Currently, 'abdm', 'mvdm' and 'abdm-mvdm'
      are supported. 'abdm' infers context from the other variables while 'mvdm' uses the model predictions.
      'abdm-mvdm' is a weighted combination of the two metrics.
  w
      Weight on 'abdm' (between 0. and 1.) distance if d_type equals 'abdm-mvdm'.
  disc_perc
      List with percentiles used in binning of numerical features used for the 'abdm'
      and 'abdm-mvdm' pairwise distance measures.
  standardize_cat_vars
      Standardize numerical values of categorical variables if True.
  feature_range
      Tuple with min and max ranges to allow for perturbed instances. Min and max ranges can be floats or
      numpy arrays with dimension (1x nb of features) for feature-wise ranges.
  smooth
      Smoothing exponent between 0 and 1 for the distances. Lower values of l will smooth the difference in
      distance metric between different features.
  center
      Whether to center the scaled distance measures. If False, the min distance for each feature
      except for the feature with the highest raw max distance will be the lower bound of the
      feature range, but the upper bound will be below the max feature range.
docstr_tok: ["If","categorical","variables","are","present",",","then","transform","those","to","numerical","values",".","This","step","is","not","necessary","in","the","absence","of","categorical","variables",".","Parameters","--","--","--","--","--","X","Batch","of","instances","used","to","infer","distances","between","categories","from",".","y","Model","class","predictions","or","ground","truth","labels","for","X",".","Used","for","'mvdm","'","and","'abdm-mvdm","'","pairwise","distance","metrics",".","Note","that","this","is","only","compatible","with","classification","problems",".","For","regression","problems",",","use","the","'abdm","'","distance","metric",".","d_type","Pairwise","distance","metric","used","for","categorical","variables",".","Currently",",","'abdm","'",",","'mvdm","'","and","'abdm-mvdm'","are","supported",".","'abdm","'","infers","context","from","the","other","variables","while","'mvdm","'","uses","the","model","predictions",".","'abdm-mvdm","'","is","a","weighted","combination","of","the","two","metrics",".","w","Weight","on","'abdm","'","(","between","0.","and","1",".",")","distance","if","d_type","equals","'abdm-mvdm","'",".","disc_perc","List","with","percentiles","used","in","binning","of","numerical","features","used","for","the","'abdm'","and","'abdm-mvdm","'","pairwise","distance","measures",".","standardize_cat_vars","Standardize","numerical","values","of","categorical","variables","if","True",".","feature_range","Tuple","with","min","and","max","ranges","to","allow","for","perturbed","instances",".","Min","and","max","ranges","can","be","floats","or","numpy","arrays","with","dimension","(","1x","nb","of","features",")","for","feature-wise","ranges",".","smooth","Smoothing","exponent","between","0","and","1","for","the","distances",".","Lower","values","of","l","will","smooth","the","difference","in","distance","metric","between","different","features",".","center","Whether","to","center","the","scaled","distance","measures",".","If","False",",","the","min","distance","for","each","feature","except","for","the","feature","with","the","highest","raw","max","distance","will","be","the","lower","bound","of","the","feature","range",",","but","the","upper","bound","will","be","below","the","max","feature","range","."]
code: def fit(
      self,
      X: np.ndarray,
      y: np.ndarray = None,
      d_type: str = "abdm",
      w: float = None,
      disc_perc: list = [25, 50, 75],
      standardize_cat_vars: bool = True,
      feature_range: tuple = (-1e10, 1e10),
      smooth: float = 1.0,
      center: bool = True,
  ) -> None:
      """
      If categorical variables are present, then transform those to numerical values.
      This step is not necessary in the absence of categorical variables.
  
      Parameters
      ----------
      X
          Batch of instances used to infer distances between categories from.
      y
          Model class predictions or ground truth labels for X.
          Used for 'mvdm' and 'abdm-mvdm' pairwise distance metrics.
          Note that this is only compatible with classification problems. For regression problems,
          use the 'abdm' distance metric.
      d_type
          Pairwise distance metric used for categorical variables. Currently, 'abdm', 'mvdm' and 'abdm-mvdm'
          are supported. 'abdm' infers context from the other variables while 'mvdm' uses the model predictions.
          'abdm-mvdm' is a weighted combination of the two metrics.
      w
          Weight on 'abdm' (between 0. and 1.) distance if d_type equals 'abdm-mvdm'.
      disc_perc
          List with percentiles used in binning of numerical features used for the 'abdm'
          and 'abdm-mvdm' pairwise distance measures.
      standardize_cat_vars
          Standardize numerical values of categorical variables if True.
      feature_range
          Tuple with min and max ranges to allow for perturbed instances. Min and max ranges can be floats or
          numpy arrays with dimension (1x nb of features) for feature-wise ranges.
      smooth
          Smoothing exponent between 0 and 1 for the distances. Lower values of l will smooth the difference in
          distance metric between different features.
      center
          Whether to center the scaled distance measures. If False, the min distance for each feature
          except for the feature with the highest raw max distance will be the lower bound of the
          feature range, but the upper bound will be below the max feature range.
      """
      if self.cat_vars is None:
          raise TypeError(
              'No categorical variables specified in the "cat_vars" argument.'
          )
  
      if d_type not in ["abdm", "mvdm", "abdm-mvdm"]:
          raise ValueError(
              'd_type needs to be "abdm", "mvdm" or "abdm-mvdm". '
              "{} is not supported.".format(d_type)
          )
  
      if self.ohe:
          X_ord, cat_vars_ord = ohe2ord(X, self.cat_vars)
      else:
          X_ord, cat_vars_ord = X, self.cat_vars
  
      # bin numerical features to compute the pairwise distance matrices
      cat_keys = list(cat_vars_ord.keys())
      n_ord = X_ord.shape[1]
      if d_type in ["abdm", "abdm-mvdm"] and len(cat_keys) != n_ord:
          fnames = [str(_) for _ in range(n_ord)]
          disc = Discretizer(
              X_ord, cat_keys, fnames, percentiles=disc_perc
          )
          X_bin = disc.discretize(X_ord)
          cat_vars_bin = {
              k: len(disc.names[k])
              for k in range(n_ord)
              if k not in cat_keys
          }
      else:
          X_bin = X_ord
          cat_vars_bin = {}
  
      # pairwise distances for categorical variables
      if d_type == "abdm":
          d_pair = abdm(X_bin, cat_vars_ord, cat_vars_bin)
      elif d_type == "mvdm":
          d_pair = mvdm(X_ord, y, cat_vars_ord, alpha=1)
  
      if type(feature_range[0]) == type(
          feature_range[1]
      ) and type(  # noqa
          feature_range[0]
      ) in [
          int,
          float,
      ]:
          feature_range = (
              np.ones((1, n_ord)) * feature_range[0],
              np.ones((1, n_ord)) * feature_range[1],
          )
  
      if d_type == "abdm-mvdm":
          # pairwise distances
          d_abdm = abdm(X_bin, cat_vars_ord, cat_vars_bin)
          d_mvdm = mvdm(X_ord, y, cat_vars_ord, alpha=1)
  
          # multidim scaled distances
          d_abs_abdm = multidim_scaling(
              d_abdm,
              n_components=2,
              use_metric=True,
              feature_range=feature_range,
              standardize_cat_vars=standardize_cat_vars,
              smooth=smooth,
              center=center,
              update_feature_range=False,
          )[0]
  
          d_abs_mvdm = multidim_scaling(
              d_mvdm,
              n_components=2,
              use_metric=True,
              feature_range=feature_range,
              standardize_cat_vars=standardize_cat_vars,
              smooth=smooth,
              center=center,
              update_feature_range=False,
          )[0]
  
          # combine abdm and mvdm
          for k, v in d_abs_abdm.items():
              self.d_abs[k] = v * w + d_abs_mvdm[k] * (1 - w)
              if center:  # center the numerical feature values
                  self.d_abs[k] -= 0.5 * (
                      self.d_abs[k].max() + self.d_abs[k].min()
                  )
      else:
          self.d_abs = multidim_scaling(
              d_pair,
              n_components=2,
              use_metric=True,
              feature_range=feature_range,
              standardize_cat_vars=standardize_cat_vars,
              smooth=smooth,
              center=center,
              update_feature_range=False,
          )[0]
  
file_path: alibi-detect/alibi_detect/od/mahalanobis.py
imports: import logging
  import numpy
  from scipy.linalg import eigh
  from typing import Dict, Optional
  from alibi_detect.utils.discretizer import Discretizer
  from alibi_detect.utils.distance import abdm, mvdm, multidim_scaling
  from alibi_detect.utils.mapping import ohe2ord, ord2num
  from alibi_detect.base import BaseDetector, FitMixin, ThresholdMixin, outlier_prediction_dict
label_desc: Use image node_id 2 for calling the Mahalanobis obj's underlying member method code with example usage: obj.fit(X, y, d_type, w, disc_perc, standardize_cat_vars, feature_range, smooth, center) without return types