code: def kmeans(data, K, max_iter=100):
      """
      Performs k-means clustering of data into K clusters.
      """
      assert K < data.size(0), "more clusters than data points"
  
      # initialize clusters at randomly selected data points:
      perm = torch.randperm(data.size(0))
      clusters = data[perm[:K], :]
      assignments = None
      for iter in range(max_iter):
          # compute assignments, and stop if converged:
          prev_assignments = assignments
          assignments = kmeans_inference(data, clusters)
          if prev_assignments is not None:
              num_changes = assignments.ne(prev_assignments).sum()
              logging.info(
                  "K-means iteration %d: %d assignments changed"
                  % (iter, num_changes)
              )
              if num_changes == 0:
                  break
  
          # re-compute cluster means:
          for k in range(K):
              index = assignments == k
              if index.any():  # ignore empty clusters
                  clusters[k, :] = data[index, :].mean(0)
  
      # done:
      return clusters
  
file_path: CrypTen/examples/util.py
imports: import logging
  import os
  import numpy
  import numpy.linalg
  import torch
label_desc: Use image node_id 3 for calling a global function with example usage: kmeans(data, K, max_iter) and returns: clusters