code: def k_modes(
      X,
      n_clusters,
      max_iter,
      dissim,
      init,
      n_init,
      verbose,
      random_state,
      n_jobs,
      sample_weight=None,
  ):
      """k-modes algorithm"""
      random_state = check_random_state(random_state)
      if sparse.issparse(X):
          raise TypeError("k-modes does not support sparse data.")
  
      X = check_array(X, dtype=None)
  
      # Convert the categorical values in X to integers for speed.
      # Based on the unique values in X, we can make a mapping to achieve this.
      X, enc_map = encode_features(X)
  
      n_points, n_attrs = X.shape
      assert n_clusters <= n_points, (
          f"Cannot have more clusters ({n_clusters}) "
          f"than data points ({n_points})."
      )
  
      # Are there more n_clusters than unique rows? Then set the unique
      # rows as initial values and skip iteration.
      unique = get_unique_rows(X)
      n_unique = unique.shape[0]
      if n_unique <= n_clusters:
          max_iter = 0
          n_init = 1
          n_clusters = n_unique
          init = unique
  
      results = []
      seeds = random_state.randint(np.iinfo(np.int32).max, size=n_init)
      if n_jobs == 1:
          for init_no in range(n_init):
              results.append(
                  _k_modes_single(
                      X,
                      n_clusters,
                      n_points,
                      n_attrs,
                      max_iter,
                      dissim,
                      init,
                      init_no,
                      verbose,
                      seeds[init_no],
                      sample_weight,
                  )
              )
      else:
          results = Parallel(n_jobs=n_jobs, verbose=0)(
              delayed(_k_modes_single)(
                  X,
                  n_clusters,
                  n_points,
                  n_attrs,
                  max_iter,
                  dissim,
                  init,
                  init_no,
                  verbose,
                  seed,
                  sample_weight,
              )
              for init_no, seed in enumerate(seeds)
          )
      (
          all_centroids,
          all_labels,
          all_costs,
          all_n_iters,
          all_epoch_costs,
      ) = zip(*results)
  
      best = np.argmin(all_costs)
      if n_init > 1 and verbose:
          print(f"Best run was number {best + 1}")
  
      return (
          all_centroids[best],
          enc_map,
          all_labels[best],
          all_costs[best],
          all_n_iters[best],
          all_epoch_costs[best],
      )
  
imports: from collections import defaultdict
  import numpy
  from joblib import Parallel, delayed
  from scipy import sparse
  from sklearn.base import BaseEstimator, ClusterMixin
  from sklearn.utils import check_random_state
  from sklearn.utils.validation import check_array
  from .util import get_max_value_key, encode_features, get_unique_rows, decode_centroids, pandas_to_numpy
  from .util.dissim import matching_dissim, ng_dissim
  from .util.init_methods import init_cao, init_huang
label_desc: Use image node_id 2 for calling a global function with example usage: k_modes(X, n_clusters, max_iter, dissim, init, n_init, verbose, random_state, n_jobs, sample_weight) and returns: all_centroids,  enc_map,  all_labels,  all_costs,  all_n_iters,  all_epoch_costs