python - Implementing K-Medoids in numpy: the medoids selection step -
as author of this question i'm trying implement k-medoids using numpy. i'm more interested in how implement medoids-individuation step (second step in [2]), consisting in selecting, cluster cluster, sample minimizes sum of distances other samples belonging same cluster. let assume have same structures described in [1]:
# number of samples n_samples = 5 # distance square matrix d = np.array([[ 0., 3.04959014, 4.74341649, 3.72424489, 6.70298441], [ 3.04959014, 0. , 5.38516481, 4.52216762, 6.16846821], [ 4.74341649, 5.38516481, 0. , 1.02469508, 8.23711114], [ 3.72424489, 4.52216762, 1.02469508, 0. , 7.69025357], [ 6.70298441, 6.16846821, 8.23711114, 7.69025357, 0. ]]) # medoids medoids = np.array([0, 3]) # cluster membership array cl = np.array([0, 0, 1, 1, 0])
i can't implement using numpy ... can me?
[edit] current best solution is:
for c in range(number_of_clusters): ind = np.where(cl == c)[0] m = np.argmin(d[np.ix_(ind, ind)].sum(axis=0)) medoids[c] = ind[m]
Comments
Post a Comment