pub struct ClusterIndex { /* private fields */ }Expand description
K-means clustering index supporting ANN search by cluster probing.
Implementations§
Source§impl ClusterIndex
impl ClusterIndex
Sourcepub fn new(k: usize, max_iter: usize) -> Self
pub fn new(k: usize, max_iter: usize) -> Self
Create a new index.
k— number of clusters (must be ≥ 1)max_iter— maximum Lloyd iterations whenbuildis called
Sourcepub fn num_clusters(&self) -> usize
pub fn num_clusters(&self) -> usize
Current number of clusters.
Sourcepub fn insert(
&mut self,
id: String,
vector: Vec<f32>,
) -> Result<(), ClusterError>
pub fn insert( &mut self, id: String, vector: Vec<f32>, ) -> Result<(), ClusterError>
Insert a vector into the index, assigning it to the nearest centroid.
If no clustering has been built yet (centroids is empty), the vector
is stored without a valid cluster assignment and an initial cluster will
be assigned during build.
Returns an error when:
- The id is already present.
- The vector dimensionality is inconsistent with existing vectors.
Sourcepub fn build(&mut self) -> Result<(), ClusterError>
pub fn build(&mut self) -> Result<(), ClusterError>
Run Lloyd’s K-means algorithm to assign all vectors to clusters.
Initialisation: k-means++ style — first centroid chosen as the first vector, subsequent centroids chosen as the furthest from the current set.
Sourcepub fn assign(&self, query: &[f32]) -> Option<usize>
pub fn assign(&self, query: &[f32]) -> Option<usize>
Return the index of the centroid nearest to query.
Sourcepub fn cluster_stats(
&self,
cluster_id: usize,
) -> Result<ClusterStats, ClusterError>
pub fn cluster_stats( &self, cluster_id: usize, ) -> Result<ClusterStats, ClusterError>
Return statistics for the cluster with the given id.
Sourcepub fn all_cluster_stats(&self) -> Vec<ClusterStats>
pub fn all_cluster_stats(&self) -> Vec<ClusterStats>
Return statistics for all clusters.
Sourcepub fn merge_clusters(&mut self, a: usize, b: usize) -> Result<(), ClusterError>
pub fn merge_clusters(&mut self, a: usize, b: usize) -> Result<(), ClusterError>
Merge clusters a and b into a single cluster.
The merged centroid is the weighted mean of the two cluster centroids. All members of both clusters are reassigned to the lower id; the higher id slot is removed by swapping with the last centroid and truncating.
Sourcepub fn merge_closest_clusters(&mut self) -> Result<(), ClusterError>
pub fn merge_closest_clusters(&mut self) -> Result<(), ClusterError>
Merge the two closest clusters (by centroid distance).
Sourcepub fn split_largest_cluster(&mut self) -> Result<(), ClusterError>
pub fn split_largest_cluster(&mut self) -> Result<(), ClusterError>
Split the largest cluster into two by creating a new centroid displaced by the principal direction (first PCA component approximated as the direction of greatest variance along each axis).
The new centroid is appended to the end; half the members are re-assigned to it.
Trait Implementations§
Source§impl Clone for ClusterIndex
impl Clone for ClusterIndex
Source§fn clone(&self) -> ClusterIndex
fn clone(&self) -> ClusterIndex
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for ClusterIndex
impl RefUnwindSafe for ClusterIndex
impl Send for ClusterIndex
impl Sync for ClusterIndex
impl Unpin for ClusterIndex
impl UnsafeUnpin for ClusterIndex
impl UnwindSafe for ClusterIndex
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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>
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>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.