Skip to content

Commit 7954ee9

Browse files
committed
Some Improvements and comments
1 parent a6fbb3d commit 7954ee9

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

k_means_clustering/model.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,37 @@
11
import random
2-
32
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
64

75
if __name__ == '__main__':
6+
# Set the random seed for reproducibility
87
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)
1112
print("3-means:")
12-
print(cluster.means)
13+
print(kmeans_3.means)
1314
print()
1415

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)
1819
print("2-means:")
19-
print(cluster.means)
20+
print(kmeans_2.means)
2021
print()
2122

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")
3025
base_cluster = bottom_up_cluster(inputs)
3126
print(base_cluster)
3227

33-
print()
34-
print("three clusters, min:")
28+
# Generate three clusters with the minimum size
29+
print("Three clusters, min:")
3530
for cluster in generate_clusters(base_cluster, 3):
3631
print(get_values(cluster))
3732

38-
print()
39-
print("three clusters, max:")
33+
# Generate three clusters with the maximum size
34+
print("Three clusters, max:")
4035
base_cluster = bottom_up_cluster(inputs, max)
4136
for cluster in generate_clusters(base_cluster, 3):
42-
print(get_values(cluster))
37+
print(get_values(cluster))

0 commit comments

Comments
 (0)