Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. clusters = {}
  2. for point_index, point in enumerate(D):
  3.     point_cluster = C[point_index]
  4.     if point_cluster not in clusters:
  5.         clusters[point_cluster] = []
  6.     clusters[point_cluster].append(point)
  7. final_result = [None] * len(clusters)
  8. for key in clusters:
  9.     centroid = get_centroid(clusters[key])
  10.     final_result[key] = centroid
  11.    
  12.  
  13. def get_centroid(cluster):
  14.     total_x = 0
  15.     total_y = 0
  16.     for x,y in cluster:
  17.         total_x += x
  18.         total_y += y
  19.     n = len(cluster)
  20.     return [1.0 * total_x / n, 1.0 * total_y / n]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement