code: def kmeans2(
      data,
      k,
      iter=10,
      thresh=1e-5,
      minit="random",
      missing="warn",
      check_finite=True,
  ):
      """
      Classify a set of observations into k clusters using the k-means algorithm.
  
      The algorithm attempts to minimize the Euclidian distance between
      observations and centroids. Several initialization methods are
      included.
  
      Parameters
      ----------
      data : ndarray
          A 'M' by 'N' array of 'M' observations in 'N' dimensions or a length
          'M' array of 'M' one-dimensional observations.
      k : int or ndarray
          The number of clusters to form as well as the number of
          centroids to generate. If `minit` initialization string is
          'matrix', or if a ndarray is given instead, it is
          interpreted as initial cluster to use instead.
      iter : int, optional
          Number of iterations of the k-means algorithm to run. Note
          that this differs in meaning from the iters parameter to
          the kmeans function.
      thresh : float, optional
          (not used yet)
      minit : str, optional
          Method for initialization. Available methods are 'random',
          'points', '++' and 'matrix':
  
          'random': generate k centroids from a Gaussian with mean and
          variance estimated from the data.
  
          'points': choose k observations (rows) at random from data for
          the initial centroids.
  
          '++': choose k observations accordingly to the kmeans++ method
          (careful seeding)
  
          'matrix': interpret the k parameter as a k by M (or length k
          array for one-dimensional data) array of initial centroids.
      missing : str, optional
          Method to deal with empty clusters. Available methods are
          'warn' and 'raise':
  
          'warn': give a warning and continue.
  
          'raise': raise an ClusterError and terminate the algorithm.
      check_finite : bool, optional
          Whether to check that the input matrices contain only finite numbers.
          Disabling may give a performance gain, but may result in problems
          (crashes, non-termination) if the inputs do contain infinities or NaNs.
          Default: True
  
      Returns
      -------
      centroid : ndarray
          A 'k' by 'N' array of centroids found at the last iteration of
          k-means.
      label : ndarray
          label[i] is the code or index of the centroid the
          i'th observation is closest to.
  
      References
      ----------
      .. [1] D. Arthur and S. Vassilvitskii, "k-means++: the advantages of
         careful seeding", Proceedings of the Eighteenth Annual ACM-SIAM Symposium
         on Discrete Algorithms, 2007.
      """
      if int(iter) < 1:
          raise ValueError(
              "Invalid iter (%s), " "must be a positive integer." % iter
          )
      try:
          miss_meth = _valid_miss_meth[missing]
      except KeyError:
          raise ValueError("Unknown missing method %r" % (missing,))
  
      data = _asarray_validated(data, check_finite=check_finite)
      if data.ndim == 1:
          d = 1
      elif data.ndim == 2:
          d = data.shape[1]
      else:
          raise ValueError("Input of rank > 2 is not supported.")
  
      if data.size < 1:
          raise ValueError("Empty input is not supported.")
  
      # If k is not a single value it should be compatible with data's shape
      if minit == "matrix" or not np.isscalar(k):
          code_book = np.array(k, copy=True)
          if data.ndim != code_book.ndim:
              raise ValueError("k array doesn't match data rank")
          nc = len(code_book)
          if data.ndim > 1 and code_book.shape[1] != d:
              raise ValueError("k array doesn't match data dimension")
      else:
          nc = int(k)
  
          if nc < 1:
              raise ValueError(
                  "Cannot ask kmeans2 for %d clusters"
                  " (k was %s)" % (nc, k)
              )
          elif nc != k:
              warnings.warn("k was not an integer, was converted.")
  
          try:
              init_meth = _valid_init_meth[minit]
          except KeyError:
              raise ValueError("Unknown init method %r" % (minit,))
          else:
              code_book = init_meth(data, k)
  
      for i in xrange(iter):
          # Compute the nearest neighbor for each obs using the current code book
          label = vq(data, code_book)[0]
          # Update the code book by computing centroids
          new_code_book, has_members = _vq.update_cluster_means(
              data, label, nc
          )
          if not has_members.all():
              miss_meth()
              # Set the empty clusters to their previous positions
              new_code_book[~has_members] = code_book[~has_members]
          code_book = new_code_book
  
      return code_book, label
  
imports: from __future__ import division, print_function, absolute_import
  import warnings
  import numpy
  from collections import deque
  from scipy._lib._util import _asarray_validated
  from scipy._lib.six import xrange
  from scipy.spatial.distance import cdist
  from .None import _vq
label_desc: Use image node_id 11 for calling a global function with example usage: kmeans2(data, k, iter, thresh, minit, missing, check_finite) and returns: code_book,  label