def compute_weights(x, C):
"""Number of points in x assigned to each centre c in C
Parameters
----------
x : ndarray
(n, d) array of n d-dimensional points
C : ndarray
(k, d) array of k cluster centres
Returns
-------
weights : ndarray
(k,) length array giving number of x closest to each c in C
"""
nsplits = max(1, int(x.shape[0]/distance_partition_size))
splits = np.array_split(x, nsplits)
closests = np.empty(x.shape[0], dtype=int)
idx = 0
for x_i in splits:
n_i = x_i.shape[0]
D2_x = scipy.spatial.distance.cdist(x_i, C, metric='sqeuclidean')
closests[idx: idx+n_i] = np.argmin(D2_x, axis=1)
idx += n_i
weights = np.bincount(closests, minlength=C.shape[0])
return weights
评论列表
文章目录