clump
Clustering algorithms for dense f32 vectors in Rust. No unsafe code.
Algorithms
| Algorithm | Kind | Discovers k | Noise handling | Input |
|---|---|---|---|---|
| K-means | Centroid | No (k required) | None | &[Vec<f32>] |
| Mini-Batch K-means | Centroid (streaming) | No (k required) | None | StreamingClustering trait |
| DBSCAN | Density | Yes | Labels noise (NOISE sentinel) |
&[Vec<f32>] |
| HDBSCAN | Density (hierarchical) | Yes | Labels noise | &[Vec<f32>] |
| DenStream | Density (streaming) | Yes | Decays outliers | StreamingClustering trait |
| EVoC | Hierarchical | Yes | Near-duplicate detection | &[Vec<f32>] |
| COP-Kmeans | Constrained centroid | No (k required) | None | &[Vec<f32>] + constraints |
| Correlation Clustering | Graph-based | Yes | None | SignedEdge list |
Quickstart
[]
= "0.4.0"
use ;
let data = vec!;
// K-means: returns labels (default: squared Euclidean)
let labels = new.with_seed.fit_predict.unwrap;
assert_eq!;
assert_ne!;
// DBSCAN: discovers clusters from density (default: Euclidean)
let labels = new.fit_predict.unwrap;
Kmeans::fit returns KmeansFit with centroids, which supports predict on new points. Dbscan::fit_predict assigns noise points to clump::NOISE; use fit_predict_with_noise for Option labels.
Streaming clustering
use ;
let mut mbk = new.with_seed;
mbk.partial_fit.unwrap;
mbk.partial_fit.unwrap;
let labels = mbk.predict.unwrap;
let mut ds = new;
ds.partial_fit.unwrap;
let labels = ds.predict.unwrap;
Constrained clustering
use ;
let constraints = vec!;
let labels = new
.with_constraints
.with_seed
.fit_predict
.unwrap;
Correlation clustering
use ;
let edges = vec!;
let labels = new.fit_predict_from_edges.unwrap;
Also see edges_from_distances to build signed edges from a distance matrix.
Distance metrics
All algorithms are generic over DistanceMetric. Built-in metrics:
| Metric | Formula |
|---|---|
SquaredEuclidean |
sum((a_i - b_i)^2) |
Euclidean |
sqrt(sum((a_i - b_i)^2)) |
CosineDistance |
1 - cos_sim(a, b) |
InnerProductDistance |
-dot(a, b) |
CompositeDistance |
Weighted sum of metrics |
Use with_metric on any algorithm to swap the metric:
use ;
let labels = with_metric
.with_seed
.fit_predict
.unwrap;
Custom metrics: implement DistanceMetric (one method: fn distance(&self, a: &[f32], b: &[f32]) -> f32).
Features
| Feature | Default | Effect |
|---|---|---|
parallel |
off | Enables Rayon parallelism for k-means and batch operations |
License
MIT OR Apache-2.0