Skip to main content

11.7 ML clustering

Source: Ideas from Computational Physics (Landau, Páez, Bordeianu), §11.6. Rewritten in original wording — no KmeansCluster.py / Perceptron.py / Pandas listing dumps. Continues 11.6b. Next: 11.7b.

Grouping data by shared features reveals structure, outliers, and (for nets) useful training signals. Labeled groups ⇒ supervised learning; no labels ⇒ unsupervised. Clustering is computationally hard in general (NP-hard): expect heuristics, not guaranteed global optima in poly-time.

kk-means and linear classifiers​

Pick kk clusters (centroids = means that minimize squared distance to members). Initialize, then iterate assign → update until centroids barely move; sklearn’s KMeans Loss is within-cluster sum of squares (inertia). Supervised linear / perceptron models score wTx+bw^{T}x+b and step weights with a learning rate. Walk assign, centroid update, linear score, and the weight step — pick each matching equation among lookalikes.

Figure 11.9 — three mass clusters and centroids.

Particle-mass demo​

Eighteen PDG-style masses (MeV/c2c^2; ν\nu treated as 00 for the toy): 00, 0.5110.511, 105.65105.65, 105.658105.658, 134.98134.98, 139.57139.57, 139.57139.57, 547.862547.862, 497.611497.611, 493.677493.677, 938.272938.272, 939.565939.565, 1115.6831115.683, 1180.371180.37, 1197.4491197.449, 1314.861314.86, 1321.711321.71, 1672.451672.45.

With k=3k=3 you typically see a low-mass clump (leptons / light mesons), a medium band (heavier mesons ± nucleons depending on init), and a baryon / heavy clump. Exact membership can shift with initialization — another reason to try several seeds.

Pandas for tables​

Hard-coding arrays fails at scale. Pandas reads whitespace / CSV tables into dataframes (columns = named series). Drop unused name columns, feed the mass column into kk-means. (Sandbox labs keep a fixed Python list; use Pandas locally.)

Perceptron / SGD boundaries​

Fire if ϕ(∑wjxj)>θ\phi(\sum w_j x_j)>\theta. Sklearn’s Perceptron / SGDClassifier scale features (zero mean, unit variance), split train/test, and report accuracy. Boundaries are hyperplanes in feature space. A decaying η(t)=1/(α(t0+t))\eta(t)=1/(\alpha(t_0+t)) shrinks steps late in training (picked above).

Figure 11.10 — linear / SGD class regions (schematic).

SGD clustering / classification​

Same particle table, supervised on (mass, type): shuffle each epoch to avoid cycles, descend the Loss stochastically. Regions resemble the perceptron’s but need not match exactly — randomness and loss details matter.

Takeaway​

MethodLabels?Idea
kk-meansNoNearest centroid; update means
PerceptronYesLinear threshold; weight updates
SGD classifierYesStochastic Loss descent; hyperplanes

Next: pure-Python assign / update / standardize / η(t)\eta(t) locks.

Quiz

ML clustering

1 / 4

Unsupervised clustering means…

Pick an answer to see if you are right.

Next: 11.7b Clustering labs.