Topics / 🌲 Classical ML II / 🧩 Level 98: Classical ML II: Clustering & Anomalies

Python exercise · about 5 minutes · free

DBSCAN: Clusters From Density

K-means needs k and likes round blobs. DBSCAN finds any shape, and marks lonely points as noise.

DBSCAN groups points that are densely packed. A point is a core point if at least min_pts points (itself included) lie within distance eps. Clusters grow by chaining from core point to core point; border points join a nearby cluster; everything else is noise (label -1). It needs no k, finds odd shapes and detects outliers, but struggles when density varies a lot.

Prices, versions and service names last checked 2026-11-19. They change, so confirm on the official pages before you rely on them.

Flow: count neighbours within eps → core point? (>= min_pts) → grow the cluster (chain through core points) → the rest is noise (label -1)count neighbours within epscore point?>= min_ptsgrow the clusterchain through core pointsthe rest is noiselabel -1
Your quest: Write dbscan(points, eps, min_pts) for 1D numbers (distance abs, neighbour if distance <= eps + 1e-9). Return a label per point: clusters numbered from 0 in the order they are discovered (scan points left to right in list order), noise -1.
Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
HintPrecompute neighbours. Loop over unvisited points; if not core mark noise, else start a cluster and expand with a queue, adding the neighbours of every core point.
Output appears here.

Open it in the full course → Free with Google: saves your progress and unlocks 627 lessons.

In this level you'll learn:
More in Level 98: Classical ML II: Clustering & Anomalies

Share on LinkedIn