Documentation
¶
Index ¶
- func ClipValue(x, clipLimit float64) float64
- func ComputeMembershipStrengths(knnIndices [][]int, knnDists [][]float64, sigmas, rhos []float64, nSamples int) *sparse.COO
- func ComputeMembershipStrengthsBipartite(knnIndices [][]int, knnDists [][]float64, sigmas, rhos []float64, ...) *sparse.COO
- func EpochsOfNextNegativeSample(epochsPerSample []float64, negativeSampleRate float64) []float64
- func EpochsOfNextSample(epochsPerSample []float64) []float64
- func FindABParams(spread, minDist float64) (float64, float64)
- func MakeEpochsPerSample(weights []float64, nEpochs int) []float64
- func OptimizeLayoutEuclidean(headEmbedding [][]float64, tailEmbedding [][]float64, head []int, tail []int, ...) [][]float64
- func OptimizeLayoutGeneric(headEmbedding [][]float64, tailEmbedding [][]float64, head []int, tail []int, ...) [][]float64
- func Rdist(x, y []float64) float64
- func SelectNEpochs(nSamples int) int
- func SpectralLayout(graph *sparse.CSR, nComponents int) [][]float64
- type FuzzySimplicialSetConfig
- type FuzzySimplicialSetResult
- type OptimizeLayoutConfig
- type Options
- type SmoothKNNResult
- type UMAP
- func (u *UMAP) A() float64
- func (u *UMAP) B() float64
- func (u *UMAP) Embedding() [][]float64
- func (u *UMAP) Fit(X [][]float64) error
- func (u *UMAP) FitTransform(X [][]float64, y []float64) ([][]float64, error)
- func (u *UMAP) Graph() *sparse.CSR
- func (u *UMAP) InverseTransform(XEmbedded [][]float64) ([][]float64, error)
- func (u *UMAP) Transform(XNew [][]float64) ([][]float64, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClipValue ¶
ClipValue clamps a value to [-clipLimit, clipLimit]. Used in SGD gradient updates. Matches UMAP's clip().
func ComputeMembershipStrengths ¶
func ComputeMembershipStrengths(knnIndices [][]int, knnDists [][]float64, sigmas, rhos []float64, nSamples int) *sparse.COO
ComputeMembershipStrengths computes the sparse fuzzy set (COO matrix) from kNN indices, distances, sigmas, and rhos.
For each point i and neighbor j:
w_ij = exp(-(d_ij - rho_i) / sigma_i)
Matches umap_.py compute_membership_strengths().
func ComputeMembershipStrengthsBipartite ¶
func ComputeMembershipStrengthsBipartite(knnIndices [][]int, knnDists [][]float64, sigmas, rhos []float64, nQueries, nTrain int) *sparse.COO
ComputeMembershipStrengthsBipartite is like ComputeMembershipStrengths but for out-of-sample mapping. It creates a bipartite graph of shape (nQueries x nTrain).
func EpochsOfNextNegativeSample ¶
EpochsOfNextNegativeSample precomputes the epoch at which each edge should next receive a negative sample.
func EpochsOfNextSample ¶
EpochsOfNextSample precomputes the epoch at which each edge should first be sampled. Used to drive the SGD loop.
func FindABParams ¶
FindABParams computes the a, b parameters for the UMAP membership function via curve fitting (Levenberg-Marquardt style).
The target curve is:
y = 1.0 if x <= min_dist y = exp(-(x - min_dist) / (spread - min_dist)) otherwise
And we fit: y = 1 / (1 + a * x^(2b))
Returns (a, b).
func MakeEpochsPerSample ¶
MakeEpochsPerSample computes the number of epochs between samples for each edge weight.
Given weights and n_epochs:
n_samples = n_epochs * (weight / max_weight) epochs_per_sample = n_epochs / n_samples
Which simplifies to: epochs_per_sample = max_weight / weight
Weights that are zero or negative get -1 (never sampled).
Matches umap_.py make_epochs_per_sample() exactly.
func OptimizeLayoutEuclidean ¶
func OptimizeLayoutEuclidean( headEmbedding [][]float64, tailEmbedding [][]float64, head []int, tail []int, epochsPerSample []float64, rngStates []nn.TauRandState, cfg OptimizeLayoutConfig, ) [][]float64
OptimizeLayoutEuclidean performs SGD optimization of the embedding layout.
Parameters:
headEmbedding: initial embedding positions (modified in place) tailEmbedding: reference embedding (same as headEmbedding for fit, different for transform) head: source indices of edges tail: target indices of edges epochsPerSample: how many epochs between samples for each edge rngState: per-sample Tausworthe PRNG states [nSamples][3] cfg: optimization configuration
Returns the optimized embedding (same slice as headEmbedding).
Matches umap/layouts.py optimize_layout_euclidean().
func OptimizeLayoutGeneric ¶
func OptimizeLayoutGeneric( headEmbedding [][]float64, tailEmbedding [][]float64, head []int, tail []int, epochsPerSample []float64, rngStates []nn.TauRandState, cfg OptimizeLayoutConfig, outputMetricGrad func(x, y []float64) (float64, []float64), ) [][]float64
OptimizeLayoutGeneric performs SGD with a custom output distance metric. Used for inverse_transform or non-Euclidean output spaces.
TODO: Implement this for inverse_transform support.
func Rdist ¶
Rdist computes the squared Euclidean distance between two vectors. Used in the SGD inner loop. Matches UMAP's rdist().
func SelectNEpochs ¶
SelectNEpochs chooses the number of optimization epochs based on dataset size if the user hasn't specified one. Matches umap_.py default n_epochs selection.
func SpectralLayout ¶
SpectralLayout computes the spectral initialization embedding.
Algorithm: 1. Compute the normalized graph Laplacian: L = D^{-1/2} (D - A) D^{-1/2} 2. Find the smallest nComponents+1 eigenvectors 3. Discard the trivial eigenvector (constant, eigenvalue ~0) 4. Return the next nComponents eigenvectors, scaled by sqrt(eigenvalue)
If the graph has multiple connected components, each component is handled independently and results are combined.
graph: the symmetrized fuzzy simplicial set (CSR) nComponents: number of embedding dimensions
Types ¶
type FuzzySimplicialSetConfig ¶
FuzzySimplicialSetConfig holds internal execution controls.
type FuzzySimplicialSetResult ¶
type FuzzySimplicialSetResult struct {
Graph *sparse.CSR // the symmetrized fuzzy simplicial set graph
Sigmas []float64 // per-point sigma values
Rhos []float64 // per-point rho values
SearchIndex *nn.SearchIndex // the nearest neighbor search index
}
FuzzySimplicialSetResult holds the output of FuzzySimplicialSet.
func FuzzySimplicialSet ¶
func FuzzySimplicialSet( data [][]float64, nNeighbors int, rng umaprand.Source, metric string, metricKwds map[string]any, localConnectivity float64, setOpMixRatio float64, ) *FuzzySimplicialSetResult
FuzzySimplicialSet constructs the fuzzy simplicial set from raw data. This is the main graph construction pipeline:
1. Compute kNN (brute-force or NN-Descent) 2. Smooth kNN distances to get sigma, rho 3. Compute membership strengths → sparse matrix 4. Symmetrize via fuzzy set union: W + W^T - W*W^T 5. Reset local connectivity
Matches umap_.py fuzzy_simplicial_set().
func FuzzySimplicialSetWithConfig ¶
func FuzzySimplicialSetWithConfig( data [][]float64, nNeighbors int, rng umaprand.Source, metric string, metricKwds map[string]any, localConnectivity float64, setOpMixRatio float64, cfg FuzzySimplicialSetConfig, ) *FuzzySimplicialSetResult
FuzzySimplicialSetWithConfig constructs the fuzzy simplicial set with execution config.
type OptimizeLayoutConfig ¶
type OptimizeLayoutConfig struct {
A float64 // UMAP a parameter
B float64 // UMAP b parameter
Gamma float64 // repulsive force weight (default: 1.0)
InitialAlpha float64 // initial learning rate (default: 1.0)
NegativeSampleRate float64 // negative samples per positive (default: 5.0)
NEpochs int // number of optimization epochs
MoveOther bool // update both head and tail embeddings
NWorkers int
ParallelMode string
}
OptimizeLayoutConfig holds the configuration for layout optimization.
type Options ¶
type Options struct {
// NNeighbors is the number of nearest neighbors to use for graph construction.
// Larger values capture more global structure at the cost of local detail.
// Default: 15.
NNeighbors int
// NComponents is the dimensionality of the output embedding.
// Default: 2.
NComponents int
// MinDist controls how tightly points are packed together.
// Smaller values produce more clustered embeddings.
// Default: 0.1.
MinDist float64
// Spread determines the scale of the embedding.
// Together with MinDist, controls the membership function.
// Default: 1.0.
Spread float64
// Metric is the distance metric to use on the input data.
// Default: "euclidean".
Metric string
// MetricKwds are additional parameters for parameterized metrics
// (e.g., "p" for Minkowski, "sigma" for StandardisedEuclidean).
MetricKwds map[string]any
// NEpochs is the number of SGD optimization epochs.
// If 0, automatically chosen based on dataset size.
NEpochs int
// InitMethod controls the initial embedding.
// "spectral" (default), "random", or a custom [][]float64.
InitMethod string
// CustomInit provides a pre-computed initial embedding.
// Only used if InitMethod == "custom".
CustomInit [][]float64
// LocalConnectivity is the number of nearest neighbors that should
// be assumed to be connected at a local level.
// Default: 1.0.
LocalConnectivity float64
// SetOpMixRatio controls the blend between fuzzy union and intersection
// for the symmetrization of the kNN graph.
// 1.0 = pure union (default), 0.0 = pure intersection.
SetOpMixRatio float64
// DisconnectionDistance removes edges with distances greater than or equal to this value.
// Default: +inf (no disconnection).
DisconnectionDistance float64
// NegativeSampleRate controls the number of negative samples per
// positive sample in SGD optimization.
// Default: 5.
NegativeSampleRate float64
// RepulsionStrength controls the weight of the repulsive force.
// Default: 1.0.
RepulsionStrength float64
// LearningRate is the initial SGD learning rate.
// Default: 1.0.
LearningRate float64
// RandSource provides the random number generator.
// Default: Production (wrapping math/rand/v2 with seed 42).
RandSource umaprand.Source
// TargetNNeighbors is the number of nearest neighbors for the target
// (y) space in supervised mode.
// Default: NNeighbors.
TargetNNeighbors int
// TargetMetric is the distance metric for the target space.
// Default: "categorical" for discrete labels, "euclidean" for continuous.
TargetMetric string
// TargetWeight controls the balance between data topology and target
// topology in supervised mode. 0.0 = data only, 1.0 = target only.
// Default: 0.5.
TargetWeight float64
// Verbose controls whether progress information is printed.
Verbose bool
// NWorkers controls the number of worker goroutines used by parallel-capable stages.
// If 0, runtime.GOMAXPROCS(0) is used.
// Default: runtime.GOMAXPROCS(0).
NWorkers int
// ParallelMode controls how parallel-capable stages execute.
// "auto" (default), "serial", or "parallel".
ParallelMode string
}
Options configures the UMAP algorithm.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns Options with all defaults set.
type SmoothKNNResult ¶
type SmoothKNNResult struct {
Sigmas []float64 // per-point bandwidth parameters
Rhos []float64 // per-point nearest neighbor distances
}
SmoothKNNResult holds the output of SmoothKNNDist.
func SmoothKNNDist ¶
func SmoothKNNDist(knnDists [][]float64, k float64, localConnectivity float64) *SmoothKNNResult
SmoothKNNDist computes the smooth nearest-neighbor distance parameters (sigma and rho) for each point using binary search.
For each point, we find sigma such that:
sum(exp(-(d_i - rho) / sigma)) = log2(k)
where d_i are the distances to k nearest neighbors and rho is the distance to the nearest neighbor (local connectivity adjustment).
Matches umap_.py smooth_knn_dist().
type UMAP ¶
type UMAP struct {
// contains filtered or unexported fields
}
UMAP is the main UMAP dimensionality reduction model.
func (*UMAP) Fit ¶
Fit fits the UMAP model to the data X without returning the embedding. X is a matrix of shape (n_samples, n_features) stored as [][]float64.
func (*UMAP) FitTransform ¶
FitTransform fits the UMAP model and returns the embedding. X: input data (n_samples x n_features) y: optional target labels for supervised mode (nil for unsupervised)
func (*UMAP) InverseTransform ¶
InverseTransform maps points from the embedding space back to data space.
TODO: Implement inverse transform.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package distance provides distance metric implementations for UMAP.
|
Package distance provides distance metric implementations for UMAP. |
|
Package nn implements nearest neighbor search for UMAP.
|
Package nn implements nearest neighbor search for UMAP. |
|
Package rand provides a random number source abstraction for UMAP.
|
Package rand provides a random number source abstraction for UMAP. |
|
Package sparse provides COO and CSR sparse matrix types for UMAP.
|
Package sparse provides COO and CSR sparse matrix types for UMAP. |