forked from thuansjsu/cs271-final
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.py
35 lines (29 loc) · 973 Bytes
/
cluster.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
from sklearn.mixture import GaussianMixture
from sklearn.cluster import KMeans
import numpy as np
import matplotlib.pyplot as plt
dataset = np.loadtxt("result1.txt", dtype=float)
dataset = dataset/1000
X = dataset.reshape(5,1000).T
kmeans = KMeans(2, random_state=0)
sse = {}
# for k in range(1, 10):
# kmeans = KMeans(n_clusters=k, max_iter=1000).fit(X)
# #X["clusters"] = kmeans.labels_
# #print(data["clusters"])
# sse[k] = kmeans.inertia_ # Inertia: Sum of distances of samples to their closest cluster center
# plt.figure()
# plt.plot(list(sse.keys()), list(sse.values()))
# plt.xlabel("Number of cluster")
# plt.ylabel("SSE")
# plt.show()
model = kmeans.fit(X)
labels = model.predict(X)
acc = model.score(X)
print(acc)
plt.scatter(X[:, 0], X[:, 1], c=labels)
plt.show()
# gmm = GaussianMixture(n_components=5,covariance_type='diag').fit(X)
# labels1 = gmm.predict(X)
# plt.scatter(X[:, 0], X[:, 1], c=labels1, s=40, cmap='viridis');
# plt.show()