mojolearn.SpectralEmbedding#

class mojolearn.SpectralEmbedding(n_components=2, *, affinity='nearest_neighbors', random_state=None, n_neighbors=None, gamma=None, eigen_solver=None, eigen_tol=None, n_jobs=None, verbose=False)[source]#

Bases: object

Spectral embedding (Laplacian eigenmaps) for nonlinear dimensionality reduction.

Builds an affinity graph, forms the normalized graph Laplacian and returns its smallest nontrivial eigenvectors, computed with a thick-restart Lanczos solver. The first (trivial) eigenvector is dropped. Follows cuML’s manifold.SpectralEmbedding. There is no transform: the embedding covers the fitted rows only, as in scikit-learn.

Parameters:
  • n_components (int, default 2) – Dimension of the embedding; at least 1, and the eigenpairs needed must number fewer than n_samples.

  • affinity (str, default “nearest_neighbors”) – "nearest_neighbors" (symmetrized k-NN connectivity of X), "precomputed" (X is a square affinity matrix, dense or sparse), "rbf" (exp(-gamma * ||x_i - x_j||**2)) or "precomputed_nearest_neighbors" (X is a distance matrix or sparse distance graph; the n_neighbors nearest per row, symmetrized as 0.5 * (C + C.T)). A callable or any other name raises ValueError.

  • random_state (int or None, default None) – Seed in [0, 2**32). None means seed 0, so results are deterministic (unlike scikit-learn, where None draws a fresh seed).

  • n_neighbors (int or None, default None) – Neighbors per row; None uses max(n_samples // 10, 1).

  • gamma (float or None, default None) – Positive RBF coefficient, used only with affinity="rbf"; None means 1 / n_features (scikit-learn’s rule). Setting it with another affinity raises NotImplementedError.

  • eigen_solver (None) – Only None is accepted; any other value raises NotImplementedError.

  • eigen_tol (float, “auto” or None, default None) – Eigensolver tolerance. "auto" and None use 1e-5; otherwise a positive finite float.

  • n_jobs (int or None, default None) – Accepted for compatibility; it does not change the result.

  • verbose (bool, default False) – Accepted for compatibility; nothing is printed.

Variables:
  • embedding_ (Array of shape (n_samples, n_components), float32) – The embedding of the training rows.

  • affinity_matrix_ (array) – The dense affinity matrix built for "rbf" and "precomputed_nearest_neighbors".

  • n_neighbors_ (int) – Number of neighbors used, for the nearest-neighbor affinities.

  • gamma_ (float) – The RBF coefficient used, for affinity="rbf".

  • n_features_in_ (int) – Number of features of X, for "nearest_neighbors" and "rbf".

Notes

A precomputed affinity must be nonnegative and finite and must not repeat a (row, col) entry; its diagonal is ignored.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> Y = mojolearn.SpectralEmbedding(n_components=2).fit_transform(X)
__init__(n_components=2, *, affinity='nearest_neighbors', random_state=None, n_neighbors=None, gamma=None, eigen_solver=None, eigen_tol=None, n_jobs=None, verbose=False)[source]#

Methods

fit(X[, y])

Compute the embedding of X.

fit_transform(X[, y])

Fit to X and return the embedding.

fit(X, y=None)[source]#

Compute the embedding of X.

Parameters:
  • X (array-like of shape (n_samples, n_features) or (n_samples, n_samples)) – Training data, or a square affinity ("precomputed") or distance ("precomputed_nearest_neighbors") matrix, dense or sparse. Values must be finite.

  • y (ignored)

Returns:

self (SpectralEmbedding) – The fitted estimator.

Raises:

ValueError – If X is not finite, a precomputed matrix is not square or has a negative or repeated entry, or n_components needs as many eigenpairs as there are samples.

fit_transform(X, y=None)[source]#

Fit to X and return the embedding.

Parameters:
  • X (array-like) – As in fit.

  • y (ignored)

Returns:

embedding (Array of shape (n_samples, n_components), float32) – Same as embedding_.