Skip to content

Some Improvements and comments #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions k_means_clustering/model.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
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))
print(get_values(cluster))