Skip to main content

CMeans

Executes one iteration of the C-means clustering algorithm. This function calculates the distance matrix, membership matrix, new centroids, cost values, and total cost function for a single iteration.
export const CMeans = (points: Point[], centroids: Point[])

Parameters

points
Point[]
required
Array of points to cluster. Each point has x and y coordinates.
centroids
Point[]
required
Array of initial centroids. Each centroid has x and y coordinates.

Returns

Returns an object containing the results of one C-means iteration:
distanceMatrix
number[][]
Matrix of distances where each element [i][j] is the distance between the i-th centroid and the j-th point.
membershipMatrix
number[][]
Membership matrix where each element [i][j] is 1 if the j-th point belongs to the i-th centroid, otherwise 0.
newCentroids
Point[]
New array of centroids calculated as the mean of all points belonging to each centroid.
costValues
number[]
Array of cost values for each centroid.
costFunction
number
Total cost as the sum of all individual cost values.

Example

import { CMeans } from './utils/CMeans';

const points = [
  { x: 1, y: 2 },
  { x: 3, y: 4 },
  { x: 5, y: 6 },
  { x: 7, y: 8 }
];

const centroids = [
  { x: 2, y: 3 },
  { x: 6, y: 7 }
];

const result = CMeans(points, centroids);

console.log(result.newCentroids);
console.log(result.costFunction);

Helper Functions

getDistanceMatrix

Calculates the distance matrix between each point and each centroid.
export const getDistanceMatrix = (points: Point[], centroids: Point[])
points
Point[]
required
Array of points.
centroids
Point[]
required
Array of centroids.
Returns: A matrix of distances where each element [i][j] is the distance between the i-th centroid and the j-th point.

getMembershipMatrix

Generates the membership matrix from the distance matrix.
export const getMembershipMatrix = (distanceMatrix: number[][])
distanceMatrix
number[][]
required
Matrix of distances between points and centroids.
Returns: A membership matrix where each element [i][j] is 1 if the j-th point belongs to the i-th centroid, otherwise 0.

getNewCentroids

Calculates new centroids based on the membership matrix.
export const getNewCentroids = (points: Point[], membershipMatrix: number[][])
points
Point[]
required
Array of points.
membershipMatrix
number[][]
required
Membership matrix.
Returns: A new array of centroids calculated as the mean of all points belonging to each centroid.

getCostValues

Calculates the cost values for each centroid based on the membership and distance matrices.
export const getCostValues = (membershipMatrix: number[][], distanceMatrix: number[][])
membershipMatrix
number[][]
required
Membership matrix.
distanceMatrix
number[][]
required
Distance matrix.
Returns: An array of cost values for each centroid.

getCostFunction

Calculates the total cost from an array of individual cost values.
export const getCostFunction = (costValues: number[])
costValues
number[]
required
Array of individual cost values.
Returns: The total cost as the sum of all individual costs.

euclidianDistance

Calculates the Euclidean distance between two points.
export const euclidianDistance = (pointA: Point, pointB: Point)
pointA
Point
required
First point.
pointB
Point
required
Second point.
Returns: The Euclidean distance between the two points.

zeroMatrix

Creates a zero matrix with the same dimensions as the input matrix.
export function zeroMatrix(matrix: number[][])
matrix
number[][]
required
Input matrix.
Returns: A matrix of the same dimensions with all elements set to 0.

generateRandomPoints

Generates an array of random points.
export function generateRandomPoints(n: number)
n
number
required
Number of points to generate.
Returns: An array of n points with random coordinates between -100 and 100.

Example

import { generateRandomPoints } from './utils/CMeans';

const randomPoints = generateRandomPoints(10);
console.log(randomPoints);
// Output: [{ x: -45, y: 67 }, { x: 23, y: -89 }, ...]

Build docs developers (and LLMs) love