code: def mkneighbors_graph(
      observations,
      n_neighbours,
      metric,
      mode="connectivity",
      metric_params=None,
  ):
      """
      Computes the (weighted) graph of mutual k-Neighbors for observations.
  
      Notes
      -----
      The distance between an observation and itself is never computed and instead set to
      ``numpy.inf``. I.e. only in the case of k>=n_observations or when the ``metric``
      returns ``numpy.inf``, the returned graph can contain loops.
  
      Parameters
      ----------
      observations : sequence
          Sequence of observations.
      n_neighbours : int
          Maximum number of neighbours for each sample.
      metric : function
          The distance metric taking two observations and returning a numeric value > 0.
      mode : {'connectivity', 'distance', 'both'}, optional
          Type of returned matrix: 'connectivity' will return the connectivity matrix with
          ones and zeros, in 'distance' the edges are distances between points, while
          'both' returns a (connectivity, distance) tuple.
      metric_params : dict, optional  (default = None)
              Additional keyword arguments for the metric function.
  
      Returns
      -------
      mkneighbors_graph : ndarray
          Sparse matrix in CSR format, shape = [n_observations, n_observations].
          mkneighbors_graph[i, j] is assigned the weight of edge that connects i to j.
          Might contain ``numpy.inf`` values.
  
      """
      # compute their pairwise-distances
      pdists = pdist(observations, metric)
  
      # get the k nearest neighbours for each patch
      k_nearest_nbhs = numpy.argsort(pdists)[:, :n_neighbours]
  
      # create a mask denoting the k nearest neighbours in image_pdist
      k_nearest_mutual_nbhs_mask = numpy.zeros(
          pdists.shape, numpy.bool_
      )
      for _mask_row, _nbhs_row in zip(
          k_nearest_mutual_nbhs_mask, k_nearest_nbhs
      ):
          _mask_row[_nbhs_row] = True
  
      # and with transposed to remove non-mutual nearest neighbours
      k_nearest_mutual_nbhs_mask &= k_nearest_mutual_nbhs_mask.T
  
      # set distance not in the mutual k nearest neighbour set to zero
      pdists[~k_nearest_mutual_nbhs_mask] = 0
  
      # check for edges with zero-weight
      if numpy.any(pdists[k_nearest_mutual_nbhs_mask] == 0):
          warnings.warn(
              'The graph contains at least one edge with a weight of "0".'
          )
  
      if "connectivity" == mode:
          return csr_matrix(k_nearest_mutual_nbhs_mask)
      elif "distance" == mode:
          return csr_matrix(pdists)
      else:
          return csr_matrix(k_nearest_mutual_nbhs_mask), csr_matrix(
              pdists
          )
  
imports: from itertools import combinations
  import warnings
  import numpy
  from scipy.sparse.csr import csr_matrix
label_desc: Use image node_id 1 for calling a global function with example usage: mkneighbors_graph(observations, n_neighbours, metric, mode, metric_params) and returns: csr_matrix, csr_matrix, csr_matrix,  csr_matrix