From 7954ee96ef70f7ebd1c07ec45b97dcbfbc520318 Mon Sep 17 00:00:00 2001 From: aditi15053 Date: Mon, 16 Oct 2023 17:56:17 +0530 Subject: [PATCH 1/2] Some Improvements and comments --- k_means_clustering/model.py | 41 ++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/k_means_clustering/model.py b/k_means_clustering/model.py index 2e05b55..61b0128 100644 --- a/k_means_clustering/model.py +++ b/k_means_clustering/model.py @@ -1,42 +1,37 @@ import random - from k_means_clustering.data import inputs -from k_means_clustering.utils import KMeans, bottom_up_cluster, \ - generate_clusters, get_values +from k_means_clustering.utils import KMeans, bottom_up_cluster, generate_clusters, get_values if __name__ == '__main__': + # Set the random seed for reproducibility random.seed(0) - cluster = KMeans(3) - cluster.train(inputs=inputs) + + # Perform k-means clustering with k=3 + kmeans_3 = KMeans(3) + kmeans_3.train(inputs=inputs) print("3-means:") - print(cluster.means) + print(kmeans_3.means) print() - random.seed(0) - cluster = KMeans(2) - cluster.train(inputs=inputs) + # Perform k-means clustering with k=2 + kmeans_2 = KMeans(2) + kmeans_2.train(inputs=inputs) print("2-means:") - print(cluster.means) + print(kmeans_2.means) print() - # for k in range(1, len(inputs) + 1): - # print(k, squared_clustering_errors(inputs, k)) - # print() - - # recolor_image('/home/amogh/Pictures/symantec.png') - - print("bottom up hierarchical clustering") - + # Perform hierarchical clustering + print("Bottom-up hierarchical clustering") base_cluster = bottom_up_cluster(inputs) print(base_cluster) - print() - print("three clusters, min:") + # Generate three clusters with the minimum size + print("Three clusters, min:") for cluster in generate_clusters(base_cluster, 3): print(get_values(cluster)) - print() - print("three clusters, max:") + # Generate three clusters with the maximum size + print("Three clusters, max:") base_cluster = bottom_up_cluster(inputs, max) for cluster in generate_clusters(base_cluster, 3): - print(get_values(cluster)) \ No newline at end of file + print(get_values(cluster)) From 55491f202df5458f9edc801602d97d4f5221d001 Mon Sep 17 00:00:00 2001 From: aditi15053 Date: Mon, 16 Oct 2023 17:57:42 +0530 Subject: [PATCH 2/2] Some improvement and comments --- k_means_clustering/model.py | 1 + 1 file changed, 1 insertion(+) diff --git a/k_means_clustering/model.py b/k_means_clustering/model.py index 61b0128..90212fa 100644 --- a/k_means_clustering/model.py +++ b/k_means_clustering/model.py @@ -35,3 +35,4 @@ base_cluster = bottom_up_cluster(inputs, max) for cluster in generate_clusters(base_cluster, 3): print(get_values(cluster)) +