# Process the data into a 2D array, omitting the header row data, labels = [], [] for row in rawdata: labels.append(row[1]) data.append([int(i) for i in row[1:]]) #print data
# Now run very basic MDS # Documentation here: http://scikit-learn.org/dev/modules/generated/sklearn.manifold.MDS.html#sklearn.manifold.MDS mds = manifold.MDS(n_components=2, dissimilarity="precomputed") pos = mds.fit_transform(data)
# distance matrix D = pairwise_distances(pos, metric='euclidean')
# split into c clusters M, C = kmedoidss.kMedoids(D, 3)
print ('Data awal : ') for index, point_idx in enumerate(pos, 1): print(index, point_idx)
print ('n medoids:' ) for point_idx in M: print('{} index ke - {} '.format (pos[point_idx], point_idx+1))
print('') print('clustering result:') for label in C: for point_idx in C[label]: print('cluster- {}:{} index- {}'.format(label, pos[point_idx], point_idx+1))
kmedoidss.py
import numpy as np import random
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 xrange(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
how to visualize the cluster result as a graph with different node color based on its cluster?
You must be logged in to post. Please login or register an account.