class_docstr: Calculate trust score.
  
  Parameters
  ----------
  k_filter : int , default: 10
      Number of neighbors used during either kNN distance or probability filtering.
  alpha : float , default: 0.
      Fraction of instances to filter out to reduce impact of outliers.
  filter_type : str , default: distance_knn
      Filter method; either 'distance_knn' or 'probability_knn'
  leaf_size : int , default: 40
      Number of points at which to switch to brute-force. Affects speed and memory required to
      build trees. Memory to store the tree scales with n_samples / leaf_size.
  metric : str , default: euclidean
      Distance metric used for the tree. See sklearn's DistanceMetric class for a list of available
      metrics.
  dist_filter_type : str , default: point
      Use either the distance to the k-nearest point (dist_filter_type = 'point') or
      the average distance from the first to the k-nearest point in the data (dist_filter_type = 'mean').
class_docstr_tok: ["Calculate","trust","score",".","Parameters","--","--","--","--","--","k_filter",":","int",",","default",":","10","Number","of","neighbors","used","during","either","kNN","distance","or","probability","filtering",".","alpha",":","float",",","default",":","0",".","Fraction","of","instances","to","filter","out","to","reduce","impact","of","outliers",".","filter_type",":","str",",","default",":","distance_knn","Filter","method",";","either","'distance_knn","'","or","'probability_knn'","leaf_size",":","int",",","default",":","40","Number","of","points","at","which","to","switch","to","brute-force",".","Affects","speed","and","memory","required","to","build","trees",".","Memory","to","store","the","tree","scales","with","n_samples","\/","leaf_size",".","metric",":","str",",","default",":","euclidean","Distance","metric","used","for","the","tree",".","See","sklearn","'s","DistanceMetric","class","for","a","list","of","available","metrics",".","dist_filter_type",":","str",",","default",":","point","Use","either","the","distance","to","the","k-nearest","point","(","dist_filter_type","=","'point","'",")","or","the","average","distance","from","the","first","to","the","k-nearest","point","in","the","data","(","dist_filter_type","=","'mean","'",")","."]
docstr: Calculate trust scores.
  
  ratio of distance to closest class other than the predicted class to distance to predicted class.
  
  Parameters
  ----------
  X : np.ndarray
      Instances to calculate trust score for.
  Y : np.ndarray
      Either prediction probabilities for each class or the predicted class.
  k : int , default: 2
      Number of nearest neighbors used for distance calculation.
  dist_type : str , default: point
      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
  -------
  Tuple[np.ndarray, np.ndarray]
      Batch with trust scores and the closest not predicted class.
docstr_tok: ["Calculate","trust","scores",".","ratio","of","distance","to","closest","class","other","than","the","predicted","class","to","distance","to","predicted","class",".","Parameters","--","--","--","--","--","X",":","np.ndarray","Instances","to","calculate","trust","score","for",".","Y",":","np.ndarray","Either","prediction","probabilities","for","each","class","or","the","predicted","class",".","k",":","int",",","default",":","2","Number","of","nearest","neighbors","used","for","distance","calculation",".","dist_type",":","str",",","default",":","point","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","--","--","--","-","Tuple","[","np.ndarray",",","np.ndarray","]","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 : np.ndarray
          Instances to calculate trust score for.
      Y : np.ndarray
          Either prediction probabilities for each class or the predicted class.
      k : int , default: 2
          Number of nearest neighbors used for distance calculation.
      dist_type : str , default: point
          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
      -------
      Tuple[np.ndarray, np.ndarray]
          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:
          X = X.reshape(X.shape[0], -1)
  
      d = np.tile(
          None, (X.shape[0], self.classes)
      )  # init distance matrix: [nb instances, nb classes]
  
      for c in range(self.classes):
          if (self.kdtrees[c] is None) or (
              self.kdtrees[c].data.shape[0] < k
          ):
              d[:, c] = np.inf
          else:
              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.nanmean(
                      d_tmp[np.isfinite(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: deepchecks/deepchecks/utils/distribution/trust_score.py
imports: from typing import Tuple
  import numpy
  from sklearn.neighbors import KDTree, KNeighborsClassifier
  from deepchecks.utils.logger import get_logger
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