Here's a TypeScript implementation of the k-medoids algorithm for data points that are sets of generic elements. This implementation uses the Euclidean distance as the distance metric between data points, but you can replace it with any other distance metric that suits your needs.
typescript
type DataPoint<T> = T[];
interface Cluster<T> {
medoid: DataPoint<T>;
points: DataPoint<T>[];
}
function euclideanDistance<T>(a: DataPoint<T>, b: DataPoint<T>, accessor: (x: T) => number): number {
if (a.length !== b.length) {
throw new Error("Data points must have the same dimensions");
}
let sum = 0;
for (let i = 0; i < a.length; i++) {
sum += Math.pow(accessor(a[i]) - accessor(b[i]), 2);
}
return Math.sqrt(sum);
}
function totalDistance<T>(cluster: Cluster<T>, accessor: (x: T) => number): number {
return cluster.points.reduce((sum, point) => sum + euclideanDistance(cluster.medoid, point, accessor), 0);
}
function initializeMedoids<T>(data: DataPoint<T>[], k: number): DataPoint<T>[] {
const medoids: DataPoint<T>[] = [];
for (let i = 0; i < k; i++) {
medoids.push(data[Math.floor(Math.random() * data.length)]);
}
return medoids;
}
function findClosestMedoidIndex<T>(point: DataPoint<T>, medoids: DataPoint<T>[], accessor: (x: T) => number): number {
let closestIndex = 0;
let closestDistance = Infinity;
for (let i = 0; i < medoids.length; i++) {
const distance = euclideanDistance(point, medoids[i], accessor);
if (distance < closestDistance) {
closestDistance = distance;
closestIndex = i;
}
}
return closestIndex;
}
function kMedoids<T>(data: DataPoint<T>[], k: number, accessor: (x: T) => number): Cluster<T>[] {
const medoids = initializeMedoids(data, k);
let clusters: Cluster<T>[] = [];
// Assign data points to their closest medoids
for (const point of data) {
const medoidIndex = findClosestMedoidIndex(point, medoids, accessor);
if (!clusters[medoidIndex]) {
clusters[medoidIndex] = { medoid: medoids[medoidIndex], points: [] };
}
clusters[medoidIndex].points.push(point);
}
// Main k-medoids loop
let converged = false;
while (!converged) {
converged = true;
for (let i = 0; i < clusters.length; i++) {
const currentMedoid = clusters[i].medoid;
let bestNonMedoid = currentMedoid;
let bestTotalDistance = totalDistance(clusters[i], accessor);
for (const nonMedoid of clusters[i].points) {
const newCluster: Cluster<T> = { medoid: nonMedoid, points: [] };
const oldCluster: Cluster<T> = { medoid: currentMedoid, points: [] };
// Reassign points to new medoid or old medoid
for (const point of data) {
if (findClosestMedoidIndex(point, [currentMedoid, nonMedoid], accessor) === 0) {
oldCluster.points.push(point);
} else {
newCluster.points.push(point);
}
}
const newTotalDistance = totalDistance(newCluster, accessor);
if (newTotalDistance < bestTotalDistance) {
bestNonMedoid = nonMedoid;
bestTotalDistance = newTotalDistance;
converged = false;
}
}
if (bestNonMedoid !== currentMedoid) {
medoids[i] = bestNonMedoid;
clusters[i] = { medoid: bestNonMedoid, points: [] };
// Reassign points to their closest medoids
for (const point of data) {
const medoidIndex = findClosestMedoidIndex(point, medoids, accessor);
clusters[medoidIndex].points.push(point);
}
}
}
}
return clusters;
}
You can use the kMedoids function by passing in your data points, the number of clusters (k), and a function to access the numeric value of each generic element in the data points. For example:
This will output the resulting clusters with their medoids and assigned data points.
“Implement k-medoids algorithm in TypeScript for data points that are sets of generic….” Conversation with mistral-large-2402, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850errrzpjcm3szxzmdvhc9