在以下代码中:
num_centroids = 8
vectors = # ndarray of shape (10000, 128)
centroids = np.random.uniform(-1., 1., (num_centroids , vectors.shape[1]))
for _ in range(max_iterations):
distances = sklearn.metrics.pairwise.euclidean_distances(vectors, centroids)
# can this (the centroid update) be done any cleaner?
for c in range(num_centroids):
centroids[c] = np.mean(vectors[np.argmax(distances, axis=1) == c], axis=0)
是否有一种更有效或更干净的方法(除了for循环)将每个centroid
(centroids
矩阵中的一行)分配给从索引的{{1} }矩阵,假设vector
索引(centroid
)和使用c
的向量索引是相同的?