Skip to main content

FastKMeans

Struct FastKMeans 

Source
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

Source

pub fn new(d: usize, k: usize) -> FastKMeans

Create a new FastKMeans instance with default configuration.

§Arguments
  • d - Number of features (dimensions) in the data
  • k - Number of clusters
§Panics

Panics if k is 0.

Source

pub fn with_config(config: KMeansConfig) -> FastKMeans

Create a new FastKMeans instance with custom configuration.

§Arguments
  • config - Custom configuration for the k-means algorithm
§Panics

Panics if config.k is 0.

Source

pub fn train( &mut self, data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>, ) -> Result<(), KMeansError>

Train the k-means model on the given data.

This method mimics the FAISS train() API.

§Arguments
  • data - Training data of shape (n_samples, n_features)
§Errors

Returns an error if:

  • Number of samples is less than k
  • Data dimensions don’t match (for subsequent calls)
Source

pub fn fit( &mut self, data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>, ) -> Result<&mut FastKMeans, KMeansError>

Fit the model to the data.

This method mimics the scikit-learn fit() API. It is equivalent to train().

§Arguments
  • data - Training data of shape (n_samples, n_features)
§Returns

Returns &mut Self for method chaining.

Source

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
Source

pub fn fit_predict( &mut self, data: &ArrayBase<ViewRepr<&f32>, Dim<[usize; 2]>>, ) -> Result<ArrayBase<OwnedRepr<i64>, Dim<[usize; 1]>>, KMeansError>

Fit the model and predict cluster assignments in one call.

This method mimics the scikit-learn fit_predict() API.

§Arguments
  • data - Training data of shape (n_samples, n_features)
§Returns

Returns an array of cluster labels of shape (n_samples,).

Source

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.

Source

pub fn k(&self) -> usize

Get the number of clusters.

Source

pub fn d(&self) -> usize

Get the number of features (dimensions).

Source

pub fn config(&self) -> &KMeansConfig

Get the configuration.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V