-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkMeans.py
More file actions
71 lines (56 loc) · 1.68 KB
/
Copy pathkMeans.py
File metadata and controls
71 lines (56 loc) · 1.68 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
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 1 17:39:47 2021
@author: maxvondanwitz
"""
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import KMeans
# Load data
testData = np.load('normalizedData.npy')
# Let's see what we got
plt.scatter(testData[:,0], testData[:,1])
plt.xlabel('x')
plt.ylabel('y')
ax = plt.gca()
ax.set(xlim=(-2.5, 4), ylim=(-2.5, 4))
ax.set_aspect('equal','box')
plt.savefig('clusteredData0.pdf')
plt.show()
# Define two 'visually pleasing' -- otherwise bad -- initial guesses
centers = np.array([(-2,3),(-1,2)])
# The KMeans implementation performs computation of new means and reassignment of data
# points in one call. To visualize both steps, we generate two plots from each
# KMeans call.
oldlabels=None
j = 1
for i in range(10):
k_means = KMeans(n_clusters=2, max_iter=1, init=centers, n_init=1, algorithm="full").fit(testData)
labels = k_means.labels_
centers = k_means.cluster_centers_
plt.plot(centers[:,0],centers[:,1],'kx', markersize=15)
plt.scatter(testData[:,0], testData[:,1], c=oldlabels)
plt.xlabel('x')
plt.ylabel('y')
ax = plt.gca()
ax.set(xlim=(-2.5, 4), ylim=(-2.5, 4))
ax.set_aspect('equal','box')
name='clusturedData'+str(j)+'.pdf'
plt.savefig(name)
plt.show()
j = j + 1
plt.plot(centers[:,0],centers[:,1],'kx', markersize=15)
plt.scatter(testData[:,0], testData[:,1], c=labels)
plt.xlabel('x')
plt.ylabel('y')
ax = plt.gca()
ax.set(xlim=(-2.5, 4), ylim=(-2.5, 4))
ax.set_aspect('equal','box')
name='clusturedData'+str(j)+'.pdf'
plt.savefig(name)
plt.show()
j = j + 1
oldlabels = labels
# Save converged results
np.save('clusterLabels.npy', labels)