scikit-learn clustering text documents using DBSCAN
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
DBSCAN can cluster text documents, but it behaves differently from the more common K-means approach because it groups points by density and labels sparse points as noise. In text problems, that means the success of DBSCAN depends heavily on how you vectorize the documents and how you choose the distance metric and eps threshold.
Vectorize the Documents First
DBSCAN works on numeric feature vectors, so text must first be converted into a vector space. A common starting point is TF-IDF.
The result is a sparse matrix where each row represents one document.
Apply DBSCAN
For text, cosine distance is often more meaningful than Euclidean distance because document direction matters more than absolute magnitude.
The labels work like this:
- non-negative integers are cluster IDs
- '
-1means noise'
That noise label is one of DBSCAN's major differences from algorithms that force every document into some cluster.
Why DBSCAN Is Tricky for Text
Text vectors are usually high-dimensional and sparse. In that kind of space:
- many documents are far apart
- density estimates become unstable
- '
epscan be difficult to tune'
So DBSCAN can work, but it is more sensitive than people expect. You often need to experiment with:
- stop-word handling
- n-grams
- dimensionality reduction
- '
eps' - '
min_samples'
A Small End-to-End Example
You may see the fruit documents cluster together while technical documents fall into a different cluster or become noise, depending on the exact parameters.
When Dimensionality Reduction Helps
Sometimes it helps to reduce the TF-IDF space before clustering. Truncated SVD is a common choice for sparse text data:
Reducing the space can make distances more stable, though it also changes the geometry of the problem. It is a tuning tool, not a guaranteed improvement.
Common Pitfalls
The most common mistake is using DBSCAN with default parameters and expecting it to work well on high-dimensional text immediately. eps in particular is highly problem-specific.
Another issue is ignoring the distance metric. Euclidean distance on raw sparse TF-IDF vectors is often a poor default for document similarity.
A third pitfall is misreading noise points. A label of -1 does not necessarily mean the document is bad; it means DBSCAN did not find enough nearby neighbors under the current settings.
Finally, if your real goal is "partition all documents into a small fixed number of topics," K-means or topic modeling may be a better fit than DBSCAN.
Summary
- To use DBSCAN on text, first convert documents into vectors, often with TF-IDF.
- Cosine distance is commonly more appropriate than Euclidean distance for text similarity.
- DBSCAN can find clusters and mark outliers as noise.
- Parameter tuning is critical because text vectors are sparse and high-dimensional.
- Consider dimensionality reduction or alternative clustering methods if DBSCAN is unstable.
Related reading
- Scikit-learn confusion matrix
- scikit-learn cross validation custom splits for time series data
- Scikit-Learn Decision Tree Probability of prediction being a or b?
- scikit-learn filling missing values by random sampling
- Scikit-learn, get accuracy scores for each class
- Scikit-learn GridSearch giving ValueError multiclass format is not supported error
- Scikit-learn How to obtain True Positive, True Negative, False Positive and False Negative
- Scikit-learn How to obtain True Positive, True Negative, False Positive and False Negative
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.