code: def best_fitting_GMM(
      x,
      krange,
      prec_type="full",
      niter=100,
      delta=1.0e-4,
      ninit=1,
      verbose=0,
  ):
      """
      Given a certain dataset x, find the best-fitting GMM
      with a number k of classes in a certain range defined by krange
  
      Parameters
      ----------
      x: array of shape (n_samples,dim)
         the data from which the model is estimated
      krange: list of floats,
              the range of values to test for k
      prec_type: string (to be chosen within 'full','diag'), optional,
                the covariance parameterization
      niter: int, optional,
             maximal number of iterations in the estimation process
      delta: float, optional,
             increment of data likelihood at which convergence is declared
      ninit: int
             number of initialization performed
      verbose=0: verbosity mode
  
      Returns
      -------
      mg : the best-fitting GMM instance
      """
      if np.size(x) == x.shape[0]:
          x = np.reshape(x, (np.size(x), 1))
  
      dim = x.shape[1]
      bestbic = -np.inf
      for k in krange:
          lgmm = GMM(k, dim, prec_type)
          gmmk = lgmm.initialize_and_estimate(
              x, None, niter, delta, ninit, verbose
          )
          bic = gmmk.evidence(x)
          if bic > bestbic:
              bestbic = bic
              bgmm = gmmk
          if verbose:
              print("k", k, "bic", bic)
      return bgmm
  
imports: import numpy
  from scipy.linalg import eigvalsh
label_desc: Use image node_id 1 for calling a global function with example usage: best_fitting_GMM(x, krange, prec_type, niter, delta, ninit, verbose) and returns: bgmm