Skip to main content

Overview

Membership matrices define how points are assigned to clusters. The library provides two types of membership calculations:
  • Crisp membership: Each point belongs to exactly one cluster (binary assignment)
  • Fuzzy membership: Each point can partially belong to multiple clusters (probabilistic assignment)

getMembershipMatrix

Generates a crisp membership matrix from a distance matrix. Each point is assigned to exactly one cluster based on the nearest centroid.
getMembershipMatrix(distanceMatrix: number[][]): number[][]

Parameters

distanceMatrix
number[][]
required
Matrix of distances between points and centroids, where distanceMatrix[i][j] represents the distance between centroid i and point j.

Returns

membershipMatrix
number[][]
A membership matrix where each element [i][j] is 1 if the j-th point belongs to the i-th centroid, otherwise 0. Returns an empty array if the distance matrix is empty.

Example

import { getDistanceMatrix, getMembershipMatrix } from 'fuzzy-cmeans';

const points = [
  { x: 1, y: 1 },
  { x: 5, y: 5 },
  { x: 2, y: 2 }
];

const centroids = [
  { x: 1.5, y: 1.5 },
  { x: 5, y: 5 }
];

const distanceMatrix = getDistanceMatrix(points, centroids);
const membershipMatrix = getMembershipMatrix(distanceMatrix);
// Returns: [[1, 0, 1],  // Points 0 and 2 belong to cluster 0
//           [0, 1, 0]]  // Point 1 belongs to cluster 1
In crisp clustering, each point belongs to exactly one cluster. The membership value is binary: either 1 (belongs) or 0 (does not belong).

getFuzzyMembershipMatrix

Calculates a fuzzy membership matrix given a distance matrix and a fuzzification parameter. Points can have partial membership in multiple clusters.
getFuzzyMembershipMatrix(distanceMatrix: number[][], fuzzyParameter: number): number[][]

Parameters

distanceMatrix
number[][]
required
Matrix of distances between points and centroids, where distanceMatrix[i][j] represents the distance between centroid i and point j.
fuzzyParameter
number
required
Fuzzification parameter (typically denoted as m). Controls the degree of fuzziness in the clustering. Common values are between 1.5 and 3, with 2 being the most common choice.
  • Values closer to 1 make the clustering more crisp
  • Higher values make the clustering more fuzzy

Returns

membershipMatrix
number[][]
A fuzzy membership matrix where each element [i][j] represents the degree of membership (between 0 and 1) of point j to cluster i. The sum of memberships for each point across all clusters equals 1. Returns an empty array if the distance matrix is empty.

Example

import { getDistanceMatrix, getFuzzyMembershipMatrix } from 'fuzzy-cmeans';

const points = [
  { x: 1, y: 1 },
  { x: 5, y: 5 },
  { x: 3, y: 3 }
];

const centroids = [
  { x: 1.5, y: 1.5 },
  { x: 4.5, y: 4.5 }
];

const distanceMatrix = getDistanceMatrix(points, centroids);
const fuzzyMembershipMatrix = getFuzzyMembershipMatrix(distanceMatrix, 2);
// Returns: [[0.95, 0.05, 0.52],  // Partial memberships to cluster 0
//           [0.05, 0.95, 0.48]]  // Partial memberships to cluster 1
// Note: Each column sums to 1.0
In fuzzy clustering, each point has a membership degree in all clusters, with values between 0 and 1. The membership values for each point across all clusters sum to 1.

Mathematical Formula

The fuzzy membership value is calculated using:
uᵢⱼ = 1 / Σₖ (dᵢⱼ / dₖⱼ)^(2/(m-1))
Where:
  • uᵢⱼ is the membership of point j to cluster i
  • dᵢⱼ is the distance from point j to centroid i
  • m is the fuzzification parameter
  • The sum is over all clusters k

Crisp vs Fuzzy Membership

Crisp Membership

  • Binary assignment: Each point belongs to exactly one cluster
  • Hard boundaries: Clear separation between clusters
  • Use case: When data has well-separated clusters

Fuzzy Membership

  • Probabilistic assignment: Each point can belong to multiple clusters with different degrees
  • Soft boundaries: Gradual transition between clusters
  • Use case: When data has overlapping clusters or unclear boundaries
const crispMembership = getMembershipMatrix(distanceMatrix);
// [[1, 0, 1],
//  [0, 1, 0]]
// Point 0: 100% cluster 0, 0% cluster 1
// Point 1: 0% cluster 0, 100% cluster 1

Build docs developers (and LLMs) love