code: def k_means(
      X,
      n_clusters,
      init="k-means++",
      precompute_distances="auto",
      n_init=10,
      max_iter=300,
      verbose=False,
      tol=1e-4,
      random_state=None,
      copy_x=True,
      n_jobs=1,
      algorithm="auto",
      return_n_iter=False,
  ):
      """K-means clustering algorithm.
  
      Read more in the :ref:`User Guide <k_means>`.
  
      Parameters
      ----------
      X : array-like or sparse matrix, shape (n_samples, n_features)
          The observations to cluster.
  
      n_clusters : int
          The number of clusters to form as well as the number of
          centroids to generate.
  
      max_iter : int, optional, default 300
          Maximum number of iterations of the k-means algorithm to run.
  
      n_init : int, optional, default: 10
          Number of time the k-means algorithm will be run with different
          centroid seeds. The final results will be the best output of
          n_init consecutive runs in terms of inertia.
  
      init : {'k-means++', 'random', or ndarray, or a callable}, optional
          Method for initialization, default to 'k-means++':
  
          'k-means++' : selects initial cluster centers for k-mean
          clustering in a smart way to speed up convergence. See section
          Notes in k_init for more details.
  
          'random': generate k centroids from a Gaussian with mean and
          variance estimated from the data.
  
          If an ndarray is passed, it should be of shape (n_clusters, n_features)
          and gives the initial centers.
  
          If a callable is passed, it should take arguments X, k and
          and a random state and return an initialization.
  
      algorithm : "auto", "full" or "elkan", default="auto"
          K-means algorithm to use. The classical EM-style algorithm is "full".
          The "elkan" variation is more efficient by using the triangle
          inequality, but currently doesn't support sparse data. "auto" chooses
          "elkan" for dense data and "full" for sparse data.
  
      precompute_distances : {'auto', True, False}
          Precompute distances (faster but takes more memory).
  
          'auto' : do not precompute distances if n_samples * n_clusters > 12
          million. This corresponds to about 100MB overhead per job using
          double precision.
  
          True : always precompute distances
  
          False : never precompute distances
  
      tol : float, optional
          The relative increment in the results before declaring convergence.
  
      verbose : boolean, optional
          Verbosity mode.
  
      random_state : integer or numpy.RandomState, optional
          The generator used to initialize the centers. If an integer is
          given, it fixes the seed. Defaults to the global numpy random
          number generator.
  
      copy_x : boolean, optional
          When pre-computing distances it is more numerically accurate to center
          the data first.  If copy_x is True, then the original data is not
          modified.  If False, the original data is modified, and put back before
          the function returns, but small numerical differences may be introduced
          by subtracting and then adding the data mean.
  
      n_jobs : int
          The number of jobs to use for the computation. This works by computing
          each of the n_init runs in parallel.
  
          If -1 all CPUs are used. If 1 is given, no parallel computing code is
          used at all, which is useful for debugging. For n_jobs below -1,
          (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one
          are used.
  
      return_n_iter : bool, optional
          Whether or not to return the number of iterations.
  
      Returns
      -------
      centroid : float ndarray with shape (k, n_features)
          Centroids found at the last iteration of k-means.
  
      label : integer ndarray with shape (n_samples,)
          label[i] is the code or index of the centroid the
          i'th observation is closest to.
  
      inertia : float
          The final value of the inertia criterion (sum of squared distances to
          the closest centroid for all observations in the training set).
  
      best_n_iter: int
          Number of iterations corresponding to the best results.
          Returned only if `return_n_iter` is set to True.
  
      """
      if n_init <= 0:
          raise ValueError(
              "Invalid number of initializations."
              " n_init=%d must be bigger than zero." % n_init
          )
      random_state = check_random_state(random_state)
  
      if max_iter <= 0:
          raise ValueError(
              "Number of iterations should be a positive number,"
              " got %d instead" % max_iter
          )
  
      best_inertia = np.infty
      X = as_float_array(X, copy=copy_x)
      tol = _tolerance(X, tol)
  
      # If the distances are precomputed every job will create a matrix of shape
      # (n_clusters, n_samples). To stop KMeans from eating up memory we only
      # activate this if the created matrix is guaranteed to be under 100MB. 12
      # million entries consume a little under 100MB if they are of type double.
      if precompute_distances == "auto":
          n_samples = X.shape[0]
          precompute_distances = (n_clusters * n_samples) < 12e6
      elif isinstance(precompute_distances, bool):
          pass
      else:
          raise ValueError(
              "precompute_distances should be 'auto' or True/False"
              ", but a value of %r was passed" % precompute_distances
          )
  
      # subtract of mean of x for more accurate distance computations
      if not sp.issparse(X) or hasattr(init, "__array__"):
          X_mean = X.mean(axis=0)
      if not sp.issparse(X):
          # The copy was already done above
          X -= X_mean
  
      if hasattr(init, "__array__"):
          init = check_array(init, dtype=X.dtype.type, copy=True)
          _validate_center_shape(X, n_clusters, init)
  
          init -= X_mean
          if n_init != 1:
              warnings.warn(
                  "Explicit initial center position passed: "
                  "performing only one init in k-means instead of n_init=%d"
                  % n_init,
                  RuntimeWarning,
                  stacklevel=2,
              )
              n_init = 1
  
      # precompute squared norms of data points
      x_squared_norms = row_norms(X, squared=True)
  
      best_labels, best_inertia, best_centers = None, None, None
      if n_clusters == 1:
          # elkan doesn't make sense for a single cluster, full will produce
          # the right result.
          algorithm = "full"
      if algorithm == "auto":
          algorithm = "full" if sp.issparse(X) else "elkan"
      if algorithm == "full":
          kmeans_single = _kmeans_single_lloyd
      elif algorithm == "elkan":
          kmeans_single = _kmeans_single_elkan
      else:
          raise ValueError(
              "Algorithm must be 'auto', 'full' or 'elkan', got"
              " %s" % str(algorithm)
          )
      if n_jobs == 1:
          # For a single thread, less memory is needed if we just store one set
          # of the best results (as opposed to one set per run per thread).
          for it in range(n_init):
              # run a k-means once
              labels, inertia, centers, n_iter_ = kmeans_single(
                  X,
                  n_clusters,
                  max_iter=max_iter,
                  init=init,
                  verbose=verbose,
                  precompute_distances=precompute_distances,
                  tol=tol,
                  x_squared_norms=x_squared_norms,
                  random_state=random_state,
              )
              # determine if these results are the best so far
              if best_inertia is None or inertia < best_inertia:
                  best_labels = labels.copy()
                  best_centers = centers.copy()
                  best_inertia = inertia
                  best_n_iter = n_iter_
      else:
          # parallelisation of k-means runs
          seeds = random_state.randint(
              np.iinfo(np.int32).max, size=n_init
          )
          results = Parallel(n_jobs=n_jobs, verbose=0)(
              delayed(kmeans_single)(
                  X,
                  n_clusters,
                  max_iter=max_iter,
                  init=init,
                  verbose=verbose,
                  tol=tol,
                  precompute_distances=precompute_distances,
                  x_squared_norms=x_squared_norms,
                  # Change seed to ensure variety
                  random_state=seed,
              )
              for seed in seeds
          )
          # Get results with the lowest inertia
          labels, inertia, centers, n_iters = zip(*results)
          best = np.argmin(inertia)
          best_labels = labels[best]
          best_inertia = inertia[best]
          best_centers = centers[best]
          best_n_iter = n_iters[best]
  
      if not sp.issparse(X):
          if not copy_x:
              X += X_mean
          best_centers += X_mean
  
      if return_n_iter:
          return best_centers, best_labels, best_inertia, best_n_iter
      else:
          return best_centers, best_labels, best_inertia
  
imports: import warnings
  import numpy
  import scipy.sparse
  from ..base import BaseEstimator, ClusterMixin, TransformerMixin
  from ..metrics.pairwise import euclidean_distances
  from ..metrics.pairwise import pairwise_distances_argmin_min
  from ..utils.extmath import row_norms, squared_norm
  from ..utils.sparsefuncs_fast import assign_rows_csr
  from ..utils.sparsefuncs import mean_variance_axis
  from ..utils.fixes import astype
  from ..utils import check_array
  from ..utils import check_random_state
  from ..utils import as_float_array
  from ..utils import gen_batches
  from ..utils.validation import check_is_fitted
  from ..utils.validation import FLOAT_DTYPES
  from ..utils.random import choice
  from ..externals.joblib import Parallel
  from ..externals.joblib import delayed
  from ..externals.six import string_types
  from .None import _k_means
  from ._k_means_elkan import k_means_elkan
label_desc: Use image node_id 4 for calling a global function with example usage: k_means(X, n_clusters, init, precompute_distances, n_init, max_iter, verbose, tol, random_state, copy_x, n_jobs, algorithm, return_n_iter) and returns: best_centers,  best_labels,  best_inertia,  best_n_iter, best_centers,  best_labels,  best_inertia