code: def _daal4py_k_means_fit(
      X,
      nClusters,
      numIterations,
      tol,
      cluster_centers_0,
      n_init,
      random_state,
  ):
      if numIterations < 0:
          raise ValueError("Wrong iterations number")
  
      X_fptype = getFPType(X)
      abs_tol = _tolerance(X, tol)  # tol is relative tolerance
  
      best_inertia, best_cluster_centers = None, None
      best_n_iter = -1
  
      kmeans_algo = _daal4py_kmeans_compatibility(
          nClusters=nClusters,
          maxIterations=numIterations,
          accuracyThreshold=abs_tol,
          fptype=X_fptype,
          resultsToEvaluate="computeCentroids",
          method="defaultDense",
      )
  
      for k in range(n_init):
          (
              deterministic,
              starting_centroids_,
          ) = _daal4py_compute_starting_centroids(
              X, X_fptype, nClusters, cluster_centers_0, random_state
          )
  
          res = kmeans_algo.compute(X, starting_centroids_)
  
          inertia = res.objectiveFunction[0, 0]
          if best_inertia is None or inertia < best_inertia:
              best_cluster_centers = res.centroids
              if n_init > 1:
                  best_cluster_centers = best_cluster_centers.copy()
              best_inertia = inertia
              best_n_iter = int(res.nIterations[0, 0])
          if deterministic and 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,
              )
              break
  
      flag_compute = "computeAssignments|computeExactObjectiveFunction"
      best_labels, best_inertia = _daal4py_k_means_predict(
          X, nClusters, best_cluster_centers, flag_compute
      )
      return (
          best_cluster_centers,
          best_labels,
          best_inertia,
          best_n_iter,
      )
  
imports: import numpy
  from scipy import sparse
  from sklearn.utils import check_array, check_random_state
  from sklearn.utils.sparsefuncs import mean_variance_axis
  from sklearn.utils.validation import _num_samples, check_is_fitted
  import warnings
  from sklearn.cluster import KMeans
  from sklearn.utils.extmath import row_norms
  import daal4py
  from .._device_offload import support_usm_ndarray
  from .._utils import PatchingConditionsChain, control_n_jobs, daal_check_version, getFPType, run_with_n_jobs
label_desc: Use image node_id 5 for calling a global function with example usage: _daal4py_k_means_fit(X, nClusters, numIterations, tol, cluster_centers_0, n_init, random_state) and returns: best_cluster_centers,  best_labels,  best_inertia,  best_n_iter