code: def _get_kmeans_lookup_table_and_weight(
      nbits, w, force_kmeans1d=False
  ):
      """
      Generate K-Means lookup table given weights
  
      nbits:
          Number of bits for quantization
  
      w:
          Weights as numpy array
  
      force_kmeans1d:
          Use kmeans1d regardless of number of weights
  
      Returns
      -------
      lut: numpy.array
          Lookup table, numpy array of shape (1 << nbits, )
      wq: numpy.array
          Quantized weight of type numpy.uint8
      """
      num_weights = _np.prod(w.shape)
      lut_len = 1 << nbits
      wf = w.reshape(-1, 1)
      lut = _np.zeros(lut_len)
  
      is_better_to_use_kmeans1d = (
          num_weights >= 10_000 and w.dtype == _np.float16
      )
  
      if (
          is_better_to_use_kmeans1d and _HAS_KMEANS1D
      ) or force_kmeans1d:
          # Cluster with kmeans1d
          assert _HAS_KMEANS1D
          values, indices, counts = _np.unique(
              wf, return_inverse=True, return_counts=True
          )
          n_clusters = min(len(values), lut_len)
          kmeans_results = _kmeans1d.cluster(
              values, n_clusters, weights=counts
          )
          lut[:n_clusters] = kmeans_results.centroids
          wq = _np.array(kmeans_results.clusters)[indices]
      else:
          # Cluster with scikit-learn
          try:
              from sklearn.cluster import KMeans
          except:
              raise ModuleNotFoundError(
                  "scikit-learn is required for k-means quantization."
                  ' To install, run: "pip install scikit-learn".'
              )
  
          if is_better_to_use_kmeans1d:
              _logger.warning(
                  "It would be better to use kmeans1d but that is not available."
                  " Using scikit-learn for K-means."
              )
  
          n_clusters = min(num_weights, lut_len)
          kmeans = KMeans(
              n_clusters,
              init="k-means++",
              tol=1e-2,
              n_init=1,
              random_state=0,
          ).fit(wf)
          wq = kmeans.labels_[:num_weights]
          lut[:n_clusters] = kmeans.cluster_centers_.flatten()
  
      return lut, wq
  
imports: from os import listdir
  from sys import stdout
  import numpy
  from coremltools import ComputeUnit, _logger
  from coremltools._deps import _HAS_KMEANS1D, _kmeans1d
  from coremltools.models import _QUANTIZATION_MODE_CUSTOM_LOOKUP_TABLE, _QUANTIZATION_MODE_DEQUANTIZE, _QUANTIZATION_MODE_LINEAR_QUANTIZATION, _QUANTIZATION_MODE_LINEAR_SYMMETRIC, _QUANTIZATION_MODE_LOOKUP_TABLE_KMEANS, _QUANTIZATION_MODE_LOOKUP_TABLE_LINEAR, _SUPPORTED_QUANTIZATION_MODES, MLModel
  from ...None import _MINIMUM_FP16_SPEC_VERSION, _MINIMUM_QUANTIZED_MODEL_SPEC_VERSION, _SPECIFICATION_VERSION_IOS_14
  from ..utils import _get_model, _macos_version, _wp_to_fp16wp
  from .optimization_utils import _optimize_nn
label_desc: Use image node_id 5 for calling a global function with example usage: _get_kmeans_lookup_table_and_weight(nbits, w, force_kmeans1d) and returns: lut,  wq