ML Algorithms Playground
Seven interactive demos: naive Bayes, ridge and lasso, DBSCAN, hierarchical clustering, gradient boosting, RNN memory and self-attention.
About this playground
These extend the other playgrounds with the remaining big ideas: probabilistic classification, regularisation, other kinds of clustering, boosting, and the basics of sequence models. Everything runs in your browser.
Naive Bayes: classifying with probabilities
Naive Bayes uses Bayes’ rule. It models each class with a Gaussian bell curve per feature (dashed ellipses), multiplies the feature likelihoods as if they were independent (the “naive” part), and multiplies by the class prior. The colour shows the resulting probability of Class B.
Click the map to add a point of the selected class.
- Training accuracy
- —
- Class priors
- —
Try this
- On Blobs, naive Bayes works very well: each class is a simple bell curve.
- Move the Class B share to 10%: the prior pushes the boundary towards Class B, so points of the rarer class are more easily labelled Class A.
- Try Diagonal: the two features are strongly correlated inside each class, which breaks the independence assumption, and accuracy drops. On XOR, one Gaussian per class cannot work at all.
Regularisation: ridge and lasso
A high-degree polynomial can wiggle through every training point (overfitting). Regularisation adds a penalty on the size of the coefficients. Ridge shrinks them all smoothly; lasso pushes many exactly to zero, so it also selects terms. The bars show each coefficient.
With Method = None the penalty slider is switched off.
- Training error (MSE)
- —
- Test error (MSE)
- —
- Non-zero coefficients
- —
Try this
- With Method = None and degree 12 the curve goes wild and the test error is enormous. Now choose Ridge: the curve calms down and the test error collapses.
- Choose Lasso and raise λ: more bars become exactly zero (grey) and the model keeps only a few terms.
- Push λ too high with either method: the curve flattens and both errors rise. That is underfitting.
DBSCAN: clusters of any shape, plus noise
DBSCAN groups points that are packed closely together. A point with at least minPts neighbours within radius ε is a core point, and clusters grow by chaining core points. Points that belong to no cluster are marked as noise (grey ✕). Unlike k-means, you don't choose the number of clusters.
Hover to see the ε circle. Click to add a point.
- Clusters found
- —
- Noise points
- —
Try this
- On Moons, find an ε (about 0.10) that gives exactly 2 clusters. k-means would cut each moon in half instead.
- Make ε too small: everything becomes noise or tiny clusters. Make it too large: all clusters merge into one.
- On Blobs + noise, the scattered points are marked as noise (grey ✕) while the three dense blobs become clusters. Raise minPts to make the rule stricter.
Hierarchical clustering and the dendrogram
Agglomerative clustering starts with every point alone and repeatedly merges the two closest clusters. The dendrogram records each merge at its distance. Cutting the tree at a height gives the number of clusters you want. The linkage rule decides how the distance between clusters is measured.
Drag the slider to move the cut line; the clusters on the map update.
- Clusters
- —
- Cut height
- —
Try this
- Set the number of clusters to the number of blobs you can see: the cut line sits in the biggest vertical gap of the dendrogram.
- Switch between Single, Complete and Average linkage and compare the trees. Single linkage can chain points into long strings.
- Increase the number of clusters one at a time and watch the cut line drop and the clusters split.
Gradient boosting: learning from the previous mistakes
Gradient boosting builds many small trees one after another. Each new tree learns to predict the errors (residuals) left by the trees so far, and its prediction is added with a small learning rate. Watch the curve improve round by round, and eventually start to overfit.
- Training error (MSE)
- —
- Test error (MSE)
- —
Try this
- Move the rounds slider up from 0: with depth 1 (stumps) the staircase curve gradually bends to follow the sine wave.
- Set the learning rate to 1 and the depth to 3, then use 150 rounds: the model chases the noise and the test error rises.
- Lower the learning rate to 0.1 and compare: smaller steps need more rounds but usually generalise better.
Recurrent networks: memory and vanishing gradients through time
A recurrent neural network reads a sequence one step at a time, carrying a hidden state h forward: h_t = f(w_in·x_t + w_h·h_(t−1)). Training sends gradients backward through all the steps, multiplying by w_h each time, so the influence of early inputs can fade away or explode.
Click the input bars to switch each step on or off.
- Final state h₂₀
- —
- Gradient ∂h₂₀/∂x₁ (influence of the first input)
- —
Try this
- With the default tanh unit the influence of the first input after 20 steps is tiny: the network cannot remember it. This is the vanishing gradient problem in time.
- Switch to Linear and set w_h = 1.0: the influence stays near 1, so memory is preserved. Try 0.8 and 1.1 to watch it fade and explode.
- LSTM and GRU networks add gates that keep this multiplier close to 1, so they remember much longer.
Self-attention: who should I listen to?
In self-attention every word has a vector (here just 2 numbers, so you can see it). The query word compares itself with all words using dot products, a softmax turns the scores into weights, and the weighted average of the vectors (the gold star) becomes its new, context-aware representation. Drag the words to change their vectors.
Drag the words on the map. Lower temperature = sharper attention.
- Strongest attention
- —
- Attention to itself
- —
Try this
- Pick “cat” as the query: it attends mostly to itself and to “dog”, the word with the most similar vector.
- Lower the temperature to 0.2: attention becomes almost winner-takes-all. Raise it to 3: the weights spread out almost evenly.
- Drag “mat” right next to “cat”: attention to it jumps. Real transformers learn the vectors so that related words end up with high dot products.
Looking for the other playgrounds?
The foundation demos and the deep learning demos are in separate playgrounds.