code: def _get_kmeans_lookup_table_and_weight(
      nbits, w, init="k-means++", tol=1e-2, n_init=1, rand_seed=0
  ):
      """
      Generate K-Means lookup table given a weight parameter field
  
      nbits:
          Number of bits for quantization
  
      w:
          Weight as numpy array
  
      Returns
      -------
      lut: numpy.array
          Lookup table, numpy array of shape (1 << nbits, );
      wq: numpy.array
          Quantized weight of type numpy.uint8
      """
      if _HAS_SKLEARN:
          from sklearn.cluster import KMeans
      else:
          raise Exception(
              "sklearn package required for k-means quantization"
          )
      units = _np.prod(w.shape)
      lut_len = 1 << nbits
      n_clusters = units if (units < lut_len) else lut_len
      wf = w.reshape(-1, 1)
      kmeans = KMeans(
          n_clusters=n_clusters,
          init=init,
          tol=tol,
          n_init=n_init,
          random_state=rand_seed,
      ).fit(wf)
      wq = kmeans.labels_[:units]
      lut = _np.zeros(lut_len)
      lut[:n_clusters] = kmeans.cluster_centers_.flatten()
      return lut, wq
  
imports: from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import numpy
  from sys import stdout
  from os import listdir
  from six import string_types
  from .optimization_utils import _optimize_nn
  from coremltools.models import _SUPPORTED_QUANTIZATION_MODES, _QUANTIZATION_MODE_DEQUANTIZE, _QUANTIZATION_MODE_LOOKUP_TABLE_LINEAR, _QUANTIZATION_MODE_LOOKUP_TABLE_KMEANS, _QUANTIZATION_MODE_CUSTOM_LOOKUP_TABLE, _QUANTIZATION_MODE_LINEAR_QUANTIZATION, _QUANTIZATION_MODE_LINEAR_SYMMETRIC, _LUT_BASED_QUANTIZATION
  from ..utils import _get_nn_layers, _wp_to_fp16wp, _get_model, _macos_version
  from ..._deps import _HAS_SKLEARN
  from ...None import _MINIMUM_QUANTIZED_MODEL_SPEC_VERSION, _MINIMUM_FP16_SPEC_VERSION, _SPECIFICATION_VERSION_IOS_14
label_desc: Use image node_id 5 for calling a global function with example usage: _get_kmeans_lookup_table_and_weight(nbits, w, init, tol, n_init, rand_seed) and returns: lut,  wq