MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / kmeans

Function kmeans

nodedb-vector/src/multivec/plaid.rs:165–186  ·  view source on GitHub ↗

Run Lloyd's K-means with k-means++ initialisation. Empty clusters are re-seeded each iteration so the result always has exactly `k` distinct centroids covering the input space.

(
    vectors: &[Vec<f32>],
    num_centroids: usize,
    iters: usize,
    seed: u64,
    dim: usize,
)

Source from the content-addressed store, hash-verified

163/// re-seeded each iteration so the result always has exactly `k` distinct
164/// centroids covering the input space.
165fn kmeans(
166 vectors: &[Vec<f32>],
167 num_centroids: usize,
168 iters: usize,
169 seed: u64,
170 dim: usize,
171) -> Vec<Vec<f32>> {
172 if vectors.is_empty() || num_centroids == 0 {
173 return Vec::new();
174 }
175
176 let k = num_centroids.min(vectors.len());
177 let mut centroids = kmeans_plus_plus_init(vectors, k, seed);
178
179 for _ in 0..iters {
180 let assignments = assign(vectors, &centroids);
181 let new_centroids = recompute(vectors, &assignments, k, dim, &centroids);
182 centroids = new_centroids;
183 }
184
185 centroids
186}
187
188// ---------------------------------------------------------------------------
189// PlaidPruner

Callers 1

trainMethod · 0.70

Calls 5

kmeans_plus_plus_initFunction · 0.85
assignFunction · 0.85
recomputeFunction · 0.85
is_emptyMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected