docstr: Calculate trust scores = ratio of distance to closest class other than the
  predicted class to distance to predicted class.
  
  Parameters
  ----------
  X
      Instances to calculate trust score for.
  Y
      Either prediction probabilities for each class or the predicted class.
  k
      Number of nearest neighbors used for distance calculation.
  dist_type
      Use either the distance to the k-nearest point (``dist_type = 'point'``) or
      the average distance from the first to the k-nearest point in the data (``dist_type = 'mean'``).
  
  Returns
  -------
  Batch with trust scores and the closest not predicted class.
code: def score(
      self,
      X: np.ndarray,
      Y: np.ndarray,
      k: int = 2,
      dist_type: str = "point",
  ) -> Tuple[np.ndarray, np.ndarray]:
      """
      Calculate trust scores = ratio of distance to closest class other than the
      predicted class to distance to predicted class.
  
      Parameters
      ----------
      X
          Instances to calculate trust score for.
      Y
          Either prediction probabilities for each class or the predicted class.
      k
          Number of nearest neighbors used for distance calculation.
      dist_type
          Use either the distance to the k-nearest point (``dist_type = 'point'``) or
          the average distance from the first to the k-nearest point in the data (``dist_type = 'mean'``).
  
      Returns
      -------
      Batch with trust scores and the closest not predicted class.
      """
      # make sure Y represents predicted classes, not probabilities
      if len(Y.shape) > 1:
          Y = np.argmax(Y, axis=1)
  
      # KDTree needs 2D data
      if len(X.shape) > 2:
          logger.warning(
              "Reshaping data from {0} to {1} so k-d trees can "
              "be queried.".format(
                  X.shape, X.reshape(X.shape[0], -1).shape
              )
          )
          X = X.reshape(X.shape[0], -1)
  
      # init distance matrix: [nb instances, nb classes]
      d: np.ndarray = np.tile(np.nan, (X.shape[0], self.classes))
  
      for c in range(self.classes):
          d_tmp = self.kdtrees[c].query(X, k=k)[
              0
          ]  # get k nearest neighbors for each class
          if dist_type == "point":
              d[:, c] = d_tmp[:, -1]
          elif dist_type == "mean":
              d[:, c] = np.mean(d_tmp, axis=1)
  
      sorted_d = np.sort(
          d, axis=1
      )  # sort distance each instance in batch over classes
      # get distance to predicted and closest other class and calculate trust score
      d_to_pred = d[range(d.shape[0]), Y]
      d_to_closest_not_pred = np.where(
          sorted_d[:, 0] != d_to_pred, sorted_d[:, 0], sorted_d[:, 1]
      )
      trust_score = d_to_closest_not_pred / (d_to_pred + self.eps)
      # closest not predicted class
      class_closest_not_pred = np.where(
          d == d_to_closest_not_pred.reshape(-1, 1)
      )[1]
      return trust_score, class_closest_not_pred
  
file_path: alibi/alibi/confidence/trustscore.py
imports: import logging
  from typing import Any, Optional, Tuple
  import numpy
  from sklearn.neighbors import KDTree, KNeighborsClassifier
label_desc: Use image node_id 5 for calling the TrustScore obj's underlying member method code with example usage: obj.score(X, Y, k, dist_type) and returns: trust_score,  class_closest_not_pred