pub struct SemanticClusterer {
pub algorithm: ClusterAlgorithm,
pub dims: usize,
}Expand description
Multi-algorithm semantic vector clustering engine.
use ipfrs_semantic::semantic_clusterer::{
SemanticClusterer, ClusterAlgorithm, ScClusterPoint,
};
let points: Vec<ScClusterPoint> = (0..20)
.map(|i| ScClusterPoint::new(format!("p{i}"), vec![(i % 4) as f64, (i / 4) as f64]))
.collect();
let clusterer = SemanticClusterer::new(
ClusterAlgorithm::KMeans { k: 4, max_iter: 100, tolerance: 1e-6 },
2,
);
let result = clusterer.fit(&points).expect("clustering failed");
assert_eq!(result.clusters.len(), 4);Fields§
§algorithm: ClusterAlgorithmAlgorithm configuration.
dims: usizeExpected embedding dimensionality.
Implementations§
Source§impl SemanticClusterer
impl SemanticClusterer
Sourcepub fn new(algorithm: ClusterAlgorithm, dims: usize) -> Self
pub fn new(algorithm: ClusterAlgorithm, dims: usize) -> Self
Create a new clusterer with the given algorithm and embedding dimension.
Sourcepub fn fit(
&self,
points: &[ScClusterPoint],
) -> Result<ScClusteringResult, ClusterError>
pub fn fit( &self, points: &[ScClusterPoint], ) -> Result<ScClusteringResult, ClusterError>
Run clustering on the provided points.
Returns a ScClusteringResult on success.
Sourcepub fn predict(
&self,
point: &ScClusterPoint,
result: &ScClusteringResult,
) -> Option<usize>
pub fn predict( &self, point: &ScClusterPoint, result: &ScClusteringResult, ) -> Option<usize>
Assign a new point to the nearest cluster centroid in result.
Returns None when result has no clusters.
Sourcepub fn cosine_distance(a: &[f64], b: &[f64]) -> f64
pub fn cosine_distance(a: &[f64], b: &[f64]) -> f64
Cosine distance: 1 - cosine_similarity(a, b).
Returns 1.0 for zero vectors.
Sourcepub fn euclidean_distance(a: &[f64], b: &[f64]) -> f64
pub fn euclidean_distance(a: &[f64], b: &[f64]) -> f64
Euclidean distance between two vectors of equal length.
Sourcepub fn silhouette_score(
points: &[ScClusterPoint],
result: &ScClusteringResult,
) -> f64
pub fn silhouette_score( points: &[ScClusterPoint], result: &ScClusteringResult, ) -> f64
Mean silhouette coefficient for the clustering.
Uses a random subset of up to 100 points when the dataset is large.
Sourcepub fn compute_centroid(embeddings: &[&[f64]]) -> Vec<f64>
pub fn compute_centroid(embeddings: &[&[f64]]) -> Vec<f64>
Compute the centroid (element-wise mean) of a set of embeddings.
Returns a zero vector when embeddings is empty or their slices are empty.
Sourcepub fn stats(result: &ScClusteringResult) -> ScClustererStats
pub fn stats(result: &ScClusteringResult) -> ScClustererStats
Summary statistics for a completed clustering result.
Trait Implementations§
Source§impl Clone for SemanticClusterer
impl Clone for SemanticClusterer
Source§fn clone(&self) -> SemanticClusterer
fn clone(&self) -> SemanticClusterer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for SemanticClusterer
impl RefUnwindSafe for SemanticClusterer
impl Send for SemanticClusterer
impl Sync for SemanticClusterer
impl Unpin for SemanticClusterer
impl UnsafeUnpin for SemanticClusterer
impl UnwindSafe for SemanticClusterer
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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
impl<T> Read<Exclusive, BecauseExclusive> 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.