Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ML-like

Two small algorithms often associated with machine learning, written with matten to show that a Tensor is enough for recognizable ML-shaped tasks. They use only the default numeric Tensor API, small hard-coded inputs, and deterministic output.

The boundary is deliberate: these are algorithm demonstrations, not an ML framework. There is no training loop abstraction, no model object, no autograd, and no randomness — k, initial centroids, labels, and iteration counts are all fixed and explicit. Both find the nearest point with Tensor::argmin (RFC-038).

Examples

37_kmeans_small.rs

Difficulty: Advanced-small. Clusters six 2-D points into two groups with Lloyd’s algorithm: assign each point to the nearest centroid, then move each centroid to the mean of its points. Deterministic initial centroids make the run reproducible; it converges to the two obvious clusters.

cargo run --example 37_kmeans_small

Source: 37_kmeans_small.rs

38_nearest_neighbor_classification.rs

Difficulty: Beginner. Classifies a query point by the label of its single nearest training point (1-NN) over a labeled [samples, features] data matrix. No training step, no fitted parameters — just a nearest-point search.

cargo run --example 38_nearest_neighbor_classification

Source: 38_nearest_neighbor_classification.rs

What this is not

These are single-file demonstrations of accepted APIs. They do not imply that matten is an ML framework, a clustering/classification library, or a replacement for a dedicated ML toolkit.