-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathk_means.py
33 lines (26 loc) · 858 Bytes
/
k_means.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
from sklearn.cluster import KMeans
import sklearn.datasets
import numpy as np
from itertools import groupby
def k_means(data, no_of_clusters):
print("Running k-means")
data = np.array(data);
kmeans = KMeans(no_of_clusters, random_state=0).fit_predict(data)
for i in range(0,150):
print(str(i) + " " + str(kmeans[i]));
l1=kmeans[:50];
l1.sort();
l = [len(list(group)) for key, group in groupby(l1)]
print (l);
max1 = max(l);
l1=kmeans[50:100];
l1.sort();
l = [len(list(group)) for key, group in groupby(l1)]
print (l);
max2 = max(l);
l1=kmeans[100:150];
l1.sort();
l = [len(list(group)) for key, group in groupby(l1)]
print (l);
max3 = max(l);
print("Clustering Accuracy = "+str(((max1+max2+max3)/150*100))+ " % ")