7 routes42 sections324 units

Unit 4.6 · about 4 h

157 / 324

DBSCAN, agglomerative clustering and cluster validation

Real groups are rarely spheres. Density and merge views find moons and hierarchies that k-means smears, but their knobs decide the answer.

  • explain
  • measure

You can now

  • Explain DBSCAN core, border and noise labels from eps and min_samples and linkage choices in agglomerative clustering.
  • Measure eps sensitivity, linkage disagreement and seed and sample stability on one dataset.
  • State what silhouette and Rand indices can and cannot justify for non-convex clusters.

Covers

  • DBSCAN and agglomerative clustering geometry
  • Cluster stability, usefulness and internal-metric limits

Study, in this order

2 steps

  1. `DBSCAN` contrast and agglomerative example
    Hands-On Machine Learning with Scikit-Learn and PyTorch - dimensionality reduction and unsupervised learning

    Book Chapter 8: DBSCAN and Other Clustering Algorithms (printed pp. 265-269). Notebook 08 cells: DBSCAN: make_moons(1000, noise=0.05) at eps=0.05 (7 clusters, many points labeled -1) versus eps=0.2 (two clusters, no noise), core_sample_indices_, components_, and KNN on the cores with a 0.2 distance cutoff to label new points; Other Clustering Algorithms: AgglomerativeClustering with complete linkage on \(\left[\begin{smallmatrix}0&2&5&8.5\end{smallmatrix}\right]\)

    Depth · Skim the Spectral Clustering bonus code; keep the eps contrast and the linkage example.

  2. Density, linkage and metric limits
    scikit-learn - decomposition, clustering and outlier detection

    2.3.7 DBSCAN: dense definition from eps and min_samples, core, border and outlier rules, the eps-must-be-tuned warning with all-noise and merged-cluster failures; 2.3.6 hierarchical clustering: Ward, complete, average and single linkage with the uneven-size guidance; 2.3.11 metrics: silhouette formula with its convex-cluster bias and the adjusted Rand index correcting for chance

Focus on

  • eps sets the neighborhood radius and min_samples the density floor. On the notebook moons, eps=0.05 breaks the two moons into 7 clusters and labels 7.7 percent as noise, while 0.2 finds the two moons with no noise; always sweep eps.
  • Linkage defines merge distance: single follows chains, complete compacts, Ward prefers equal-variance merges. On \(\left[\begin{smallmatrix}0&2&5&8.5\end{smallmatrix}\right]\) single splits off 8.5 while complete pairs neighbors.
  • DBSCAN labels new points only by extra machinery: the notebook trains KNN on cores. The clusterer itself has no predict.
  • Silhouette rewards dense convex separation and can punish correct density shapes. Pair it with stability and downstream checks.

Reproduce closed-book

Closed-book, hand-run DBSCAN with eps=2.0 and min_samples=2 on \(\left[\begin{smallmatrix}1&1\\1.5&1.5\\2&1\\8&8\\8.5&8.5\\9&8\end{smallmatrix}\right]\): two core groups \(\left[\begin{smallmatrix}0&0&0&1&1&1\end{smallmatrix}\right]\) with no noise. Then move one point to \(\left[\begin{smallmatrix}100&100\end{smallmatrix}\right]\) and show it becomes -1.

Traps

  • Leaving eps at 0.5 on unscaled features. eps lives in feature units; scale first or the radius is meaningless.
  • Calling -1 a detected anomaly set. It means not dense enough under this eps; nearby eps values relabel it.
  • Reporting silhouette alone on moons. On the notebook moons a k-means split scores 0.49 and the true moon labels 0.33; the docs warn that silhouette favors convex clusters.

Practice

Deep-ML 259: Implement DBSCAN Clustering Algorithm

Problem 259: implement dbscan(X, eps, min_samples) with Euclidean distance and -1 noise labels

Test one checks [0 0 0 1 1 1] on two triplets; test two checks the far point becomes -1. Count the point itself toward min_samples, as scikit-learn does; with min_samples=2 test two needs that, or the pair \(\left[\begin{smallmatrix}8&8\end{smallmatrix}\right]\), \(\left[\begin{smallmatrix}8.5&8.5\end{smallmatrix}\right]\) turns into noise. Expand clusters from core points only.

Self-test

  1. On the notebook moons, eps=0.05 gives 7 clusters and 7.7 percent noise, while eps=0.2 gives two clean moons. What went wrong at 0.05, and what does the -1 label mean?

  2. Single linkage gives \(\left[\begin{smallmatrix}0&0&0&1\end{smallmatrix}\right]\) and complete gives \(\left[\begin{smallmatrix}1&1&0&0\end{smallmatrix}\right]\) on \(\left[\begin{smallmatrix}0&2&5&8.5\end{smallmatrix}\right]\). Which is right?

  3. On the notebook moons, KMeans(2) scores silhouette 0.49 and the true moon labels score 0.33. Which clustering is better?

  4. Two DBSCAN runs with close eps values disagree on 5 percent of labels. What do you report?

Completion criterion

Stop when

Your implementation matches both Deep-ML label vectors, your eps sweep on moons shows the noise-to-merged transition, and two linkages disagree on record with reasons.

Mark passed, then open 4.7 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

Also in this section

  • ISLP - Unsupervised Learning
    James, Witten, Hastie, Tibshirani and Taylorrequiredtheory
    Use Chapter 12 and its Python lab for the PCA derivations (12.2, including the low-rank approximation view and proportion of variance explained) and k-means (12.4.1), plus stability and interpretation limits, beside the notebook route.
  • Deep-ML - unsupervised problems
    Deep-MLsupportpractice
    Use 17 K-Means Clustering, 19 PCA, 341 Gaussian Mixture Model with EM, 259 DBSCAN and 367 Isolation Forest as unit checks after the local implementation passes. Their tests were read in review; each card states what they pin, including the 367 path-length convention.
  • scikit-learn - preprocessing data
    scikit-learnreferencereference
    Read 8.3.1 standardization with the sparse with_mean=False rule, 8.3.1.3 scaling data with outliers, and 8.3.4 categorical encoding with handle_unknown, infrequent and sparse_output behavior. Apply after the HOML notebook build works.
  • scikit-learn - pipelines and composite estimators
    scikit-learnreferencereference
    Read 8.1.1 Pipeline chaining with the no-leakage rule and transformer caching, and 8.1.4 ColumnTransformer for heterogeneous columns with remainder and fitted-state behavior. Use it to assemble and serialize the feature-selection pipeline.
  • scikit-learn - imputation of missing values
    scikit-learnreferencereference
    Read 8.4.2 univariate median and most_frequent strategies, 8.4.6 Marking imputed values with MissingIndicator or add_indicator=True for missingness flags, and 8.4.7 estimators that accept NaN directly. Decide impute-only versus impute-plus-indicator per column.
  • UMAP - basic usage and parameters
    McInnes, Healy and the UMAP contributorsreferencereference
    Work the Penguin scaling and fit_transform section and the Digits fit and transform section, then read the parameters page for the n_neighbors local-global tradeoff and min_dist packing. Stay in 2D exploratory views with fixed seeds.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels