Módulo 6: Embedding Operations
Centroid & Clustering
Centroid
def calculate_centroid(embeddings):
"""Centroid = promedio de embeddings"""
return np.mean(embeddings, axis=0)
# Ejemplo: Cluster de documentos similares
cluster_embs = [emb1, emb2, emb3, emb4, emb5]
centroid = calculate_centroid(cluster_embs)
# Centroid representa el "concepto central" del 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:
# Agrupar 1000 docs en 10 topics
kmeans = KMeans(n_clusters=10)
labels = kmeans.fit_predict(doc_embeddings)
# Cada cluster = topic automático
for i in range(10):
cluster_docs = docs[labels == i]
print(f"Topic {i}: {len(cluster_docs)} documents")
Representative sampling:
# Seleccionar 1 doc por cluster (más representativo)
representatives = []
for i in range(n_clusters):
cluster_embs = embeddings[labels == i]
centroid = kmeans.cluster_centers_[i]
# Doc más cercano al centroid
dists = [np.linalg.norm(emb - centroid) for emb in cluster_embs]
closest_idx = np.argmin(dists)
representatives.append(cluster_docs[closest_idx])
Resumen
- ✅ Centroid: Representante de cluster
- ✅ K-means: Clustering automático
- ✅ Use case: Document organization, topic detection
Módulo 6 - Cápsula 06