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:
objectSpectral 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 notransform: 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 ofX),"precomputed"(Xis a square affinity matrix, dense or sparse),"rbf"(exp(-gamma * ||x_i - x_j||**2)) or"precomputed_nearest_neighbors"(Xis a distance matrix or sparse distance graph; then_neighborsnearest per row, symmetrized as0.5 * (C + C.T)). A callable or any other name raisesValueError.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 means1 / n_features(scikit-learn’s rule). Setting it with another affinity raisesNotImplementedError.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
Xand 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
Xis not finite, a precomputed matrix is not square or has a negative or repeated entry, orn_componentsneeds as many eigenpairs as there are samples.