Advertisement
Natalia_L

Untitled

Nov 10th, 2020
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. def compute_clusters(X, centers):
  2.     """
  3.    INPUT:
  4.    X - np.array, (n_objects x n_features) - матрица объекты-признаки
  5.    centers - np.array, (n_clusters x n_features) - матрица, содержащая координаты центров кластеров
  6.    
  7.    OUTPUT:
  8.    y - np.array, (n_objects,) - вектор, состоящий из меток кластеров (0, 1, ..., n_clusters-1), где
  9.    на i-ом месте стоит метка кластера для i-ого объекта выборки X
  10.    """
  11.     #n_objects, n_features = X.shape
  12.     #n_clusters, n_features = centers.shape
  13.    
  14.     distance = pairwise_distances(X, centers, metric = 'euclidean')
  15.     y = np.argmin(distance, axis = 1)
  16.    
  17.     return y
  18.  
  19. centers = (np.arange(6)*0.05).reshape(3, 2)
  20. assert np.allclose(
  21.     compute_clusters(X, centers),
  22.     np.array([0, 2, 2, 2, 2, 0, 1, 2, 2, 2, 1, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0,
  23.        2, 0, 2, 0, 0, 2, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0,
  24.        0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0,
  25.        2, 0, 0, 1, 2, 0, 0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0,
  26.        2, 1, 0, 0, 0, 2, 0, 2, 2, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  27.        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28.        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29.        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30.        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  31.        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32.        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  33.        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  34.        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  35.        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement