-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
46 lines (39 loc) · 1.32 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobs
from sklearn.metrics.pairwise import pairwise_distances
from constrained_kmedoids import KMedoids
def plot_graphs(data, clusters):
colors = {0:'b*', 1:'g^',2:'ro',3:'c*', 4:'m^', 5:'yo', 6:'ko', 7:'w*'}
index = 0
for key in clusters.keys():
temp_data = clusters[key]
x = [data[i][0] for i in temp_data]
y = [data[i][1] for i in temp_data]
plt.plot(x, y, colors[index])
index += 1
plt.title('Cluster formations')
plt.show()
medoid_data_points = []
for m in clusters.keys():
medoid_data_points.append(data[m])
x = [i[0] for i in data]
y = [i[1] for i in data]
x_ = [i[0] for i in medoid_data_points]
y_ = [i[1] for i in medoid_data_points]
plt.plot(x, y, 'yo')
plt.plot(x_, y_, 'r*')
plt.title('Mediods are highlighted in red')
plt.show()
def main():
# generate random points
X, _ = make_blobs(n_samples=18, centers=4)
# compute distance matrix
dist = pairwise_distances(X, metric='euclidean')
# k-medoids algorithm
km = KMedoids(distance_matrix=dist, n_clusters=4)
km.run(max_iterations=10, tolerance=0.001)
print(km.clusters)
plot_graphs(X, km.clusters)
if __name__ == '__main__':
main()