What makes two rows similar?
Open the retail locations preset and imagine choosing stores for a merchandising trial. A busy store with small baskets might benefit from faster checkout. A quieter store with large baskets might benefit from personal consultations. Grouping by revenue alone could put both stores in the same segment. Tracking foot traffic and basket value separately gives each location a position on a map. K-Means looks for sets of nearby positions and represents each set with one typical position, its centroid. The same idea works when a position takes four columns to describe and you can no longer draw the map directly.
Start with one observation per row and choose a common measurement window. For accounts, a week of activity and a month of spending can work together if every row uses those same definitions. Mixing annual and monthly invoices in one spend column creates a divide that can look like a customer segment. Exclude numeric identifiers before fitting. Two customers who registered a minute apart can have consecutive account numbers and entirely different purchasing habits. Record the input order so you can restore those identifiers after exporting the assignments.
Read inertia as the cost of representing rows by their centers
Pick a row and subtract its assigned centroid from it, column by column. Square each difference and sum the squares. That row contributes the result to the within-cluster sum of squares, also called WCSS or inertia. Adding the contributions from all rows gives the objective K-Means tries to minimize. For a fixed assignment, the column means minimize this cost. For fixed centers, assigning each row to its nearest center minimizes its contribution. Lloyd’s algorithm alternates these two operations until its stopping rule takes effect.
With one center, every account takes the portfolio average as its representative. Adding a second center can remove a large error if enterprise accounts spend much more than the others. Later centers may distinguish only small variations among accounts on the same plan. The best attainable inertia falls or stays level as centers are added. Individual fits can settle at local optima, so the calculator tries ten initializations for each count and keeps the fit with the lowest inertia. Compare costs on the same rows with the same scaling. Doubling the dataset by duplicating every observation doubles the sum even if every center stays put.
Choose a cluster count with three different questions
The elbow curve answers how much error another center removes. Read from left to right and look for a steep descent followed by smaller gains. In the retail example, compare the step from three to four centers with the step from four to five. Inspect the profiles at both settings. A new center that separates busy stores from quiet ones at a similar basket value may justify a different staffing plan. A fifth center that divides nearly identical stores may add operational overhead for very little reduction in error.
Silhouette measures the experience of individual samples. For each sample, compare its average distance to other members of its cluster with its lowest average distance to members of any other cluster. Subtract the within-group distance from the neighboring-group distance and divide by the larger of the two. The resulting score ranges from minus one to one. A value near zero places a row near a boundary under this measure. The chart annotates each fit with the mean score. A large, well-separated group can dominate that mean, so also inspect the sizes and characteristics of smaller groups before adopting the recommendation.
The Davies–Bouldin index gives each occupied cluster a turn. For a pair of groups, it compares their combined internal scatter with the distance between their centers. Each group contributes its worst such ratio to the final average. Lower values indicate compact groups with relatively distant centers. If the silhouette score looks favorable while Davies–Bouldin worsens after a split, inspect the two resulting profiles. A small pair of close groups can have little influence on a sample-weighted average while each still contributes a whole term to the cluster-weighted index.
Choose the units before interpreting the distances
In the SaaS preset, a difference of one hundred dollars contributes ten thousand squared dollars to raw inertia. A difference of three active days contributes nine squared days. Even changing dollars to cents would dramatically alter the relative influence of those two features. Standardization subtracts each column mean and divides by its population standard deviation. Now a move of one standard deviation makes the same contribution on either axis. A constant column becomes zero for every row and contributes nothing to the fitted distances.
Scaling deserves a domain decision. If two columns already measure distances in meters along perpendicular axes, the original geometry can have a physical interpretation worth retaining. Conversely, standardizing every column does little to fix duplication. Monthly spend and monthly spend including a fixed sales tax encode the same behavior twice. Including both doubles that behavior’s influence even after scaling. Try removing one and check which accounts change groups. The profile table always reports original units, which lets you describe a change in terms such as seats per account or weekly active days.
Interpret the scatter alongside the full feature profiles
For two columns, each point sits at the original pair of measurements. A cross marks its group’s center. With one column, the horizontal axis carries the measurement and all points share a baseline. Three or more columns require a projection. The calculator fits principal component analysis to the matrix used for clustering and projects both samples and fitted centers through those same two component directions. The first direction captures the greatest variance and the second captures the greatest remaining variance along a perpendicular direction.
Two accounts can appear almost on top of each other while differing substantially along a discarded direction. Their colors can therefore differ even when they appear closer to the same cross on screen. Check the original column means to find the measurements behind that separation. Conversely, a visibly isolated point may have pulled a centroid toward itself because squaring a large residual amplifies its cost. Review the source row for unit errors and decide whether it represents a repeatable group or one exceptional observation. Both the assignments and separation scores use all fitted features.
Recognize when a different cluster model fits the problem
Nearest-center assignment splits space with straight boundaries. A ring around a compact core has a meaningful separation in density, yet several centers placed around the ring can slice it into wedges. DBSCAN can follow chains of dense neighbors and mark isolated observations as noise when a useful neighborhood radius exists. HDBSCAN explores density structure across neighborhood scales and can help when a single radius merges one pair of groups while breaking another into fragments. Inspect the noise assignments when using either approach, especially if unusual observations are central to your question.
For overlapping elliptical populations, a Gaussian mixture model can estimate covariance within each group and return membership probabilities. Those probabilities can support review of ambiguous cases. For large collections of dense vectors where a fixed number of nearest centers still meets the objective, E-Means offers moving-average center updates during a pass. Compare reconstruction error on held-out vectors and measure runtime on your own workload. Changing the update algorithm addresses computational cost while keeping the decision about a meaningful distance measure in your hands.
Reproduce the result in JavaScript or Python
The code tabs carry the current rows, selected count, random seed, restart count, iteration limit, and tolerance into a standalone fit. The JavaScript example uses the browser’s scaling parameters with @kanaries/ml. The Python example computes population scaling with scikit-learn when the toggle is enabled. Compare the resulting inertia first, then match centers by their coordinates. Label zero simply identifies a position in a particular fit’s center array. Another run can call the same segment cluster two and still give identical assignments after relabeling.
Different implementations can draw different initial centers from the same integer seed and apply different convergence tests. If the solutions differ, check the input order, feature order, and scaling before comparing individual labels. For a repeatable report, keep the clustered CSV together with the selected code and record the package version you ran. A scatter image preserves the view that informed your decision, while the CSV preserves each row’s membership for downstream analysis.