Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn SpectralEmbedding

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Dimensionality Reduction
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for spectral embedding for non-linear dimensionality reduction provided by scikit-learn.

Description

This module implements Spectral Embedding (also known as Laplacian Eigenmaps) for non-linear dimensionality reduction. It first constructs an affinity matrix using either a nearest-neighbor graph with RBF kernel, a precomputed affinity matrix, or a callable affinity function. Then it computes the graph Laplacian and finds its eigenvectors to produce the embedding. The module includes helper functions for computing connected components and a standalone spectral_embedding function.

Usage

Use SpectralEmbedding for non-linear dimensionality reduction when you want to preserve local neighborhood structure. It is closely related to spectral clustering and is useful for visualizing data that lies on a low-dimensional manifold within a high-dimensional space.

Code Reference

Source Location

Signature

class SpectralEmbedding(BaseEstimator):
    """Spectral embedding for non-linear dimensionality reduction."""

    def __init__(
        self,
        n_components=2,
        *,
        affinity="nearest_neighbors",
        gamma=None,
        random_state=None,
        eigen_solver=None,
        eigen_tol="auto",
        n_neighbors=None,
        n_jobs=None,
    ):
        ...

def spectral_embedding(
    adjacency,
    *,
    n_components=8,
    eigen_solver=None,
    random_state=None,
    eigen_tol="auto",
    norm_laplacian=True,
    drop_first=True,
):
    ...

Import

from sklearn.manifold import SpectralEmbedding
from sklearn.manifold import spectral_embedding

I/O Contract

Inputs

Name Type Required Description
X array-like of shape (n_samples, n_features) or (n_samples, n_samples) Yes Data or precomputed affinity matrix
n_components int No Target dimensionality (default: 2)
affinity str or callable No Affinity type: 'nearest_neighbors', 'rbf', 'precomputed', 'precomputed_nearest_neighbors', or callable (default: 'nearest_neighbors')
gamma float or None No Kernel coefficient for RBF kernel (default: None)
n_neighbors int or None No Number of neighbors for nearest_neighbors affinity (default: None)
eigen_solver str or None No Eigenvalue solver: 'arpack', 'lobpcg', 'amg', or None (default: None)
random_state int or None No Random state for initialization (default: None)

Outputs

Name Type Description
embedding_ ndarray of shape (n_samples, n_components) Spectral embedding of training data
affinity_matrix_ ndarray of shape (n_samples, n_samples) Computed affinity matrix
n_neighbors_ int Number of neighbors used

Usage Examples

Basic Usage

from sklearn.manifold import SpectralEmbedding
from sklearn.datasets import make_moons

# Generate non-linear data
X, y = make_moons(n_samples=200, noise=0.05, random_state=42)

# Apply Spectral Embedding
se = SpectralEmbedding(n_components=2, affinity='nearest_neighbors',
                       n_neighbors=10, random_state=42)
X_embedded = se.fit_transform(X)
print("Embedded shape:", X_embedded.shape)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment