#k-means #cluster #clustering #means

nightly kmeans

Small and fast library for k-means clustering calculations

1 unstable release

0.2.0 Oct 8, 2020
0.1.0 Jul 27, 2019

#432 in Science

Download history 20/week @ 2023-06-08 42/week @ 2023-06-15 43/week @ 2023-06-22 27/week @ 2023-06-29 27/week @ 2023-07-06 22/week @ 2023-07-13 26/week @ 2023-07-20 33/week @ 2023-07-27 23/week @ 2023-08-03 36/week @ 2023-08-10 60/week @ 2023-08-17 124/week @ 2023-08-24 20/week @ 2023-08-31 44/week @ 2023-09-07 34/week @ 2023-09-14 28/week @ 2023-09-21

128 downloads per month
Used in 2 crates

Apache-2.0

89KB
1K SLoC

kmeans

Current Crates.io Version docs

kmeans is a small and fast library for k-means clustering calculations. Here is a small example, using kmean++ as initialization method and lloyd as k-means variant:

use kmeans::*;

fn main() {
    let (sample_cnt, sample_dims, k, max_iter) = (20000, 200, 4, 100);

    // Generate some random data
    let mut samples = vec![0.0f64;sample_cnt * sample_dims];
    samples.iter_mut().for_each(|v| *v = rand::random());

    // Calculate kmeans, using kmean++ as initialization-method
    let kmean = KMeans::new(samples, sample_cnt, sample_dims);
    let result = kmean.kmeans_lloyd(k, max_iter, KMeans::init_kmeanplusplus, &KMeansConfig::default());

    println!("Centroids: {:?}", result.centroids);
    println!("Cluster-Assignments: {:?}", result.assignments);
    println!("Error: {}", result.distsum);
}

Datastructures

For performance-reasons, all calculations are done on bare vectors, using hand-written SIMD intrinsics from the packed_simd crate. All vectors are stored row-major, so each sample is stored in a consecutive block of memory.

Supported variants / algorithms

  • lloyd (standard kmeans)
  • minibatch

Supported centroid initialization methods

  • KMean++
  • random partition
  • random sample

Dependencies

~3MB
~61K SLoC