Skip to main content

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    #[error(
15        "only KMeansAlgorithm::Lloyd is supported by fit_with (Mini-Batch K-means); \
16         Hamerly requires a persistent dataset across iterations and cannot be used incrementally"
17    )]
18    IncrementalHamerly,
19}
20
21/// An error when modeling a KMeans algorithm
22#[derive(Error, Debug)]
23pub enum KMeansError {
24    /// When any of the hyperparameters are set the wrong value
25    #[error("Invalid hyperparameter: {0}")]
26    InvalidParams(#[from] KMeansParamsError),
27    /// When inertia computation fails
28    #[error("Fitting failed: No inertia improvement (-inf)")]
29    InertiaError,
30    #[error(transparent)]
31    LinfaError(#[from] linfa::error::Error),
32}
33
34#[derive(Error, Debug)]
35pub enum IncrKMeansError<M: std::fmt::Debug> {
36    /// When any of the hyperparameters are set the wrong value
37    #[error("Invalid hyperparameter: {0}")]
38    InvalidParams(#[from] KMeansParamsError),
39    /// When the distance between the old and new centroids exceeds the tolerance parameter. Not an
40    /// actual error, just there to signal that the algorithm should keep running.
41    #[error("Algorithm has not yet converged, Keep on running the algorithm.")]
42    NotConverged(M),
43    #[error(transparent)]
44    LinfaError(#[from] linfa::error::Error),
45}