input: Write a function to implement the k-means clustering algorithm in Python, which takes the number of clusters k and the data set as inputs, initializes the centroids randomly, and iteratively updates the centroids to minimize the distance to each data point until convergence. Return the final clusters assignment and the centroids.
  import numpy as np 
  
  def kmeans(k, data): 
      """
      Perform k-means clustering on data
      """
      # randomly set initial centroids
      centroids = data[np.random.randint(low=0, high=len(data), size=k)]
      prev_centroids = np.zeros(centroids.shape)
  
      clusters = np.zeros(len(data))
      distances = np.zeros((len(data), k))
      
      # find the closest centroid for each sample
      while not np.allclose(centroids, prev_centroids):
          # save old centroids
          prev_centroids = centroids
      
          # get the distance between each point and each centroid
          for idx, centroid in enumerate(centroids):
              distances[:, idx] = np.linalg.norm(data - centroid, axis=1)
      
          # assign each sample to the closest centroids
          clusters = np.argmin(distances, axis=1)
      
          # update centroids 
          for idx in range(k):
              centroids[idx] = np.mean(data[clusters == idx], axis=0)
      
      return clusters, centroids
  
  assert kmeans(3, np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]))[0].tolist()
  assert kmeans(2, np.array([[1, 1], [1, 3], [4, 4], [6, 1]]))[1].tolist()
output: import numpy as np 
  
  def kmeans(k, data): 
      """
      Perform k-means clustering on data
      """
      # randomly set initial centroids
      centroids = data[np.random.randint(low=0, high=len(data), size=k)]
      prev_centroids = np.zeros(centroids.shape)
  
      clusters = np.zeros(len(data))
      distances = np.zeros((len(data), k))
      
      # find the closest centroid for each sample
      while not np.allclose(centroids, prev_centroids):
          # save old centroids
          prev_centroids = centroids
      
          # get the distance between each point and each centroid
          for idx, centroid in enumerate(centroids):
              distances[:, idx] = np.linalg.norm(data - centroid, axis=1)
      
          # assign each sample to the closest centroids
          clusters = np.argmin(distances, axis=1)
      
          # update centroids 
          for idx in range(k):
              centroids[idx] = np.mean(data[clusters == idx], axis=0)
      
      return clusters, centroids