pub struct Umap { /* private fields */ }Expand description
UMAP dimensionality reduction algorithm.
This struct holds the configuration and metrics for UMAP. It can be reused to learn manifolds from multiple datasets with the same parameters.
§Example
use umap::{Umap, UmapConfig};
use ndarray::Array2;
let config = UmapConfig::default();
let umap = Umap::new(config);
// Learn the manifold structure
let manifold = umap.learn_manifold(
data.view(),
knn_indices.view(),
knn_dists.view(),
);
// Create an optimizer and run training
let mut opt = Optimizer::new(
manifold,
init,
500, // total epochs
config.optimization.repulsion_strength,
config.optimization.learning_rate,
config.optimization.negative_sample_rate,
&euclidean_metric,
);
while opt.remaining_epochs() > 0 {
opt.step_epochs(opt.remaining_epochs().min(10));
}
let fitted = opt.into_fitted(config);
let embedding = fitted.embedding();Implementations§
Source§impl Umap
impl Umap
Sourcepub fn new(config: UmapConfig) -> Self
pub fn new(config: UmapConfig) -> Self
Create a new UMAP instance with default Euclidean metrics.
Both the input space metric (for graph construction) and output space metric (for optimization) are set to Euclidean distance.
§Arguments
config- UMAP configuration parameters
Sourcepub fn with_metrics(
config: UmapConfig,
metric: Box<dyn Metric>,
output_metric: Box<dyn Metric>,
) -> Self
pub fn with_metrics( config: UmapConfig, metric: Box<dyn Metric>, output_metric: Box<dyn Metric>, ) -> Self
Create a UMAP instance with custom distance metrics.
§Arguments
config- UMAP configuration parametersmetric- Distance metric for input space (graph construction)output_metric- Distance metric for output embedding space (optimization)
§Example
let umap = Umap::with_metrics(
config,
Box::new(MyCustomMetric),
Box::new(EuclideanMetric),
);Sourcepub fn learn_manifold(
&self,
data: ArrayView2<'_, f32>,
knn_indices: ArrayView2<'_, u32>,
knn_dists: ArrayView2<'_, f32>,
) -> LearnedManifold
pub fn learn_manifold( &self, data: ArrayView2<'_, f32>, knn_indices: ArrayView2<'_, u32>, knn_dists: ArrayView2<'_, f32>, ) -> LearnedManifold
Learn the manifold structure from high-dimensional data.
This is the expensive graph construction phase that builds a fuzzy topological representation of the data. The result can be cached, serialized, and reused for multiple different optimizations.
This phase is deterministic (no randomness) and independent of the target embedding dimensionality.
§Arguments
data- Input data matrix (n_samples × n_features). Used for validation.knn_indices- Precomputed k-nearest neighbor indices (n_samples × n_neighbors). Each row contains indices of the k nearest neighbors for that sample.knn_dists- Precomputed k-nearest neighbor distances (n_samples × n_neighbors). Each row contains distances to the k nearest neighbors.
§Returns
A LearnedManifold containing the fuzzy simplicial set and local geometry.
§Panics
Panics if:
- Parameter validation fails (invalid ranges, incompatible sizes)
- Array shapes are incompatible
- Number of samples <= n_neighbors
§Example
let manifold = umap.learn_manifold(
data.view(),
knn_indices.view(),
knn_dists.view(),
);
// Save for later use
save_manifold(&manifold)?;Sourcepub fn fit(
&self,
data: ArrayView2<'_, f32>,
knn_indices: ArrayView2<'_, u32>,
knn_dists: ArrayView2<'_, f32>,
init: ArrayView2<'_, f32>,
) -> FittedUmap
pub fn fit( &self, data: ArrayView2<'_, f32>, knn_indices: ArrayView2<'_, u32>, knn_dists: ArrayView2<'_, f32>, init: ArrayView2<'_, f32>, ) -> FittedUmap
High-level convenience method that learns and optimizes in one call.
This is equivalent to:
learn_manifold()- build the graphOptimizer::new()- set up optimization- Run all epochs
into_fitted()- extract final model
For checkpointing or more control, use the lower-level API instead.
§Arguments
data- Input data matrix (n_samples × n_features)knn_indices- Precomputed k-nearest neighbor indicesknn_dists- Precomputed k-nearest neighbor distancesinit- Initial embedding coordinates (n_samples × n_components)
§Returns
A FittedUmap containing the optimized embedding and learned manifold.
§Example
let fitted = umap.fit(
data.view(),
knn_indices.view(),
knn_dists.view(),
init.view(),
);
let embedding = fitted.embedding();Auto Trait Implementations§
impl Freeze for Umap
impl !RefUnwindSafe for Umap
impl Send for Umap
impl Sync for Umap
impl Unpin for Umap
impl !UnwindSafe for Umap
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> 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<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§unsafe fn to_subset_unchecked(&self) -> SS
unsafe 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.