kmeans/src/lib.rs
2018-02-16 17:21:05 +01:00

32 lines
667 B
Rust

pub mod kmeans;
pub mod kmeansdata;
pub mod cluster;
pub mod test;
pub use kmeans::Kmeans;
pub use kmeansdata::KmeansData;
pub use cluster::{Cluster, Clusterable};
pub enum Error {
IterationsLimitExceeded,
}
pub fn kmeans<T: Clusterable>(centroids: Vec<T>, data: Vec<T>, max_iterations: usize)
-> Result<(Kmeans<T>, usize), Error> {
let mut kmeans = Kmeans::new(centroids, vec![data]);
for nb_iterations in 0..max_iterations {
let (new_kmeans, stable) = kmeans.next_iteration();
kmeans = new_kmeans;
if stable {
return Ok((kmeans, nb_iterations));
}
}
Err(Error::IterationsLimitExceeded)
}