Decomposition Diffusion Map#
diffusion_map
#
| CLASS | DESCRIPTION |
|---|---|
DiffusionMap |
Diffusion map for nonlinear dimensionality reduction. |
| FUNCTION | DESCRIPTION |
|---|---|
diffusion_map |
Computes diffusion coordinates based on kernel matrix eigendecomposition |
Classes#
DiffusionMap(*, kernel_fn: Kernel | str = 'gaussian', kernel_kwargs: dict | None = None, distance_fn: PairwiseDistance | str = 'euclidean', distance_kwargs: dict | None = None, score_fn: Callable | None = None, out_features: int = 0, symmetric_eigendecomposition: bool = True, norm_l2_eigenvectors: bool = True)
#
Bases: Decomposition
Diffusion map for nonlinear dimensionality reduction.
Computes diffusion coordinates based on kernel matrix eigendecomposition [1, 2]. Provides sklearn-style fit and transform interface. Diffusion maps are particularly effective for manifold learning and nonlinear dimensionality reduction on data with intrinsic low-dimensional structure.
This implementation uses a modular architecture that separates pairwise distance computation from kernel computation, allowing flexible combinations of distance metrics (Euclidean, covariance, Mahalanobis) with various kernel functions (Gaussian, t-distribution, etc.).
| PARAMETER | DESCRIPTION |
|---|---|
kernel_fn
|
Kernel function for similarity computation. Can be a Kernel instance,
string name of a kernel, or None. Use kernel's
TYPE:
|
kernel_kwargs
|
Additional keyword arguments to pass to the kernel function if
TYPE:
|
distance_fn
|
Pairwise distance function. Can be a PairwiseDistance instance, string name of a distance function, or None.
TYPE:
|
distance_kwargs
|
Additional keyword arguments to pass to the distance function if
TYPE:
|
score_fn
|
Scoring function to evaluate the model. Must be callable if provided. If None,
TYPE:
|
out_features
|
Number of eigenvectors to compute. If 0, computes all eigenvectors. Must be non-negative.
TYPE:
|
symmetric_eigendecomposition
|
Whether to perform symmetric eigendecomposition.
TYPE:
|
norm_l2_eigenvectors
|
Whether to normalize eigenvectors to unit L2 norm.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
eigenvalues |
Eigenvalues from kernel matrix decomposition of shape (out_features, ).
TYPE:
|
eigenvectors |
Eigenvectors from kernel matrix decomposition of shape (n_samples, out_features).
TYPE:
|
diffusion_coordinates |
Diffusion coordinates extracted from the eigenvectors (n_samples, out_features) or (n_samples, out_features - 1).
TYPE:
|
kernel |
Computed kernel matrix of shape (n_samples, n_samples).
TYPE:
|
explained_variance |
Explained variance for each component.
TYPE:
|
cumulative_explained_variance |
Cumulative explained variance for components.
TYPE:
|
Examples:
Basic usage with Gaussian kernel:
>>> import torch
>>> from spectre.decomposition import DiffusionMap
>>> from spectre.kernel import GaussianKernel
>>> from spectre.pairwise_distance import PairwiseDistanceEuclidean
>>> X = torch.randn(100, 5) # 100 samples, 5 features
>>> kernel_fn = GaussianKernel(bw_method=torch.tensor(0.5))
>>> distance_fn = PairwiseDistanceEuclidean()
>>> dm = DiffusionMap(
... kernel_fn=kernel_fn,
... distance_fn=distance_fn,
... out_features=3,
... )
>>> dm.fit(X)
>>> X_embedded = dm.predict(X) # Shape: (100, 3)
With kernel normalization:
>>> from spectre.kernel.normalization import KernelNormalizer
>>> normalization = KernelNormalizer(norm_type="markov_symmetric", alpha=0.5)
>>> kernel_fn = GaussianKernel(
... bw_method=torch.tensor(0.5),
... normalization=normalization,
... )
>>> dm = DiffusionMap(
... kernel_fn=kernel_fn,
... distance_fn=distance_fn,
... out_features=10,
... )
>>> dm.fit(X)
| METHOD | DESCRIPTION |
|---|---|
score |
Compute score using the provided scoring function. |
fit |
Fit the diffusion map to the provided data. |
predict |
Predict diffusion coordinates for new data using Nyström extension. |
Source code in spectre/decomposition/diffusion_map.py
Attributes#
diffusion_coordinates: torch.Tensor
property
#
Return the fitted diffusion coordinates.
explained_variance: torch.Tensor
property
#
Explained variance for each diffusion component.
cumulative_explained_variance: torch.Tensor
property
#
Cumulative explained variance for diffusion components.
eigenvalues: torch.Tensor
property
#
Eigenvalues of the diffusion matrix.
eigenvectors: torch.Tensor
property
#
Eigenvectors of the diffusion matrix.
kernel: torch.Tensor
property
#
Kernel matrix used for diffusion map computation.
Functions#
score(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None, **kwargs) -> float | torch.Tensor
#
Compute score using the provided scoring function.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, out_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Target values for scoring.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float | Tensor
|
Score computed by the scoring function. |
Source code in spectre/decomposition/diffusion_map.py
fit(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> DiffusionMap
#
Fit the diffusion map to the provided data.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Training data of shape (n_samples, out_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|
target
|
Not used in diffusion maps (unsupervised method).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
self
|
Fitted estimator.
TYPE:
|
Source code in spectre/decomposition/diffusion_map.py
predict(X: torch.Tensor) -> torch.Tensor
#
Predict diffusion coordinates for new data using Nyström extension.
For training data, returns the fitted diffusion coordinates. For new data, uses Nyström extension to approximate the embedding.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, n_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Diffusion coordinates of shape (n_samples, out_features) or (n_samples, out_features - 1) depending on out_features setting. |
Notes
The Nyström extension approximates the embedding of new points by computing kernel similarities to training points and projecting onto the learned eigenvectors. This is an approximation that works well when the new data lies on or near the manifold learned from training data.
Examples:
>>> import torch
>>> from spectre.decomposition import DiffusionMap
>>> from spectre.kernel import GaussianKernel
>>> from spectre.pairwise_distance import PairwiseDistanceEuclidean
>>> X_train = torch.randn(100, 5)
>>> X_test = torch.randn(20, 5)
>>> kernel_fn = GaussianKernel(bw_method=torch.tensor(0.5))
>>> dm = DiffusionMap(
... kernel_fn=kernel_fn,
... distance_fn=PairwiseDistanceEuclidean(),
... out_features=3,
... )
>>> dm.fit(X_train)
>>> X_test_embedded = dm.predict(X_test) # Shape: (20, 3)
Source code in spectre/decomposition/diffusion_map.py
Functions#
diffusion_map(X: torch.Tensor, weights: torch.Tensor | None = None, *, kernel_fn: Kernel, distance_fn: PairwiseDistance, out_features: int = 0, symmetric_eigendecomposition: bool = True, norm_l2_eigenvectors: bool = True) -> DecompositionResult
#
Computes diffusion coordinates based on kernel matrix eigendecomposition [1, 2]. Provides sklearn-style fit and transform interface. Diffusion maps are particularly effective for manifold learning and nonlinear dimensionality reduction on data with intrinsic low-dimensional structure.
This implementation uses a modular architecture that separates pairwise distance computation from kernel computation, allowing flexible combinations of distance metrics (Euclidean, covariance, Mahalanobis) with various kernel functions (Gaussian, t-distribution, etc.).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, out_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples,). If None, weights are assigned ones.
TYPE:
|
kernel_fn
|
Kernel function for similarity computation. Must be a Kernel instance.
Use kernel's
TYPE:
|
distance_fn
|
Pairwise distance function. Must be a PairwiseDistance instance.
TYPE:
|
out_features
|
Number of eigenvectors to compute. If 0, computes all eigenvectors. Must be non-negative.
TYPE:
|
symmetric_eigendecomposition
|
Whether to compute symmetric eigendecomposition.
TYPE:
|
norm_l2_eigenvectors
|
Whether to normalize eigenvectors to unit L2 norm.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DecompositionResult
|
A tuple containing:
|
Examples:
Basic usage with Gaussian kernel:
>>> import torch
>>> from spectre.decomposition.diffusion_map import diffusion_map
>>> from spectre.kernel import GaussianKernel
>>> from spectre.pairwise_distance import PairwiseDistanceEuclidean
>>> X = torch.randn(50, 10)
>>> kernel_fn = GaussianKernel(bw_method=torch.tensor(1.0))
>>> pairwise_dist_fn = PairwiseDistanceEuclidean()
>>> eigenvals, eigenvecs, kernel_matrix = diffusion_map(
... X=X,
... kernel_fn=kernel_fn,
... distance_fn=pairwise_dist_fn,
... out_features=5,
... )
With kernel normalization:
>>> from spectre.kernel.normalization import KernelNormalizer
>>> normalization = KernelNormalizer(norm_type="markov_symmetric", alpha=0.5)
>>> kernel_fn = GaussianKernel(
... bw_method=torch.tensor(1.0),
... normalization=normalization,
... )
>>> eigenvals, eigenvecs, K = diffusion_map(
... X=X,
... kernel_fn=kernel_fn,
... distance_fn=pairwise_dist_fn,
... out_features=5,
... )
Source code in spectre/decomposition/diffusion_map.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | |