pub struct MiniBatchKmeans<D = SquaredEuclidean>where
D: DistanceMetric,{ /* private fields */ }Expand description
Mini-Batch K-means clustering (Sculley, 2010).
Maintains k centroids and updates them incrementally as new data arrives. The first batch initializes centroids via k-means++; subsequent batches use a decaying learning rate per centroid.
use clump::MiniBatchKmeans;
let mut mbk = MiniBatchKmeans::new(2).with_seed(42);
// First batch seeds centroids via k-means++
let batch1 = vec![
vec![0.0f32, 0.0], vec![0.1, 0.1],
vec![10.0, 10.0], vec![10.1, 10.1],
];
let labels = mbk.update_batch(&batch1).unwrap();
assert_eq!(labels.len(), 4);
// Subsequent updates refine centroids
let label = mbk.update(&[0.05, 0.05]).unwrap();
assert_eq!(mbk.n_clusters(), 2);Implementations§
Source§impl MiniBatchKmeans
impl MiniBatchKmeans
Sourcepub fn new(k: usize) -> MiniBatchKmeans
pub fn new(k: usize) -> MiniBatchKmeans
Create a new Mini-Batch K-means clusterer with default squared Euclidean distance.
Source§impl<D> MiniBatchKmeans<D>where
D: DistanceMetric,
impl<D> MiniBatchKmeans<D>where
D: DistanceMetric,
Sourcepub fn with_metric(k: usize, metric: D) -> MiniBatchKmeans<D>
pub fn with_metric(k: usize, metric: D) -> MiniBatchKmeans<D>
Create a new Mini-Batch K-means clusterer with a custom distance metric.
Sourcepub fn with_seed(self, seed: u64) -> MiniBatchKmeans<D>
pub fn with_seed(self, seed: u64) -> MiniBatchKmeans<D>
Set random seed for reproducibility.
Source§impl<D> MiniBatchKmeans<D>where
D: DistanceMetric,
impl<D> MiniBatchKmeans<D>where
D: DistanceMetric,
Sourcepub fn update(&mut self, point: &[f32]) -> Result<usize, Error>
pub fn update(&mut self, point: &[f32]) -> Result<usize, Error>
Update the model with a single new point, returning its cluster assignment.
Sourcepub fn update_batch(
&mut self,
points: &(impl DataRef + ?Sized),
) -> Result<Vec<usize>, Error>
pub fn update_batch( &mut self, points: &(impl DataRef + ?Sized), ) -> Result<Vec<usize>, Error>
Update the model with a mini-batch of points.
Sourcepub fn predict(
&self,
data: &(impl DataRef + ?Sized),
) -> Result<Vec<usize>, Error>
pub fn predict( &self, data: &(impl DataRef + ?Sized), ) -> Result<Vec<usize>, Error>
Predict cluster labels for new points without modifying centroids.
Read-only inference: assigns each point to its nearest centroid but does not update centroid positions or counts.
Sourcepub fn n_clusters(&self) -> usize
pub fn n_clusters(&self) -> usize
Get the current number of clusters.
Trait Implementations§
Source§impl<D> Clone for MiniBatchKmeans<D>where
D: Clone + DistanceMetric,
impl<D> Clone for MiniBatchKmeans<D>where
D: Clone + DistanceMetric,
Source§fn clone(&self) -> MiniBatchKmeans<D>
fn clone(&self) -> MiniBatchKmeans<D>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<D> Freeze for MiniBatchKmeans<D>where
D: Freeze,
impl<D> RefUnwindSafe for MiniBatchKmeans<D>where
D: RefUnwindSafe,
impl<D> Send for MiniBatchKmeans<D>
impl<D> Sync for MiniBatchKmeans<D>
impl<D> Unpin for MiniBatchKmeans<D>where
D: Unpin,
impl<D> UnsafeUnpin for MiniBatchKmeans<D>where
D: UnsafeUnpin,
impl<D> UnwindSafe for MiniBatchKmeans<D>where
D: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
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>
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>
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