K-Means Clustering Calculator

Stephanie Ben-Joseph headshot Stephanie Ben-Joseph

How to Use This K-Means Clustering Calculator

This calculator runs the k-means clustering algorithm on two-dimensional data. You provide a list of points in the plane and choose how many clusters k you want. The tool then returns the coordinates of the cluster centroids and the cluster assignment for each point.

Input format

After you click the button to run k-means, the calculator:

How K-Means Clustering Works

K-means is an unsupervised learning method that partitions data into k clusters. Each cluster is represented by a centroid (a point in the same space as the data). The algorithm tries to place centroids so that points in the same cluster are close to each other and far from points in other clusters, using standard Euclidean distance.

Suppose you have n data points in 2D, written as

p, p, , p , where each point has coordinates p=(xi,yi).

You choose a number of clusters k. The algorithm searches for centroids

c, c, , c

and a partition of the points into sets (clusters) S1,S2,,Sk that minimize the total squared distance from each point to the centroid of its cluster. In symbols, k-means tries to minimize the objective

J= i=1 k pSi |pc| 2

Here |pc| is the usual Euclidean distance between point p and centroid c. In 2D this distance is

|pc| = (xxc)2 + (yyc)2

The centroid of each cluster is simply the average of the points assigned to it:

c= pSi p |Si|

In practice, k-means alternates between assigning each point to its nearest centroid and recomputing centroids as these averages, until the assignments stop changing or the improvement becomes negligible.

Interpreting This Calculator’s Results

When you run the calculator, it typically displays two main outputs:

You can use these results to:

If you try multiple values of k, you will notice that:

Worked Example

Consider this simple dataset of six points:

0, 0
0, 1
1, 0
5, 5
5, 6
6, 5
  

There are two obvious groups: three points near (0,0) and three near (5,5). If you set k = 2 and run the calculator, you should see:

Interpretation:

Comparison: K-Means vs. Other Clustering Approaches

Method Key idea When it works well Limitations
K-means (this calculator) Finds k centroids that minimize squared distances within clusters. Compact, roughly spherical clusters with similar size; numeric 2D data. Sensitive to outliers and scaling; requires choosing k in advance.
Hierarchical clustering Builds a tree of merges or splits between clusters. Exploratory analysis when you want to see structure at multiple levels. Can be slower on large datasets; tree cut choice can be subjective.
Density-based (e.g., DBSCAN) Groups dense regions and marks isolated points as noise. Irregular shapes and clusters of varying size; noise detection. Requires density parameters; may struggle with varying densities.

This calculator is intentionally focused on the classic k-means setting: fixed k, Euclidean distance, and two-dimensional numeric data.

Assumptions and Limitations of This Tool

Keep these assumptions in mind when interpreting the output. For high-stakes decisions or complex datasets, consider complementing this simple calculator with more advanced statistical or machine learning tools.

Enter data points and k.

Embed this calculator

Copy and paste the HTML below to add the K-Means Clustering Calculator – Cluster 2D Points into k Groups Online to your website.