Skip to content
CS Fundamentals

K-Means Clustering Visualizer

Watch k-means clustering work step by step. Add points, pick k, and see the centres move until every point settles into a cluster.

  • Free
  • No sign-up
  • Runs in your browser

K-Means Clustering Visualizer

runs on your device

Click the chart to add points, or start with random data. Then press Step to watch each iteration.

    How k-means works

    1. Choose k, the number of clusters you want.
    2. Place k starting centres. This tool uses k-means++, which spreads the first centres out so results are more stable.
    3. Assign every point to its nearest centre.
    4. Update each centre to the average position of the points assigned to it.
    5. Repeat steps 3 and 4 until no point changes cluster. The algorithm has converged.

    What is inertia?

    Inertia is the sum of the squared distances from each point to its centre. It drops with every iteration. Lower inertia means tighter clusters, but it always falls as k grows, so compare runs with the same k.

    How to choose k

    Run the tool with k from 2 to 6 and note the final inertia each time. The "elbow", where adding another cluster stops reducing inertia much, is usually a good choice.

    Code

    For a complete C++ implementation with example input, see the related tutorial below.

    Go deeper

    CS Fundamentals

    C++ Program / Code of K Means Clustering with Example

    Get code of K Means Clustering with Example in C++ language. This is very simple code with example. Copy this code from here and paste into any compiler and run code. #in…