Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. def kmeans(data, k, c=None):
  2. if c is not None:
  3. centroids = c
  4. else:
  5. centroids = []
  6. centroids = randomize_centroids(data, centroids, k)
  7.  
  8. old_centroids = [[] for i in range(k)]
  9.  
  10. iterations = 0
  11. while not (has_converged(centroids, old_centroids, iterations)):
  12. iterations += 1
  13.  
  14. clusters = [[] for i in range(k)]
  15.  
  16. # assign data points to clusters
  17. clusters = euclidean_dist(data, centroids, clusters)
  18.  
  19. # recalculate centroids
  20. index = 0
  21. for cluster in clusters:
  22. old_centroids[index] = centroids[index]
  23. centroids[index] = np.mean(cluster, axis=0).tolist()
  24. index += 1
  25.  
  26.  
  27. print("The total number of data instances is: " + str(len(data)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement