K-Means Clustering Implementation

Implement the K-means clustering algorithm from scratch.

Task:

Implement the K-means clustering algorithm from scratch using only NumPy. Your implementation should include:

  • A function to initialize centroids randomly
  • A function to assign data points to the nearest centroid
  • A function to update centroids based on assigned points
  • A function to calculate inertia (sum of squared distances)
  • The main K-means algorithm with convergence criteria

Use the provided synthetic dataset to test your implementation and visualize the clustering results.

Code Editor

Run your code to see the output here.

Output

Run your code to see the output here.