liveBook · Manning

Clustering

As explained in AI-Powered Search

Clustering is an unsupervised learning technique that groups items according to similarity.

Clustering is an unsupervised learning technique that groups items by similarity. In search, it turns embedding vectors for products or documents into data-driven categories that can guide personalization and relevance decisions. section 9.4.2 section 1.1

A practical pipeline embeds items, fits a clustering algorithm, assigns each item a label, and inspects representative terms to check whether the groups form coherent categories. PCA can reduce cluster-center embeddings for a two-dimensional diagnostic plot, but the visualization is for inspection rather than clustering itself. section 9.4.2

Dynamic product categories

A catalog of nearly 50,000 products is grouped into 100 KMeans clusters. Inspecting top words reveals coherent areas such as kitchen appliances, music, video, and data storage. A query such as “microwave” maps to the microwave cluster; comparing nearest cluster centers can also return related kitchen-product clusters. section 9.4.2

Listing 9.13 Generating dynamic categories from clustered products
def get_clusters(data, algorithm, args):
  return algorithm(**args).fit(data)

def assign_clusters(labels, product_names):
  clusters = defaultdict(lambda:[], {})
  for i in range(len(labels)):
    clusters[labels[i]].append(product_names[i])
  return clusters

args = {"n_clusters": 100, "n_init": 10, "random_state": 0} #1
algo = get_clusters(product_embeddings, cluster.KMeans, args)  #1
labels = algo.predict(product_embeddings)
clusters = assign_clusters(labels, product_names)  #2

Shows the compact Python pattern for fitting KMeans, predicting labels, and collecting product names into dynamic categories. section 9.4.2

Listing 9.15 Comparing techniques for mapping queries to clusters
import sentence_transformers, heapq

def get_top_labels_centers(query, centers, n=2):  #1
  query_embedding = transformer.encode([query], convert_to_tensor=False)
  similarities = sentence_transformers.util.cos_sim(
                            query_embedding, centers)
  sim = similarities.tolist()[0]
  return [sim.index(i) for i in heapq.nlargest(n, sim)]

def get_query_cluster(query):  #2
  query_embedding = transformer.encode([query], convert_to_tensor=False)
  return algo.predict(query_embedding)

def get_cluster_description(cluser_num):
  return "_".join(top_words(clusters[cluser_num], 5))

query = "microwave"
kmeans_predict = get_query_cluster(query)[0]  #3
print("K-means Predicted Cluster:")
print(f"    {kmeans_predict} ({get_cluster_description(kmeans_predict)})")

closest_sim = get_top_labels_centers(query, centers, 1)[0] #4
print(f"\nCosine Predicted Cluster:")
print(f"    {closest_sim} ({get_cluster_description(closest_sim)})")

knn_cosine_similarity = get_top_labels_centers(query, centers, 5)  #5
print(f"\nKNN Cosine Predicted Clusters: {knn_cosine_similarity}")
for n in knn_cosine_similarity:
  print(f"    {n} ({get_cluster_description(n)})")

Compares assigning a query through KMeans prediction, similarity to cluster centers, and nearest-neighbor cosine similarity. section 9.4.2

How AI-Powered Search covers it

How deep it goes
Explains an embedding-to-label workflow, inspection with top words and PCA, and query assignment, but not clustering theory or optimization internals.
Examples
Uses product catalogs, top cluster words, PCA plots, microwave queries, and related kitchen-product clusters.
What it assumes
Assumes embeddings and basic search concepts while introducing clustering as a way to generate categories dynamically.
What you can do after
Build it from scratch with Python by clustering embeddings, labeling items, inspecting groups, and mapping queries to categories.
How it connects
Connects embeddings, personalization, semantic search, PCA, KMeans, cosine similarity, and collaborative filtering.

Also explained in

Compare how 18 books explain clustering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Search. The section links point to the parts of the book it draws on.