Unsupervised Machine Learning

Unsupervised learning finds hidden patterns or intrinsic structures in input data. Unlike supervised learning, there are no labeled outputs. Common tasks include clustering and dimensionality reduction.

The main families of unsupervised learning are:

  • Clustering: grouping similar data points together (K-Means).
  • Association: finding rules about which items tend to occur together.
  • Dimensionality reduction: compressing many features into fewer ones while keeping the important structure (PCA).

The K-Means demo loads a tiny ONNX model trained on real Iris petal measurements (see scripts/export_tutorial_onnx.py).

K-Means Clustering

K-Means groups data into 'k' non-overlapping subgroups (clusters) distinct from each other. It tries to make the intra-cluster data points as similar as possible while keeping the clusters as different as possible.

The algorithm assigns each point to its nearest centroid, then moves each centroid to the average of its points. It converges when the centroids stop moving. Each iteration has two steps.

1. Assignment step

Measure the distance from each data point to every centroid and assign the point to the nearest one. The usual choice is Euclidean distance between a point \(x\) and a centroid \(c\):

$$d(x, c) = \sqrt{\sum_{i=1}^{n} (x_i - c_i)^2}$$

Here \(x_i\) and \(c_i\) are the feature values of the point and the centroid.

2. Update step

Recompute each centroid as the mean of the \(N\) points assigned to it:

$$c = \frac{1}{N} \sum_{j=1}^{N} x_j$$

Repeat both steps until the centroids stabilise. The result is a set of clusters in which every point belongs to its nearest centroid.

Python Implementation

from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)
labels = kmeans.predict(X)

Live Demo: Iris Sort

ONNX

Sorts Iris flowers into clusters based on petal dimensions.

K-Means Step-by-Step

Watch K-Means converge step by step. Initialize centroids, then step through assignment and update phases. Choose a preset dataset or draw your own points by clicking on the canvas.

K:
Iteration
0
Status
Ready
Inertia
—

Principal Component Analysis (PCA)

PCA finds the directions of maximum variance in high-dimensional data and projects it onto a lower-dimensional subspace — preserving as much information as possible. It's used for visualisation, noise reduction, and speeding up downstream models.

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)   # PCA needs normalised data

pca = PCA(n_components=2)
X_2d = pca.fit_transform(X_scaled)

print(pca.explained_variance_ratio_)  # % variance per component

Live Demo — Iris 4D → 2D Projection

Enter all four Iris measurements and see where they land in 2D principal component space.

Hidden Markov Models

A Hidden Markov Model (HMM) is a statistical model in which the system being modeled is assumed to be a Markov process with unobserved (hidden) states.

In other words, it is a probabilistic model for describing a random process. What you observe at each step does not directly reveal the underlying state. Instead, each hidden state produces observations according to a probability distribution, and the model works backwards from those observations to reason about the states.