k-means
Overview
k-means partitions trials into k clusters by minimizing the Within-Cluster Sum of Squares (WCSS). Each trial belongs to the nearest centroid.
Objective
μ_k is the centroid of cluster C_k.
Note on the app's wcss field. The value the app reports as wcss is the
mean of squared distances to the nearest centroid ( ), not
the summed WCSS defined above. This does not affect the Elbow method: since N is
the same across all k tried, using the mean instead of the sum only rescales
every by a common constant and does not shift the position of the
second-difference maximum (see elbow.md).
Algorithm
- Initialize — select k starting centroids using the chosen strategy
- Assign — assign each point to the nearest centroid
- Update — recompute each centroid using an m_k-means-style update (see below), which folds in the previous centroid rather than taking a plain mean
- Converge — stop when the Euclidean distance between the old and new centroid arrays is below tolerance 1e-5 (max 300 iterations)
Update Step
Tunny Dashboard does not average each cluster's points directly. It uses an m_k-means-style update that folds the previous centroid in as an extra point:
Empty cluster: when , the formula reduces to — the previous centroid is kept automatically, with no special-cased branch needed.
Initialization Strategies
k-means++ (Default)
Selects centroids far from existing ones using D²-weighted probability:
D(x_i) = distance from x_i to the nearest existing centroid.
The initial centroids are chosen using a seeded random-number generator, with the seed derived from the number of trials (n) and the requested number of clusters (k). Same data and k always produce the same result.
Theoretical guarantee: expected WCSS ≤ 8(ln k + 2) × WCSS_opt.
Deterministic
Uses the same D²-proportional centroid-selection algorithm as k-means++, but with a fixed seed (42), so the result is fully reproducible on every run.
Used internally by the Elbow method for auto-k estimation.
| Aspect | k-means++ | Deterministic |
|---|---|---|
| Selection | D²-proportional sampling | D²-proportional sampling (fixed seed) |
| Randomness | Seed derived from n, k | Seed fixed at 42 |
| Reproducibility | Same data+k → same result | Always identical (seed=42) |
| Theory | O(log k) approximation | Same guarantee (identical algorithm) |
| Local optima | Reduced by best-of-10 (see below) | Reduced by best-of-10; always the same run since the seed is fixed |
Both strategies run the same D²-proportional selection and the same best-of-10 re-run (below); the only difference is the seed. The "Theory" and "Local optima" rows are therefore not a meaningful basis for choosing one over the other — pick k-means++ for a fresh seed derived from the data, or Deterministic when the caller (e.g. the Elbow method) needs a fixed, repeatable seed.
Multiple Runs (best-of-10)
For a given seed, Tunny Dashboard runs the full initialize → assign → update → converge procedure 10 independent times and keeps the result with the lowest inertia (WCSS). This reduces — but does not eliminate — the risk of returning a poor local optimum, for both k-means++ and Deterministic alike.
Parameters Used by Tunny Dashboard
| Parameter | Value |
|---|---|
| Max iterations | 300 |
| Runs per seed | 10 (best-of-10 by inertia) |
| Distance metric | Squared Euclidean |
| Empty cluster | Keep previous centroid (see Update Step) |
Strengths and Limitations
Strengths
- Fast and interpretable
- WCSS quantifies solution quality
Limitations
- Requires k upfront (use Elbow method for auto-selection)
- Assumes convex / spherical cluster shapes
- Sensitive to outliers (centroid pulled toward them)
- May converge to local optima
Input Space
| Setting | Features used | Best for |
|---|---|---|
| Objective Space | Objective values only | Cluster by performance similarity |
| Variable Space | Parameter values only | Cluster by design space patterns |
| Combined | Both | Joint structure analysis |
References
- Lloyd, S. P. (1982). Least squares quantization in PCM. IEEE Transactions on Information Theory, 28(2), 129–137. https://doi.org/10.1109/TIT.1982.1056489
- Arthur, D., & Vassilvitskii, S. (2007). k-means++: The Advantages of Careful Seeding. SODA 2007, 1027–1035. https://dl.acm.org/doi/10.5555/1283383.1283494