-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagnes.py
More file actions
63 lines (49 loc) · 1.38 KB
/
agnes.py
File metadata and controls
63 lines (49 loc) · 1.38 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import matplotlib.pyplot as plt
import numpy as np
import torch
print(torch.cuda_version)
cluster_Num = 4
color = ['red', 'black', 'blue', 'orange']
C = []
x = []
y = []
data = open('data/dataset.txt')
for line in data.readlines():
x.append(float(line.strip().split('\t')[0]))
y.append(float(line.strip().split('\t')[1]))
for i in range(len(x)):
C.append([i])
def distance(Ci, Cj): # calculate the distance between two clusters
dis = []
for i in Ci:
for j in Cj:
dis.append(np.sqrt((x[i] - x[j]) ** 2 + (y[i] - y[j]) ** 2))
dis = list(set(dis))
return np.mean(dis)
def find_Two_cluster():
temp = []
for i in range(len(C)):
for j in range(i + 1, len(C)):
dis = distance(C[i], C[j])
temp.append([i, j, dis])
temp = sorted(temp, key=lambda x: x[2])
return temp[0][0], temp[0][1]
def agnes():
global C
while len(C) > cluster_Num:
i, j = find_Two_cluster()
merge = C[i] + C[j]
C = [C[t] for t in range(len(C)) if t != i and t != j]
C.append(merge)
for i in range(len(C)):
X = []
Y = []
for j in range(len(C[i])):
X.append(x[C[i][j]])
Y.append(y[C[i][j]])
plt.scatter(X, Y, c=color[i])
plt.legend(['C1', 'C2', 'C3', 'C4'])
plt.title('agnes')
plt.show()
if __name__ == '__main__':
agnes()