Skip to content

The eigsh call in spectral embedding is done wrong and differently from what is described in the comments #33242

Closed

Description

@dkobak

Describe the bug and give evidence about its user-facing impact

The way eigsh is run in the spectral embedding routine is suboptimal and not consistent with what is described in the comments there.

Here are the relevant bits from https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/manifold/_spectral_embedding.py#L346

        # Because the normalized Laplacian has eigenvalues between 0 and 2,
        # I - L has eigenvalues between -1 and 1.  ARPACK is most efficient
        ...
        # instead, we'll use ARPACK's shift-invert mode, asking for the
        # eigenvalues near 1.0.
        ...
            laplacian *= -1
            ...
            _, diffusion_map = eigsh(
                laplacian, k=n_components, sigma=1.0, which="LM", tol=tol, v0=v0
            )

The comments say that eigsh is supposed to be run on I - L and the eigenvalues of interest are close to 1, which is why sigma=1.0 is used. This makes sense. However, in the code, the eigsh is run on -L. Of course it has the same eigenvectors, but the relevant eigenvalues are close to 0. So setting sigma=1.0 does not make any sense.

To make the code follow what is described in the comments, one would need to add ones to the diagonal of laplacian prior to running eigsh.

The user-facing impact is that the runtime is slower than it could be (see below).

Steps/Code to Reproduce

import numpy as np
import pylab as plt
from scipy.sparse.linalg import eigsh
from scipy.sparse.csgraph import laplacian
from sklearn.utils._arpack import _init_arpack_v0
from sklearn.neighbors import kneighbors_graph
from sklearn.datasets import make_swiss_roll

X, _ = make_swiss_roll(n_samples=5000)

# These is identical to SpectralEmbedding(n_neighbors=10, eigen_solver="arpack", affinity="nearest_neighbors")
A = kneighbors_graph(X, n_neighbors=10, include_self=True).toarray()
A = (A + A.T) / 2

Lsym, dd = laplacian(A, normed=True, return_diag=True)
v0 = _init_arpack_v0(A.shape[0], 42)

np.fill_diagonal(Lsym, 1)
Lsym *= -1
# The next line is not present in the spectral embedding routine but it should be!
np.fill_diagonal(Lsym, 0)
    
%time eigvals, eigvecs = eigsh(Lsym, k=3, sigma=1, which="LM", tol=0, v0=v0)
eigvecs = eigvecs[:, ::-1]
Z = np.diag(1/dd) @ eigvecs

Expected Results

Runtime 1.5 sec on my machine and the eiganvalues [0.99935026, 0.99984568, 1. ].

Actual Results

Runtime 10.1 sec on my machine and eigenvalues array([-6.55448604e-04, -1.62765707e-04, -1.99840144e-15]).

Versions

System:
    python: 3.14.2 | packaged by conda-forge | (main, Dec  6 2025, 11:21:58) [GCC 14.3.0]
executable: /home/dmitry/anaconda3/envs/nmds/bin/python
   machine: Linux-5.15.0-139-generic-x86_64-with-glibc2.31

Python dependencies:
      sklearn: 1.8.0
          pip: 25.3
   setuptools: 80.10.1
        numpy: 2.3.5
        scipy: 1.16.3
       Cython: None
       pandas: 2.3.3
   matplotlib: 3.10.8
       joblib: 1.5.3
threadpoolctl: 3.6.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
    num_threads: 8
         prefix: libopenblas
       filepath: /home/dmitry/anaconda3/envs/nmds/lib/libopenblasp-r0.3.30.so
        version: 0.3.30
threading_layer: pthreads
   architecture: Haswell

       user_api: openmp
   internal_api: openmp
    num_threads: 8
         prefix: libgomp
       filepath: /home/dmitry/anaconda3/envs/nmds/lib/libgomp.so.1.0.0
        version: None

Interest in fixing the bug

One-line fix.

Activity

  1. added
    Needs Triage
    on Feb 8, 2026
  2. adrinjalali commented on Feb 10, 2026

    @adrinjalali
    Member

    Nice catch! Could you please open a PR with the fix, and make sure you test comparing the PR with main? Should be an easy one to merge if I understand your comments correctly.

  3. removed
    Needs Triage
    on Feb 10, 2026
  4. dkobak commented on Feb 11, 2026

    @dkobak
    ContributorAuthor

    @adrinjalali I opened a minimal PR (#33262) but unexpectedly some tests are failing in some environments (while all passing on my machine), and at the moment I have no idea what may case it or how to debug it :-(

  5. dkobak commented on Feb 12, 2026

    @dkobak
    ContributorAuthor

    Update. I found a discussion of this in comments of a very old PR: #14647 (comment). There @lobpcg also notes that the way eigsh is called makes little sense, and suggests to run eigsh directly on L (not on I - L and not on -L either):

    eigsh(laplacian, k=n_components, sigma=-1e-5, which='LM', tol=eigen_tol, v0=v0)

    Note that he recommends to set sigma=-1e-5 instead of sigma=0 which is recommended by the docs: https://docs.scipy.org/doc/scipy/tutorial/arpack.html.

    This makes sense to me and I will try to implement it in the linked PR.

  6. added a commit that references this issue on Feb 21, 2026
    be25825
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions