original_docstring: Test for convergence.
          Easiest way, also saves most memory, is to test if the centroids distance from the
          previous centroid is less than epsilon (usually a small value).
          Hence, if the centroid has not moved or barely moved, the desired number of clusters has been obtained
  
      Args:
          parameters: see k_means.utils.mapping.Parameters for more details.
          data_eng: see k_means.core.data_prep.DataEng for more details.
          counter: the iteration number.
  
      Returns:
          data_eng: if we have not reached convergence,
              we need to reset our clusters and assign our new found centroids as the previous ones.
          convergence: True or False, have we reached convergence or not.
      
docstring: Test for convergence.
  Easiest way, also saves most memory, is to test if the centroids distance from the
  previous centroid is less than epsilon (usually a small value).
  Hence, if the centroid has not moved or barely moved, the desired number of clusters has been obtained
docstring_tokens: Test for convergence . Easiest way also saves most memory is to test if the centroids distance from the previous centroid is less than epsilon ( usually a small value ) . Hence if the centroid has not moved or barely moved the desired number of clusters has been obtained
code: def check_for_convergence(parameters: Parameters,
                            data_eng: DataEng,
                            counter: int) -> [DataEng, bool]:
      """Test for convergence.
          Easiest way, also saves most memory, is to test if the centroids distance from the
          previous centroid is less than epsilon (usually a small value).
          Hence, if the centroid has not moved or barely moved, the desired number of clusters has been obtained
  
      Args:
          parameters: see k_means.utils.mapping.Parameters for more details.
          data_eng: see k_means.core.data_prep.DataEng for more details.
          counter: the iteration number.
  
      Returns:
          data_eng: if we have not reached convergence,
              we need to reset our clusters and assign our new found centroids as the previous ones.
          convergence: True or False, have we reached convergence or not.
      """
      distances_bool = []
      try:
          for i in range(parameters.num_clusters):
              dist_cen = sqrt((data_eng.centroids_new[i][0] - data_eng.centroids_prev[i][0]) ** 2 + (
                      data_eng.centroids_new[i][1] - data_eng.centroids_prev[i][1]) ** 2)
              if dist_cen <= parameters.eps:
                  distances_bool.append(True)
              elif dist_cen > parameters.eps:
                  distances_bool.append(False)
      except IndexError as index_expt:
          print_detailed_exception(currentframe().f_code.co_name, sys.exc_info(), index_expt)
      # if all distances are under a certain eps threshold, end algorithm;
      # if not, assign new centroids as previous, and clear clusters from plots
      if all(distances_bool):
          print(f'\nConvergence achieved in {counter + 1} iterations')
          convergence = True
      elif not all(distances_bool):
          data_eng.centroids_prev = data_eng.centroids_new
          data_eng.clusters = clusters_list(parameters.num_clusters)
          convergence = False
      # also test if max number of iterations has been reached
      if counter >= parameters.max_iter:
          print('Convergence timed out by Maximum number of iterations')
          convergence = True
      return data_eng, convergence
code_tokens: def check_for_convergence ( parameters : Parameters , data_eng : DataEng , counter : int ) -> [ DataEng , bool ] : distances_bool = [ ] try : for i in range ( parameters . num_clusters ) : dist_cen = sqrt ( ( data_eng . centroids_new [ i ] [ 0 ] - data_eng . centroids_prev [ i ] [ 0 ] ) ** 2 + ( data_eng . centroids_new [ i ] [ 1 ] - data_eng . centroids_prev [ i ] [ 1 ] ) ** 2 ) if dist_cen <= parameters . eps : distances_bool . append ( True ) elif dist_cen > parameters . eps : distances_bool . append ( False ) except IndexError as index_expt : print_detailed_exception ( currentframe ( ) . f_code . co_name , sys . exc_info ( ) , index_expt ) if all ( distances_bool ) : print ( f'\nConvergence achieved in {counter + 1} iterations' ) convergence = True elif not all ( distances_bool ) : data_eng . centroids_prev = data_eng . centroids_new data_eng . clusters = clusters_list ( parameters . num_clusters ) convergence = False if counter >= parameters . max_iter : print ( 'Convergence timed out by Maximum number of iterations' ) convergence = True return data_eng , convergence
short_docstring: Test for convergence.
short_docstring_tokens: Test for convergence .
comment: """Test for convergence.
          Easiest way, also saves most memory, is to test if the centroids distance from the
          previous centroid is less than epsilon (usually a small value).
          Hence, if the centroid has not moved or barely moved, the desired number of clusters has been obtained
  
      Args:
          parameters: see k_means.utils.mapping.Parameters for more details.
          data_eng: see k_means.core.data_prep.DataEng for more details.
          counter: the iteration number.
  
      Returns:
          data_eng: if we have not reached convergence,
              we need to reset our clusters and assign our new found centroids as the previous ones.
          convergence: True or False, have we reached convergence or not.
      """$SEP$# if all distances are under a certain eps threshold, end algorithm;$SEP$# if not, assign new centroids as previous, and clear clusters from plots$SEP$# also test if max number of iterations has been reached