Converter tool

K-Means Clustering on a CSV with Elbow and Silhouette

Group rows by their numeric columns with standardised k-means++, compare k from 2 to 10 using the elbow and silhouette, read each cluster's profile and see the clusters on a two-component map.

One row per customer, product or sample

1. Your data

Or paste the table

K-Means Clustering on a CSV

Operator: Abdulla Al Fahad. How tools are built and checked

Run on 240 customers described by orders per year, average basket, days since the last order and tenure, the page finds three clusters of 100, 70 and 70 rows with a mean silhouette of 0.528, while k = 2 scores 0.434 and k = 4 scores 0.479. This page runs standardised k-means for every k from 2 to 10, shows the elbow and the silhouette side by side, profiles each cluster in the original units and draws the rows on a two-component map. It is not RFM Segmentation, which scores recency, frequency and spend by fixed rules: here the groups come from distances between rows across whichever numeric columns you tick.

Key features

  • k-means++ seeding with 1 to 50 restarts per k, keeping the run with the lowest within-cluster sum of squares
  • Elbow plot and mean silhouette for k = 2 up to a maximum of 10, with the best silhouette highlighted
  • Cluster profiles: size, share, silhouette and each column's mean in original units with its distance from the overall mean in standard deviations
  • A map of the rows on their first two principal components, with the share of variance the map covers
  • Assignments CSV keeping every input column, profile and k-comparison CSVs, and an XLSX whose profile sheet uses COUNTIF and AVERAGEIF on the assignments

How standardised k-means turns numeric columns into clusters, and how the best k is judged

Tick two to 30 numeric columns. Rows with an empty or non-numeric cell in any ticked column are left out and counted, up to 30,000 usable rows are clustered, text and ID columns are ignored, and a ticked column that holds one value in every usable row stops the run because it cannot separate anything. The largest k is capped at the number of distinct rows, since k cannot exceed the number of different points. Standardising (z-scores) is on by default and should stay on unless every column already shares one unit.

How it works

With standardising on, every column is converted to z-scores first, so a column measured in days (recency from 9 to 183) cannot swamp one measured in orders. Clustering and the silhouette then use ordinary Euclidean distance between those standardised rows.

Seeding uses k-means++: the first centre is a random row and each next one is drawn with probability proportional to its squared distance from the nearest centre already chosen, which spreads the starts out. Lloyd's algorithm then alternates assigning rows to the nearest centre and moving centres to the cluster mean until no row changes cluster, with a cap of 200 iterations. A cluster that empties is re-seeded at the row farthest from its centre. Each restart uses a different seed derived from the seed you enter, and the lowest-inertia run is kept, so the same seed always reproduces the same clusters.

The silhouette of a row is (b - a) / max(a, b), where a is its mean distance to its own cluster and b the smallest mean distance to another cluster; a singleton cluster scores 0. The page reports the mean over rows. Beyond 1,500 rows the exact value would need every pairwise distance, so a fixed random subset of 1,500 rows is scored against that subset; the within-cluster sum of squares always uses every row.

Clusters are renumbered from largest to smallest. The map is a projection of the standardised rows onto their two largest principal axes, and the variance between clusters is 1 minus the within-cluster sum of squares over the total, which is 68.0% for k = 3 in the example.

Common use cases

  • Splitting 240 customers into frequent small-basket buyers (24 orders, 31.40), occasional big-basket buyers (6 orders, 113.20) and lapsed customers (183 days since the last order).
  • Grouping 85 stores by twelve operating measures to choose peer sets for benchmarking.
  • Checking whether 1,800 survey respondents scored on seven Likert items fall into three or four attitude groups by comparing the silhouette at k = 3 and k = 4.

How to use this tool

  1. Open a table with at least two numeric columns, or press Load example for the 240 sample customers.
  2. Tick the numeric columns to cluster on and decide whether to standardise.
  3. Press Run k-means and wait for all restarts; Cancel stops the run.
  4. Compare the elbow and silhouette plots, pick a k, and read the cluster profiles and the map.

Example

Excerpt of the example customers (240 rows)

customer,orders_12m,avg_basket,days_since_last_order,tenure_months
C231,3,48.77,197,39
C110,4,117.37,46,34
C111,4,75.62,50,27
...

Mean silhouette by k, and the k = 3 profiles

k=2 0.434 | k=3 0.528 | k=4 0.479 | k=5 0.406 | k=6 0.362 | k=10 0.321
Cluster 1: 100 rows, 24.2 orders, basket 31.40, 9.0 days since order, silhouette 0.611
Cluster 2:  70 rows,  6.0 orders, basket 113.20, 36.9 days, silhouette 0.456
Cluster 3:  70 rows,  2.9 orders, basket 43.20, 183.1 days, silhouette 0.480
Variance between clusters 68.0%; map covers 77.3%

Tips for best results

  • Run the same data with standardising on and off when your columns share a unit. If the clusters change a lot, the scale of one column was deciding them.
  • A best silhouette below about 0.25 is the data saying it has little cluster structure, not that k = 2 is right. Read the profiles before naming any group.
  • Keep the seed fixed when you share results, and change it once to confirm the clusters survive. Real structure does; noise reshuffles.

Common pitfalls

Numeric codes for categories (1, 2, 3 for three regions) are treated as distances, which makes no sense. Leave them out or recode them first.

Extreme rows pull centroids and can claim a cluster of their own. Check the smallest cluster's size, and log or cap heavily skewed columns such as spend before clustering.

Common errors and how to fix them

Only 4 rows have a number in every chosen column
Clustering needs at least 6 complete rows. Untick the column with the most blanks or fill the gaps in the source file.
"region_code" has the same value in every usable row
A constant column carries no information and would divide by a zero standard deviation. Untick it.
This page clusters up to 30,000 rows
Take a random sample of the file, cluster that, and assign the remaining rows by nearest profile in a spreadsheet if you need every row labelled.
No column in this file looks numeric
Currency symbols and thousands commas are accepted, but text categories are not. Convert them to numbers or pick another file.

Frequently asked questions

How should I choose k?

Look for the k with the highest silhouette and for a bend in the elbow where adding a cluster stops reducing the within-cluster sum of squares much. They often agree for well-separated data and disagree for blurry data, in which case pick the k whose profiles you can explain.

Why do different seeds sometimes give different clusters?

k-means finds a local optimum that depends on its starting centres. Restarts reduce this by keeping the best of several runs, but data without clear groups can still have several near-equal solutions.