linfa_clustering/k_means/
errors.rs

1use thiserror::Error;
2
3/// An error when fitting with an invalid hyperparameter
4#[derive(Error, Debug)]
5pub enum KMeansParamsError {
6    #[error("n_clusters cannot be 0")]
7    NClusters,
8    #[error("n_runs cannot be 0")]
9    NRuns,
10    #[error("tolerance must be greater than 0")]
11    Tolerance,
12    #[error("max_n_iterations cannot be 0")]
13    MaxIterations,
14}
15
16/// An error when modeling a KMeans algorithm
17#[derive(Error, Debug)]
18pub enum KMeansError {
19    /// When any of the hyperparameters are set the wrong value
20    #[error("Invalid hyperparameter: {0}")]
21    InvalidParams(#[from] KMeansParamsError),
22    /// When inertia computation fails
23    #[error("Fitting failed: No inertia improvement (-inf)")]
24    InertiaError,
25    #[error(transparent)]
26    LinfaError(#[from] linfa::error::Error),
27}
28
29#[derive(Error, Debug)]
30pub enum IncrKMeansError<M: std::fmt::Debug> {
31    /// When any of the hyperparameters are set the wrong value
32    #[error("Invalid hyperparameter: {0}")]
33    InvalidParams(#[from] KMeansParamsError),
34    /// When the distance between the old and new centroids exceeds the tolerance parameter. Not an
35    /// actual error, just there to signal that the algorithm should keep running.
36    #[error("Algorithm has not yet converged, Keep on running the algorithm.")]
37    NotConverged(M),
38    #[error(transparent)]
39    LinfaError(#[from] linfa::error::Error),
40}