Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def kMedoids(D, k, tmax=100):
- # determine dimensions of distance matrix D
- m, n = D.shape
- # randomly initialize an array of k medoid indices
- M = np.sort(np.random.choice(n, k)
- # create a copy of the array of medoid indices
- Mnew = np.copy(M)
- # initialize a dictionary to represent clusters
- C = {}
- for t in range(tmax):
- # determine clusters, i.e. arrays of data indices
- J = np.argmin(D[:,M], axis=1)
- for kappa in range(k):
- C[kappa] = np.where(J==kappa)[0]
- # update cluster medoids
- for kappa in range(k):
- J = np.mean(D[np.ix_(C[kappa],C[kappa])],axis=1)
- j = np.argmin(J)
- Mnew[kappa] = C[kappa][j]
- np.sort(Mnew)
- # check for convergence
- if np.array_equal(M, Mnew):
- break
- M = np.copy(Mnew)
- else:
- # final update of cluster memberships
- J = np.argmin(D[:,M], axis=1)
- for kappa in range(k):
- C[kappa] = np.where(J==kappa)[0]
- # return results
- return M, C
- D = pairwise_distances(arraydata,metric='euclidean')
- # split into 2 clusters
- M, C = kMedoids(D, 2)
- print('medoids:')
- for point_idx in M:
- print(arraydata[point_idx] )
- print('')
- # array for get label
- temp = []
- indeks = []
- print('clustering result:')
- for label in C:
- for point_idx in C[label]:
- print('label {0}: {1}'.format(label, arraydata[point_idx]))
- temp.append(label)
- indeks.append(point_idx)
- clustering result:
- label 0: [0.00000000e+00 0.00000000e+00 1.00000000e+00 1.00000000e+00
- 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
- 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
- 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
- 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
- 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
- 1.00000000e+00 0.00000000e+00 1.00000000e+00 1.00000000e+00
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement