Here's an implementation of the k-medoids algorithm in TypeScript for data points that are sets of generic elements:
typescript
function kMedoids<T>(data: Set<T>[], k: number, maxIterations: number = 100): Set<T>[] {
const n = data.length;
const medoids: Set<T>[] = [];
const clusters: Set<T>[][] = Array(k).fill(null).map(() => []);
// Initialize medoids randomly
const indices = new Set<number>();
while (indices.size < k) {
const index = Math.floor(Math.random() * n);
if (!indices.has(index)) {
indices.add(index);
medoids.push(data[index]);
}
}
let iteration = 0;
while (iteration < maxIterations) {
// Assign each data point to the nearest medoid
for (let i = 0; i < n; i++) {
let minDistance = Infinity;
let nearestMedoidIndex = -1;
for (let j = 0; j < k; j++) {
const distance = jaccardDistance(data[i], medoids[j]);
if (distance < minDistance) {
minDistance = distance;
nearestMedoidIndex = j;
}
}
clusters[nearestMedoidIndex].push(data[i]);
}
// Update medoids
let medoidsChanged = false;
for (let i = 0; i < k; i++) {
let minTotalDistance = Infinity;
let bestMedoid: Set<T> | null = null;
for (const point of clusters[i]) {
let totalDistance = 0;
for (const otherPoint of clusters[i]) {
totalDistance += jaccardDistance(point, otherPoint);
}
if (totalDistance < minTotalDistance) {
minTotalDistance = totalDistance;
bestMedoid = point;
}
}
if (!setsEqual(bestMedoid!, medoids[i])) {
medoids[i] = bestMedoid!;
medoidsChanged = true;
}
}
if (!medoidsChanged) {
break;
}
// Reset clusters
clusters.forEach((cluster) => cluster.length = 0);
iteration++;
}
return medoids;
}
function jaccardDistance<T>(set1: Set<T>, set2: Set<T>): number {
const intersection = new Set([...set1].filter((item) => set2.has(item)));
const union = new Set([...set1, ...set2]);
return 1 - intersection.size / union.size;
}
function setsEqual<T>(set1: Set<T>, set2: Set<T>): boolean {
if (set1.size !== set2.size) {
return false;
}
for (const item of set1) {
if (!set2.has(item)) {
return false;
}
}
return true;
}
In this implementation:
The kMedoids function takes an array of sets (data) representing the data points, the desired number of clusters (k), and an optional maximum number of iterations (maxIterations).
The function initializes the medoids randomly by selecting k unique indices from the data points.
It enters a loop that iterates until the maximum number of iterations is reached or the medoids converge.
In each iteration, it assigns each data point to the nearest medoid based on the Jaccard distance. The Jaccard distance is calculated using the jaccardDistance function, which measures the dissimilarity between two sets.
After assigning the data points to clusters, it updates the medoids by finding the point in each cluster that minimizes the total distance to all other points in the same cluster.
If the medoids haven't changed, the algorithm terminates. Otherwise, it resets the clusters and proceeds to the next iteration.
Finally, the function returns the final medoids.
The jaccardDistance function calculates the Jaccard distance between two sets, which is defined as 1 minus the size of the intersection divided by the size of the union of the sets.
The setsEqual function checks if two sets are equal by comparing their sizes and elements.
Note that this implementation assumes that the data points are represented as sets of generic elements. You can modify the distance function and equality check based on your specific requirements.
Cite this conversation
“Implement k-medoids algorithm in TypeScript for data points that are sets of generic….” Conversation with claude-3-opus-20240229, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850errrhd7wsvw9v8v39rp5