← Back

Centroid Calculation in K-Means

2 min read

In K-Means clustering, a centroid represents the multi-dimensional geometric center of a cluster. It acts as the minimal-distance point (the representative average) for all data points assigned to that specific cluster.

1. The Mathematical Formulation

Let S\mathcal{S} be a subset of nn high-dimensional vectors assigned to cluster kk:

S={x1,x2,…,xn}\mathcal{S} = \{\mathbf{x}_1, \mathbf{x}_2, \dots, \mathbf{x}_n\}

Where each vector xi\mathbf{x}_i lies in a DD-dimensional real vector space RD\mathbb{R}^D:

xi=[xi,1xi,2⋮xi,D]∈RD\mathbf{x}_i = \begin{bmatrix} x_{i,1} \\ x_{i,2} \\ \vdots \\ x_{i,D} \end{bmatrix} \in \mathbb{R}^D

The centroid ck\mathbf{c}_k is defined as the coordinate-wise arithmetic mean of these vectors:

ck=1n∑i=1nxi\mathbf{c}_k = \frac{1}{n} \sum_{i=1}^{n} \mathbf{x}_i

For each individual dimension d∈{1,2,…,D}d \in \{1, 2, \dots, D\}, the coordinate of the centroid ck,dc_{k,d} is calculated as:

ck,d=1n∑i=1nxi,dc_{k,d} = \frac{1}{n} \sum_{i=1}^{n} x_{i,d}

2. A 2D Concrete Example

Let's calculate the centroid for n=4n = 4 points in a 2D space (D=2D = 2):

x1=[2,4]Tx2=[3,8]Tx3=[5,2]Tx4=[6,6]T\begin{aligned} \mathbf{x}_1 &= [2, 4]^T \\ \mathbf{x}_2 &= [3, 8]^T \\ \mathbf{x}_3 &= [5, 2]^T \\ \mathbf{x}_4 &= [6, 6]^T \end{aligned}

Step 1: Calculate the Mean of Dimension 1 (xx-coordinate)

c1=2+3+5+64=164=4.0c_1 = \frac{2 + 3 + 5 + 6}{4} = \frac{16}{4} = 4.0

Step 2: Calculate the Mean of Dimension 2 (yy-coordinate)

c2=4+8+2+64=204=5.0c_2 = \frac{4 + 8 + 2 + 6}{4} = \frac{20}{4} = 5.0

Step 3: Define the Centroid Vector

The new centroid is:

c=[4.05.0]\mathbf{c} = \begin{bmatrix} 4.0 \\ 5.0 \end{bmatrix}

3. Interactive Implementation (Python & TS)

Since this is MDX, you can easily import this into your React dashboard or use these code snippets directly in your pipeline.

Python (NumPy)

python
import numpy as np
 
def calculate_centroid(points: np.ndarray) -> np.ndarray:
    """
    Calculates the centroid of a set of D-dimensional points.
    :param points: A NumPy array of shape (n, D)
    :return: A NumPy array of shape (D,) representing the centroid
    """
    return np.mean(points, axis=0)
 
# Example execution matching our math
points = np.array([[2, 4], [3, 8], [5, 2], [6, 6]])
centroid = calculate_centroid(points)
print(f"Centroid: {centroid}") # Output: [4. 5.]

TypeScript

typescript
interface Point {
  coordinates: number[];
}
 
function calculateCentroid(points: Point[]): number[] {
  if (points.length === 0) return [];
 
  const numDimensions = points[0].coordinates.length;
  const numPoints = points.length;
  const centroid = new Array(numDimensions).fill(0);
 
  for (let d = 0; d < numDimensions; d++) {
    let sum = 0;
    for (let i = 0; i < numPoints; i++) {
      sum += points[i].coordinates[d];
    }
    centroid[d] = sum / numPoints;
  }
 
  return centroid;
}
 
// Example Execution
const pointsList = [
  { coordinates: [2, 4] },
  { coordinates: [3, 8] },
  { coordinates: [5, 2] },
  { coordinates: [6, 6] },
];
console.log(calculateCentroid(pointsList)); // Output: [4, 5]