◢◤ ARKANE_
SYS_TIME UTC+0
Work with me
/W14

实验Lab: Find the groups and the outliers

week 14
Dataset
UCI Dry Bean (13,611 rows, CC BY 4.0) and OpenML Mammography (11,183 rows)
Time
3 hours

This week you'll be able to

  • Cluster with k-means, Gaussian mixtures, DBSCAN, and hierarchical clustering, and say which assumptions each makes.
  • Choose K with the elbow method and silhouette, and test whether the clusters are useful.
  • Find anomalies with an isolation forest and report precision and recall where labels exist.

Goal

Cluster data without labels, then use the hidden labels to check how well each method did. Find rare anomalies and measure what you caught.

Data

Dry Bean. Shape measurements from images of seven varieties of dry bean. You'll hide the Class column while clustering.

Mammography. A standard anomaly-detection benchmark: 11,183 cases, of which 260 (2.3%) are the rare class.

import pandas as pd
from sklearn.datasets import fetch_openml

beans = pd.read_csv("https://archive.ics.uci.edu/static/public/602/data.csv")
labels = beans.pop("Class")  # hidden while clustering, used only to check

mammo = fetch_openml(data_id=310, as_frame=True)
X_m, y_m = mammo.data, (mammo.target == "1").astype(int)

Tasks

  1. Scale the bean features. Explain what happens to k-means if you don't.
  2. Choose K with the elbow method and the silhouette score (Procedure 19.1). Do they agree? Do they find 7?
  3. Run k-means, a Gaussian mixture, DBSCAN, and agglomerative clustering (Procedure 19.2). For DBSCAN, choose eps from a k-distance plot.
  4. Score each method two ways: silhouette (no labels) and adjusted Rand index against the hidden labels (Procedure 19.4). Do the two scores rank the methods the same way?
  5. Crosstab your best clustering against the true varieties. Which varieties get merged? Look at their measurements and explain why.
  6. Mammography: fit an isolation forest without using the labels (Procedure 19.3). Set contamination to 0.023.
  7. Using the labels only now, report precision and recall for the flagged cases. Then sweep the anomaly-score threshold and plot precision against recall.

Deliverable

A notebook with the K-selection plots, a table of silhouette and ARI per method, the crosstab, and the anomaly precision-recall plot.

Done when

  • Labels are used only for scoring, never for fitting.
  • Each clustering method gets both scores.
  • The anomaly results report precision and recall, not accuracy.

Stretch

Project the beans to 2D with PCA and with UMAP. Colour by cluster and by true variety. Where do the pictures mislead?

online /teaching/semester-1/labs/14 Newcastle, UK v0.1.0 // rss