pub struct KMeans { /* private fields */ }Expand description
K-Means clustering algorithm.
Uses Lloyd’s algorithm with k-means++ initialization for faster convergence.
§Algorithm
- Initialize centroids using k-means++
- Assign each sample to nearest centroid
- Update centroids as mean of assigned samples
- Repeat until convergence or max iterations
§Examples
use aprender::prelude::*;
let data = Matrix::from_vec(6, 2, vec![
1.0, 2.0,
1.5, 1.8,
5.0, 8.0,
8.0, 8.0,
1.0, 0.6,
9.0, 11.0,
]).expect("Valid matrix dimensions and data length");
let mut kmeans = KMeans::new(2);
kmeans.fit(&data).expect("Fit succeeds with valid data");
let labels = kmeans.predict(&data);
assert_eq!(labels.len(), 6);§Performance
- Time complexity: O(nkdi) where n=samples, k=clusters, d=features, i=iterations
- Space complexity: O(nk)
Implementations§
Source§impl KMeans
impl KMeans
Sourcepub fn new(n_clusters: usize) -> Self
pub fn new(n_clusters: usize) -> Self
Creates a new K-Means with the specified number of clusters.
Sourcepub fn with_max_iter(self, max_iter: usize) -> Self
pub fn with_max_iter(self, max_iter: usize) -> Self
Sets the maximum number of iterations.
Sourcepub fn with_random_state(self, seed: u64) -> Self
pub fn with_random_state(self, seed: u64) -> Self
Sets the random seed for reproducibility.
Sourcepub fn n_clusters(&self) -> usize
pub fn n_clusters(&self) -> usize
Returns the number of clusters.
Sourcepub fn random_state(&self) -> Option<u64>
pub fn random_state(&self) -> Option<u64>
Returns the random state.
Sourcepub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), String>
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), String>
Saves the model to a binary file using bincode.
§Errors
Returns an error if serialization or file writing fails.
Sourcepub fn load<P: AsRef<Path>>(path: P) -> Result<Self, String>
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, String>
Loads a model from a binary file.
§Errors
Returns an error if file reading or deserialization fails.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for KMeans
impl<'de> Deserialize<'de> for KMeans
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for KMeans
impl RefUnwindSafe for KMeans
impl Send for KMeans
impl Sync for KMeans
impl Unpin for KMeans
impl UnwindSafe for KMeans
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more