milia.represent.diffusion#

Diffusion maps (Coifman and Lafon, 2006).

Functions

embed(X[, n, alpha, sigma])

Diffusion-map coordinates and eigenvalues of the rows of X.

Classes

Diffusion([n_components, alpha, sigma])

Diffusion-map embedding as a scikit-learn estimator.

milia.represent.diffusion.embed(X, n=8, alpha=1.0, sigma='median')#

Diffusion-map coordinates and eigenvalues of the rows of X.

A Gaussian kernel on Euclidean distance is alpha-normalised (alpha=1 removes the effect of sampling density), turned into a Markov matrix, and its leading non-trivial eigenvectors give the coordinates. Dense O(n^2) memory, fine for a few thousand rows.

Parameters:
  • X (ArrayLike) – Data, shape (n_samples, n_features).

  • n (int) – Number of diffusion coordinates.

  • alpha (float) – Density normalisation exponent.

  • sigma (float | str) – Kernel bandwidth, or "median" for the median pairwise distance.

Return type:

tuple[ndarray, ndarray]

Returns:

(eigenvalues, coordinates) of shapes (n,) and (n_samples, n), eigenvalues in decreasing order.

Examples

>>> X = np.vstack([np.zeros((10, 2)), np.full((10, 2), 5.0)]) + np.random.default_rng(0).normal(size=(20, 2))
>>> values, coords = embed(X, n=2)
>>> coords.shape, bool(values[0] >= values[1])
((20, 2), True)
class milia.represent.diffusion.Diffusion(n_components=8, alpha=1.0, sigma='median')#

Bases: BaseEstimator

Diffusion-map embedding as a scikit-learn estimator.

Like t-SNE it embeds the data it is fitted on and has no out-of-sample transform; use fit_transform().

Parameters:
  • n_components (int) – Number of diffusion coordinates.

  • alpha (float) – Density normalisation exponent.

  • sigma (float | str) – Kernel bandwidth, or "median".

Variables:
  • eigenvalues – Eigenvalues of the kept coordinates, for spotting a spectral gap.

  • embedding – The coordinates of the training rows.

Examples

>>> model = Diffusion(n_components=2).fit(np.random.default_rng(0).normal(size=(30, 3)))
>>> model.embedding_.shape, model.eigenvalues_.shape
((30, 2), (2,))
fit(X, y=None)#

Embed the training rows.

Parameters:
  • X (ArrayLike) – Data, shape (n_samples, n_features).

  • y (None) – Ignored; present for API consistency.

Return type:

Diffusion

Returns:

The fitted estimator, with embedding_ and eigenvalues_.

fit_transform(X, y=None)#

Fit and return the diffusion coordinates of the training rows.

Parameters:
  • X (ArrayLike) – Data, shape (n_samples, n_features).

  • y (None) – Ignored; present for API consistency.

Return type:

ndarray

Returns:

The coordinates, shape (n_samples, n_components).

Examples

>>> X = np.random.default_rng(0).normal(size=(30, 3))
>>> Diffusion(n_components=3).fit_transform(X).shape
(30, 3)