Skip to content

kmeans

kmeans<T>(n, k, ops, options?): KmeansCluster<T>[]

Defined in: kmeans.ts:96

Generic k-means clustering.

Partitions n data points into at most k clusters by iterating: 1. Assignment — each point is assigned to its nearest centroid. 2. Update — each centroid is recomputed from its assigned points via ops.computeCentroid.

Initial centroids are chosen via k-means++ (D² weighting), which spreads seeds across the data distribution and reduces the risk of degenerate local minima.

By supplying a circular-mean implementation of computeCentroid, this function also serves as circular k-means for angular data (e.g. OKLch hue).

T

number

Number of data points (indexed 0 … n-1).

number

Desired number of clusters; clamped to min(k, n).

KmeansOps<T>

Problem-specific operations.

KmeansOptions

Optional algorithm tuning.

KmeansCluster<T>[]

One entry per non-empty cluster, with its final centroid and assigned indices.