pub struct FastKMeans { /* private fields */ }Expand description
Fast k-means clustering implementation compatible with ndarray.
This implementation uses double-chunking to process large datasets efficiently without running out of memory. It provides an API similar to FAISS and scikit-learn.
§Example
use fastkmeans_rs::FastKMeans;
use ndarray::Array2;
use ndarray_rand::RandomExt;
use ndarray_rand::rand_distr::Uniform;
// Generate random data
let data = Array2::random((1000, 128), Uniform::new(-1.0f32, 1.0));
// Create and train the model
let mut kmeans = FastKMeans::new(128, 10);
kmeans.train(&data.view()).unwrap();
// Get cluster assignments
let labels = kmeans.predict(&data.view()).unwrap();Implementations§
Source§impl FastKMeans
impl FastKMeans
Sourcepub fn new(d: usize, k: usize) -> FastKMeans
pub fn new(d: usize, k: usize) -> FastKMeans
Sourcepub fn with_config(config: KMeansConfig) -> FastKMeans
pub fn with_config(config: KMeansConfig) -> FastKMeans
Sourcepub fn train(
&mut self,
data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>,
) -> Result<(), KMeansError>
pub fn train( &mut self, data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>, ) -> Result<(), KMeansError>
Sourcepub fn fit(
&mut self,
data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>,
) -> Result<&mut FastKMeans, KMeansError>
pub fn fit( &mut self, data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>, ) -> Result<&mut FastKMeans, KMeansError>
Sourcepub fn predict(
&self,
data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>,
) -> Result<ArrayBase<OwnedRepr<i64>, Dim<[usize; 1]>>, KMeansError>
pub fn predict( &self, data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>, ) -> Result<ArrayBase<OwnedRepr<i64>, Dim<[usize; 1]>>, KMeansError>
Predict cluster assignments for new data.
This method mimics the scikit-learn predict() API.
§Arguments
data- Data to predict, of shape (n_samples, n_features)
§Returns
Returns an array of cluster labels of shape (n_samples,).
§Errors
Returns an error if:
- The model has not been fitted yet
- Data dimensions don’t match the training data
Sourcepub fn fit_predict(
&mut self,
data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>,
) -> Result<ArrayBase<OwnedRepr<i64>, Dim<[usize; 1]>>, KMeansError>
pub fn fit_predict( &mut self, data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>, ) -> Result<ArrayBase<OwnedRepr<i64>, Dim<[usize; 1]>>, KMeansError>
Sourcepub fn centroids(&self) -> Option<&ArrayBase<OwnedRepr<f32>, Dim<[usize; 2]>>>
pub fn centroids(&self) -> Option<&ArrayBase<OwnedRepr<f32>, Dim<[usize; 2]>>>
Get the centroids of the fitted model.
§Returns
Returns Some(&Array2<f32>) if the model has been fitted, None otherwise.
Sourcepub fn config(&self) -> &KMeansConfig
pub fn config(&self) -> &KMeansConfig
Get the configuration.
Auto Trait Implementations§
impl Freeze for FastKMeans
impl RefUnwindSafe for FastKMeans
impl Send for FastKMeans
impl Sync for FastKMeans
impl Unpin for FastKMeans
impl UnwindSafe for FastKMeans
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> 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