SyncAI.news, a Varaisys broadcasting
Blending Unsupervised Clustering Tasks with Scikit-LLM
IP

Iván Palomares Carrascosa

· 1 min read

EngineeringKDnuggets

Blending Unsupervised Clustering Tasks with Scikit-LLM

Introduction

Traditional approaches for unsupervised text clustering usually produce groups that are defined in a mathematical, rather "anonymous" way. More often than not, we have the extra burden of guessing the underlying themes characterizing each cluster based on manual keyword inspection.

This article proposes an alternative, hands-on approach: aided by Scikit-LLM's summarization tools fueled by large language models (LLMs) alongside local text embeddings, we build a two-stage pipeline that:

  1. Clusters text documents based on deep meaning rather than overlapping terms, as TF-IDF and other classical methods do
  2. Automatically generates rich, human-readable labels for each cluster, aided by a pre-trained LLM for text summarization wrapped by Scikit-LLM

The solution we are about to build is cost-free and easy to run even in a notebook environment.

Building a Two-Stage Topic Discovery Pipeline

This example can be run entirely in a Google Colab notebook. It uses Groq's LLM API to rely fully on free models. A prerequisite is to visit the Groq website, register, and generate your own API key. Once it is generated, carefully copy your API key (it is shown only once) and add it as a new "Secret" in Google Colab — found under the key icon on the left-hand side of Colab. There, paste your copied API key and name the secret GROQ_API_KEY.

After that, we start our notebook by installing Scikit-LLM and sentence-transformers:

!pip install scikit-llm sentence-transformers -q

Now, let's start writing our Python code. The first step is to configure the environment surrounding Scikit-LLM, routing our requests through Groq's API instead of the default OpenAI API. This way, we can leverage Groq's free-tier pre-trained models and avoid per-token costs.

Another important step here is applying some basic "data hygiene" and removing noise in the form of extremely short documents without clear topic semantics — these outlier documents might otherwise hijack our clustering results:

Results:

Original source

This story was published by KDnuggets and written by Iván Palomares Carrascosa. SyncAI.news shows a preview; the complete article is on the publisher's site.

Read the full story on kdnuggets.com

Similar News