code: def knn(X, k=1):
      """returns the k-nearest-neighbours graph of the data
  
      Parameters
      ----------
      X, array of shape (n_samples, n_features): the input data
      k, int, optional:  is the number of neighbours considered
  
      Returns
      -------
      the corresponding WeightedGraph instance
  
      Notes
      -----
      The knn system is symmeterized: if (ab) is one of the edges then (ba) is
      also included
      """
      from ..utils.fast_distance import euclidean_distance
  
      if np.size(X) == X.shape[0]:
          X = np.reshape(X, (np.size(X), 1))
      try:
          k = int(k)
      except:
          "k cannot be cast to an int"
      if np.isnan(k):
          raise ValueError("k is nan")
      if np.isinf(k):
          raise ValueError("k is inf")
      k = min(k, X.shape[0] - 1)
  
      # create the distance matrix
      dist = euclidean_distance(X)
      sorted_dist = dist.copy()
      sorted_dist.sort(0)
  
      # neighbour system
      bool_knn = dist < sorted_dist[k + 1]
      bool_knn += bool_knn.T
      # xor diagonal
      bool_knn ^= np.diag(np.diag(bool_knn))
      dist *= bool_knn > 0
      return wgraph_from_adjacency(dist)
  
imports: import numpy
  from scipy.sparse import coo_matrix
label_desc: Use image node_id 5 for calling a global function with example usage: knn(X, k) and returns: wgraph_from_adjacency