-
Notifications
You must be signed in to change notification settings - Fork 0
/
K-means_cluster.py
46 lines (37 loc) · 941 Bytes
/
K-means_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
36
37
38
39
40
41
42
43
44
45
46
import random
import sys
import os
import pandas as pd
from pylab import *
from scipy.cluster.vq import *
from numpy import *
from PIL import *
# generate test data
# generate 100 rows and 2 columns
class1 = 1.5 * random.randn(100, 2)
print(class1)
class2 = random.randn(100, 2) + array([5, 5])
# print(class2)
features = vstack((class1, class2))
#print(features[0:1])
#print("wanttttt")
# K-Means clustering
centroids, variance = kmeans(features, 2)
# print(centroids)
#print("varancccc")
# print(variance)
code, distance = vq(features, centroids)
print(vq(features, centroids))
# print(distance)
figure()
ndx = where(code == 0)[0]
print(ndx)
plot1 = plot(features[ndx, 0], features[ndx, 1], '*')
#show(plot1)
ndx = where(code == 1)[0]
plot2 = plot(features[ndx, 0], features[ndx, 1], 'r.')
#show(plot2)
plot3 = plot(centroids[:,0],centroids[:,1],'go')
#show(plot3)
axis('off')
show()