Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. def quality(x, y):
  2. """
  3. Инерция
  4. """
  5. # сколько всего кластеров
  6. clusters_n = 0
  7. # ответ
  8. res = 0
  9.  
  10. # сколько точек в каждом кластере
  11. points_in_cluster = {}
  12.  
  13. # посчитаем сколько у нас всего кластеров и посчитаем сколько точек в каждом кластере
  14. for cluster_id in y:
  15.  
  16. # пропускаем шумы
  17. if (cluster_id == -1):
  18. continue
  19.  
  20. clusters_n = max(clusters_n, cluster_id)
  21. if (cluster_id in points_in_cluster):
  22. points_in_cluster[cluster_id] += 1
  23. else:
  24. points_in_cluster[cluster_id] = 1
  25. clusters_n += 1
  26.  
  27. centroids = np.zeros(clusters_n, dtype=object)
  28. for index in range(len(x)):
  29.  
  30. # пропускаем шумы
  31. if (y[index] == -1):
  32. continue
  33.  
  34. centroids[y[index]] += np.array(x[index])
  35.  
  36. for cluster_id in points_in_cluster:
  37. centroids[cluster_id] /= points_in_cluster[cluster_id]
  38.  
  39. res = 0
  40. centroids = centroids.tolist()
  41. if (len(centroids) == 1):
  42. return -1
  43. for c in centroids:
  44. c = c.tolist()
  45. for index in range(len(x)):
  46. if (y[index] == -1):
  47. continue
  48. dist = cdist([x[index]], centroids, 'sqeuclidean')[0]
  49. mini_dist = 1e100
  50. for d in dist:
  51. mini_dist = min(mini_dist, d)
  52. res += mini_dist
  53.  
  54. return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement