#[non_exhaustive]pub struct KMeans { /* private fields */ }Expand description
K-Means clustering.
Uses k-means++ initialization for better convergence.
When n_init > 1 (default 10), the algorithm runs multiple times
with different random seeds and keeps the result with the lowest inertia.
Implementations§
Source§impl KMeans
impl KMeans
Sourcepub fn n_init(self, n: usize) -> Self
pub fn n_init(self, n: usize) -> Self
Set the number of independent runs with different random seeds.
The result with the lowest inertia is kept. Default is 10, matching sklearn. Set to 1 for a single run (faster but less reliable).
Sourcepub fn fit(&mut self, data: &Dataset) -> Result<()>
pub fn fit(&mut self, data: &Dataset) -> Result<()>
Fit the model on a dataset (uses features only, ignores target).
When n_init > 1, runs K-Means multiple times and keeps the best.
Sourcepub fn predict(&self, features: &[Vec<f64>]) -> Result<Vec<usize>>
pub fn predict(&self, features: &[Vec<f64>]) -> Result<Vec<usize>>
Predict cluster assignments for new data.
Sourcepub fn transform(&self, features: &[Vec<f64>]) -> Result<Vec<Vec<f64>>>
pub fn transform(&self, features: &[Vec<f64>]) -> Result<Vec<Vec<f64>>>
Transform data into cluster-distance space.
Returns a n_samples × k matrix where each value is the Euclidean
distance from the sample to each centroid.
§Example
let distances = km.transform(&[vec![5.0, 5.0]]).unwrap();
assert_eq!(distances[0].len(), 2); // one distance per centroidTrait Implementations§
Auto Trait Implementations§
impl Freeze for KMeans
impl RefUnwindSafe for KMeans
impl Send for KMeans
impl Sync for KMeans
impl Unpin for KMeans
impl UnsafeUnpin 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