-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
39 lines (33 loc) · 1 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
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import k_means
from sklearn.datasets.samples_generator import make_blobs
from sklearn.cluster import KMeans
n_samples = 300
centers = [[-10, -10], [0, 0], [3, 5]]
cluster_std = [0.2, 0.1, 0.3]
n_clusters = len(centers)
random_state = 3
X, Y = make_blobs(n_samples=n_samples, n_features=2,
centers=centers, cluster_std=cluster_std,
random_state=random_state)
R, C = X.shape
with open('data.txt', 'w') as fout:
fout.write(f"{R} {C}\n")
for r in range(R):
fout.write(' '.join(f"{X[r][c]}" for c in range(C)) + '\n')
'''
plt.scatter(X[:, 0], X[:, 1], marker='o')
plt.show()
'''
res = KMeans(n_clusters=n_clusters, random_state=9).fit(X)
print(res.cluster_centers_, res.labels_, res.inertia_)
centers = res.cluster_centers_
tot_dis = 0.0
for i, c in enumerate(res.labels_):
x = X[i]
center = centers[c]
diff = x - center
dis = ((diff * diff).sum())
tot_dis += dis
print(tot_dis)