Skip to content

Commit

Permalink
Add comments for explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
Haleshot committed Sep 12, 2024
1 parent 893f2c7 commit 24fb38a
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions medium/K-Means_Clustering/K-Means_Clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ def euc_dist(x1, x2):

# Iterate for the given max_iterations
for _ in range(max_iterations):
clusters = [[] for _ in range(k)] # Create empty lists for clusters
clusters = [[] for _ in range(k)] # Create empty lists for clusters


# Assign points to the nearest centroid (initial draft)
for point in points:
distance = [euc_dist(point, final_centroids[i]) for i in range(k)]
Expand All @@ -32,12 +31,14 @@ def euc_dist(x1, x2):

# Update centroids by computing the mean of points in each cluster
for cluster in clusters:
if cluster: # Only update centroids if cluster is not empty
if cluster: # Only update centroids if cluster is not empty
new_centroid = tuple(np.round(np.mean(cluster, axis=0), 4))
new_centroids.append(new_centroid)
else:
new_centroids.append(final_centroids[clusters.index(cluster)]) # Keep the same centroid if no points in the cluster

new_centroids.append(
final_centroids[clusters.index(cluster)]
) # Keep the same centroid if no points in the cluster

# If centroids do not change, break early
if new_centroids == final_centroids:
break
Expand Down Expand Up @@ -69,7 +70,7 @@ def euc_dist(x1, x2):
# centroids = np.round(centroids,4)
# return [tuple(centroid) for centroid in centroids]


# Example usage
points = [(1, 2), (1, 4), (1, 0), (10, 2), (10, 4), (10, 0)]
k = 2
initial_centroids = [(1, 1), (10, 1)]
Expand Down

0 comments on commit 24fb38a

Please sign in to comment.