Finding structure in athlete performance

Implementing K-means and examining what four clusters can—and cannot—tell us.

Statistics
Unsupervised learning
R

The assignment asked whether measurements of strength, endurance, agility and speed could be used to group athletes by their performance profiles.

I implemented K-means in R, applied it to measurements from 131 athletes, and compared solutions containing one to six clusters. I compared within-cluster sums of squares with simplified silhouette scores and interpreted the resulting profiles.

From measurements to clusters

Each row of the matrix \(X\) represents an athlete and each column a measured performance dimension. K-means represents a cluster by its centroid, then alternates between assigning observations to their nearest centroid and recalculating those centroids.

The following notation describes the calculation implemented in my code. For athlete \(i\) and cluster \(k\), the Euclidean distance is

\[ d_{ik}=\left\|\mathbf{x}_i-\boldsymbol{\mu}_k\right\|_2. \]

The assignment step chooses

\[ z_i=\underset{k}{\operatorname{argmin}}\;d_{ik}, \]

and the update step replaces each non-empty cluster’s centroid with the mean of its members:

\[ \boldsymbol{\mu}_k= \frac{1}{n_k}\sum_{i:z_i=k}\mathbf{x}_i. \]

The within-cluster sum of squares measures the resulting compactness:

\[ \mathrm{SSQ}=\sum_{i=1}^{N} \left\|\mathbf{x}_i-\boldsymbol{\mu}_{z_i}\right\|_2^2. \]

Implementing the iteration

I initialised the centroids by sampling \(K\) distinct observations. For every iteration, I calculated the full \(N\times K\) distance matrix, selected the nearest centroid for each athlete, and updated the cluster means.

An empty cluster needs special treatment because its mean is undefined. My implementation replaces an empty cluster’s centroid with the observation furthest from its nearest current centroid. That gives the unused centre a location poorly represented by the current solution.

set.seed(2026)

k_means = function(X, K, iter) {
  N = dim(X)[1]
  d = dim(X)[2]
  Mus = X[sample(1:N, K, replace = FALSE), , drop = FALSE]
  ones = matrix(1, N, 1)
  dists = matrix(0, N, K)

  for (i in 1:iter) {
    for (k in 1:K) {
      dists[, k] = sqrt(rowSums((X - ones %*% Mus[k, ])^2))
    }
    assigned_labels = apply(dists, 1, which.min)

    for (k in 1:K) {
      if (any(assigned_labels == k)) {
        wh = which(assigned_labels == k)
        nk = length(wh)
        Mus[k, ] = 1 / nk * colSums(X[wh, , drop = FALSE])
      } else {
        centroid_dist = apply(dists, 1, min)
        wh_worst = which.max(centroid_dist)
        Mus[k, ] = X[wh_worst, , drop = FALSE]
      }
    }
  }

  for (k in 1:K) {
    dists[, k] = sqrt(rowSums((X - ones %*% Mus[k, ])^2))
  }
  assigned_labels = apply(dists, 1, which.min)
  SSQ = sum((dists[cbind(1:N, assigned_labels)])^2)
  return(list(D = dists, SSQ = SSQ, z = assigned_labels))
}

The final distance calculation matters. The last centroid update changes the locations of the centres, so I recomputed the distances and assignments before calculating SSQ. The returned object contains the distance matrix, the objective value and the cluster labels, making it possible to inspect the solution rather than retaining only a single score.

Measuring separation

Compact clusters are not necessarily well separated. Alongside SSQ, I calculated the simplified silhouette coefficient used in the assignment.

For each athlete, let \(r_{i1}\) be the distance to the nearest centroid and \(r_{i2}\) the distance to the second-nearest centroid. The implemented coefficient is

\[ s_i=\frac{r_{i2}-r_{i1}}{r_{i2}}. \]

A value close to one indicates that the nearest centroid is much closer than the alternative. A value close to zero indicates a less clear assignment. This is a centroid-based, simplified coefficient, rather than the conventional silhouette calculated from average distances between observations.

I retained both the overall mean and the means within individual clusters:

\[ \bar{s}=\frac{1}{N}\sum_i s_i, \qquad \bar{s}_k=\frac{1}{n_k}\sum_{i:z_i=k}s_i. \]

SSC = function(res_k_means) {
  D = res_k_means$D
  D_sorted = apply(D, 1, sort)
  r_1 = D_sorted[1, ]
  r_2 = D_sorted[2, ]
  s_value = (r_2 - r_1) / r_2
  z = res_k_means$z
  K = ncol(D)
  s_bar_k = rep(NA, K)

  for (k in 1:K) {
    wh = which(z == k)
    s_bar_k[k] = mean(s_value[wh])
  }
  s_bar_overall = mean(s_value)
  return(list(s = s_value, s_bar_k = s_bar_k,
              s_bar_overall = s_bar_overall))
}

There is no second-nearest centroid when \(K=1\), so the comparison leaves its simplified silhouette coefficient undefined rather than assigning it an artificial score.

Choosing the number of groups

I evaluated \(K=1,\ldots,6\), using 20 iterations for each fit. I used the elbow assessment of SSQ alongside the mean simplified silhouette coefficient.

Within-cluster sum of squares for fits with one to six clusters.
Figure 1: Within-cluster sum of squares for fits with one to six clusters.
Mean simplified silhouette coefficient, peaking at approximately 0.70 for four clusters.
Figure 2: Mean simplified silhouette coefficient, peaking at approximately 0.70 for four clusters.

I selected four clusters by considering the SSQ plot alongside the mean simplified silhouette coefficient, which peaked at approximately 0.70 for four clusters.

For the selected solution, the reported mean simplified silhouette coefficients were:

Cluster Mean simplified silhouette coefficient
1 0.755
2 0.69
3 0.75
4 0.60

All four groups had positive average separation by this measure. Cluster 4 was less distinctly separated than the other three, an important qualification when interpreting its label.

Interpreting the athlete profiles

The report describes the groups by their relative performance patterns:

  • Cluster 1: stronger agility, with weaker strength, endurance and speed.
  • Cluster 2: stronger strength and endurance, with weaker agility and speed.
  • Cluster 3: particularly strong endurance, with weaker performance in the other dimensions.
  • Cluster 4: relative strength, with weaker performance in the remaining dimensions.

I proposed four training programmes based on these profiles, each focused on the weaker dimensions of its group. The programmes were not tested.

What the investigation established

I built the assignment and update steps myself, handled empty clusters, and used two complementary summaries to examine the result. This made the relationship between the code and the interpretation visible: SSQ described compactness, while the simplified silhouette described how decisively an athlete belonged to the nearest centre.

Originally completed as part of STA3047S · Introduction to Machine Learning, University of Cape Town, 2026.