pub struct AgglomerativeClustering { /* private fields */ }Expand description
Agglomerative (bottom-up) hierarchical clustering.
Builds a tree of clusters (dendrogram) by iteratively merging closest clusters using various linkage methods.
§Algorithm
- Start with each point as its own cluster
- Repeat until reaching
n_clusters:- Find two closest clusters using linkage method
- Merge them
- Update distance matrix
- Return cluster labels
§Examples
use aprender::prelude::*;
let data = Matrix::from_vec(6, 2, vec![
1.0, 1.0, 1.1, 1.0, 1.0, 1.1,
5.0, 5.0, 5.1, 5.0, 5.0, 5.1,
]).expect("Valid matrix dimensions and data length");
let mut hc = AgglomerativeClustering::new(2, Linkage::Average);
hc.fit(&data).expect("Fit succeeds with valid data");
let labels = hc.predict(&data);
assert_eq!(labels.len(), 6);§Performance
- Time complexity: O(n³) for naive implementation
- Space complexity: O(n²) for distance matrix
Implementations§
Source§impl AgglomerativeClustering
impl AgglomerativeClustering
Sourcepub fn new(n_clusters: usize, linkage: Linkage) -> Self
pub fn new(n_clusters: usize, linkage: Linkage) -> Self
Create new AgglomerativeClustering with target number of clusters and linkage method.
Sourcepub fn n_clusters(&self) -> usize
pub fn n_clusters(&self) -> usize
Get target number of clusters.
Sourcepub fn dendrogram(&self) -> &Vec<Merge>
pub fn dendrogram(&self) -> &Vec<Merge>
Get dendrogram merge history (panic if not fitted).
Trait Implementations§
Source§impl Clone for AgglomerativeClustering
impl Clone for AgglomerativeClustering
Source§fn clone(&self) -> AgglomerativeClustering
fn clone(&self) -> AgglomerativeClustering
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for AgglomerativeClustering
impl Debug for AgglomerativeClustering
Source§impl<'de> Deserialize<'de> for AgglomerativeClustering
impl<'de> Deserialize<'de> for AgglomerativeClustering
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 AgglomerativeClustering
impl Serialize for AgglomerativeClustering
Auto Trait Implementations§
impl Freeze for AgglomerativeClustering
impl RefUnwindSafe for AgglomerativeClustering
impl Send for AgglomerativeClustering
impl Sync for AgglomerativeClustering
impl Unpin for AgglomerativeClustering
impl UnwindSafe for AgglomerativeClustering
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