pub struct SpectralClustering { /* private fields */ }Expand description
Spectral Clustering using graph Laplacian and eigendecomposition.
Uses graph theory to find clusters by analyzing the spectrum (eigenvalues) of the graph Laplacian. Effective for non-convex cluster shapes.
§Algorithm
- Construct affinity matrix W (RBF or k-NN)
- Compute graph Laplacian: L = D - W (D = degree matrix)
- Find k smallest eigenvectors of L
- Cluster rows of eigenvector matrix using K-Means
§Examples
use aprender::prelude::*;
// Non-convex clusters (concentric circles)
let data = Matrix::from_vec(
8,
2,
vec![
0.0, 1.0, 1.0, 0.0, 0.0, -1.0, -1.0, 0.0, // Inner circle
0.0, 3.0, 3.0, 0.0, 0.0, -3.0, -3.0, 0.0, // Outer circle
],
)
.expect("Valid matrix dimensions and data length");
let mut sc = SpectralClustering::new(2).with_gamma(0.5);
sc.fit(&data).expect("Fit succeeds with valid data");
let labels = sc.predict(&data);§Performance
- Time complexity: O(n² + n³) for eigendecomposition
- Space complexity: O(n²) for affinity matrix
Implementations§
Source§impl SpectralClustering
impl SpectralClustering
Sourcepub fn new(n_clusters: usize) -> Self
pub fn new(n_clusters: usize) -> Self
Create a new Spectral Clustering with default parameters.
Default: RBF affinity, gamma=1.0, n_neighbors=10
Sourcepub fn with_affinity(self, affinity: Affinity) -> Self
pub fn with_affinity(self, affinity: Affinity) -> Self
Set the affinity type.
Sourcepub fn with_gamma(self, gamma: f32) -> Self
pub fn with_gamma(self, gamma: f32) -> Self
Set gamma for RBF kernel (higher = more local similarity).
Sourcepub fn with_n_neighbors(self, n_neighbors: usize) -> Self
pub fn with_n_neighbors(self, n_neighbors: usize) -> Self
Set number of neighbors for KNN affinity.
Trait Implementations§
Source§impl Clone for SpectralClustering
impl Clone for SpectralClustering
Source§fn clone(&self) -> SpectralClustering
fn clone(&self) -> SpectralClustering
Returns a duplicate of the value. Read more
1.0.0§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for SpectralClustering
impl Debug for SpectralClustering
Source§impl Default for SpectralClustering
impl Default for SpectralClustering
Source§impl<'de> Deserialize<'de> for SpectralClustering
impl<'de> Deserialize<'de> for SpectralClustering
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Serialize for SpectralClustering
impl Serialize for SpectralClustering
Auto Trait Implementations§
impl Freeze for SpectralClustering
impl RefUnwindSafe for SpectralClustering
impl Send for SpectralClustering
impl Sync for SpectralClustering
impl Unpin for SpectralClustering
impl UnwindSafe for SpectralClustering
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
🔬This is a nightly-only experimental API. (
clone_to_uninit)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