Module 6: Embedding Operations

Centroid & Clustering

Centroid

def calculate_centroid(embeddings):
    """Centroid = average of the embeddings"""
    return np.mean(embeddings, axis=0)

# Example: A cluster of similar documents
cluster_embs = [emb1, emb2, emb3, emb4, emb5]
centroid = calculate_centroid(cluster_embs)

# The centroid represents the "central concept" of the cluster

K-Means Clustering

from sklearn.cluster import KMeans

# Embeddings (N × 1536)
embeddings = np.array([...])

# K-means
kmeans = KMeans(n_clusters=5, random_state=42)
labels = kmeans.fit_predict(embeddings)

# Centroids
centroids = kmeans.cluster_centers_  # (5 × 1536)

print(f"Cluster 0: {len(labels[labels==0])} docs")
print(f"Cluster 1: {len(labels[labels==1])} docs")

Applications

Document organization:

# Group 1000 docs into 10 topics
kmeans = KMeans(n_clusters=10)
labels = kmeans.fit_predict(doc_embeddings)

# Each cluster = an automatic topic
for i in range(10):
    cluster_docs = docs[labels == i]
    print(f"Topic {i}: {len(cluster_docs)} documents")

Representative sampling:

# Select 1 doc per cluster (the most representative)
representatives = []
for i in range(n_clusters):
    cluster_embs = embeddings[labels == i]
    centroid = kmeans.cluster_centers_[i]
    
    # The doc closest to the centroid
    dists = [np.linalg.norm(emb - centroid) for emb in cluster_embs]
    closest_idx = np.argmin(dists)
    representatives.append(cluster_docs[closest_idx])

Summary

  • Centroid: Cluster representative
  • K-means: Automatic clustering
  • Use case: Document organization, topic detection

Module 6 - Capsule 06