1
1
import random
2
-
3
2
from k_means_clustering .data import inputs
4
- from k_means_clustering .utils import KMeans , bottom_up_cluster , \
5
- generate_clusters , get_values
3
+ from k_means_clustering .utils import KMeans , bottom_up_cluster , generate_clusters , get_values
6
4
7
5
if __name__ == '__main__' :
6
+ # Set the random seed for reproducibility
8
7
random .seed (0 )
9
- cluster = KMeans (3 )
10
- cluster .train (inputs = inputs )
8
+
9
+ # Perform k-means clustering with k=3
10
+ kmeans_3 = KMeans (3 )
11
+ kmeans_3 .train (inputs = inputs )
11
12
print ("3-means:" )
12
- print (cluster .means )
13
+ print (kmeans_3 .means )
13
14
print ()
14
15
15
- random . seed ( 0 )
16
- cluster = KMeans (2 )
17
- cluster .train (inputs = inputs )
16
+ # Perform k-means clustering with k=2
17
+ kmeans_2 = KMeans (2 )
18
+ kmeans_2 .train (inputs = inputs )
18
19
print ("2-means:" )
19
- print (cluster .means )
20
+ print (kmeans_2 .means )
20
21
print ()
21
22
22
- # for k in range(1, len(inputs) + 1):
23
- # print(k, squared_clustering_errors(inputs, k))
24
- # print()
25
-
26
- # recolor_image('/home/amogh/Pictures/symantec.png')
27
-
28
- print ("bottom up hierarchical clustering" )
29
-
23
+ # Perform hierarchical clustering
24
+ print ("Bottom-up hierarchical clustering" )
30
25
base_cluster = bottom_up_cluster (inputs )
31
26
print (base_cluster )
32
27
33
- print ()
34
- print ("three clusters, min:" )
28
+ # Generate three clusters with the minimum size
29
+ print ("Three clusters, min:" )
35
30
for cluster in generate_clusters (base_cluster , 3 ):
36
31
print (get_values (cluster ))
37
32
38
- print ()
39
- print ("three clusters, max:" )
33
+ # Generate three clusters with the maximum size
34
+ print ("Three clusters, max:" )
40
35
base_cluster = bottom_up_cluster (inputs , max )
41
36
for cluster in generate_clusters (base_cluster , 3 ):
42
- print (get_values (cluster ))
37
+ print (get_values (cluster ))
0 commit comments