code: def spectral_embedding(
      adjacency,
      *,
      n_components=8,
      eigen_solver=None,
      random_state=None,
      eigen_tol="auto",
      norm_laplacian=True,
      drop_first=True,
  ):
      """Project the sample on the first eigenvectors of the graph Laplacian.
  
      The adjacency matrix is used to compute a normalized graph Laplacian
      whose spectrum (especially the eigenvectors associated to the
      smallest eigenvalues) has an interpretation in terms of minimal
      number of cuts necessary to split the graph into comparably sized
      components.
  
      This embedding can also 'work' even if the ``adjacency`` variable is
      not strictly the adjacency matrix of a graph but more generally
      an affinity or similarity matrix between samples (for instance the
      heat kernel of a euclidean distance matrix or a k-NN matrix).
  
      However care must taken to always make the affinity matrix symmetric
      so that the eigenvector decomposition works as expected.
  
      Note : Laplacian Eigenmaps is the actual algorithm implemented here.
  
      Read more in the :ref:`User Guide <spectral_embedding>`.
  
      Parameters
      ----------
      adjacency : {array-like, sparse graph} of shape (n_samples, n_samples)
          The adjacency matrix of the graph to embed.
  
      n_components : int, default=8
          The dimension of the projection subspace.
  
      eigen_solver : {'arpack', 'lobpcg', 'amg'}, default=None
          The eigenvalue decomposition strategy to use. AMG requires pyamg
          to be installed. It can be faster on very large, sparse problems,
          but may also lead to instabilities. If None, then ``'arpack'`` is
          used.
  
      random_state : int, RandomState instance or None, default=None
          A pseudo random number generator used for the initialization
          of the lobpcg eigen vectors decomposition when `eigen_solver ==
          'amg'`, and for the K-Means initialization. Use an int to make
          the results deterministic across calls (See
          :term:`Glossary <random_state>`).
  
          .. note::
              When using `eigen_solver == 'amg'`,
              it is necessary to also fix the global numpy seed with
              `np.random.seed(int)` to get deterministic results. See
              https://github.com/pyamg/pyamg/issues/139 for further
              information.
  
      eigen_tol : float, default="auto"
          Stopping criterion for eigendecomposition of the Laplacian matrix.
          If `eigen_tol="auto"` then the passed tolerance will depend on the
          `eigen_solver`:
  
          - If `eigen_solver="arpack"`, then `eigen_tol=0.0`;
          - If `eigen_solver="lobpcg"` or `eigen_solver="amg"`, then
            `eigen_tol=None` which configures the underlying `lobpcg` solver to
            automatically resolve the value according to their heuristics. See,
            :func:`scipy.sparse.linalg.lobpcg` for details.
  
          Note that when using `eigen_solver="amg"` values of `tol<1e-5` may lead
          to convergence issues and should be avoided.
  
          .. versionadded:: 1.2
             Added 'auto' option.
  
      norm_laplacian : bool, default=True
          If True, then compute symmetric normalized Laplacian.
  
      drop_first : bool, default=True
          Whether to drop the first eigenvector. For spectral embedding, this
          should be True as the first eigenvector should be constant vector for
          connected graph, but for spectral clustering, this should be kept as
          False to retain the first eigenvector.
  
      Returns
      -------
      embedding : ndarray of shape (n_samples, n_components)
          The reduced samples.
  
      Notes
      -----
      Spectral Embedding (Laplacian Eigenmaps) is most useful when the graph
      has one connected component. If there graph has many components, the first
      few eigenvectors will simply uncover the connected components of the graph.
  
      References
      ----------
      * https://en.wikipedia.org/wiki/LOBPCG
  
      * :doi:`"Toward the Optimal Preconditioned Eigensolver: Locally Optimal
        Block Preconditioned Conjugate Gradient Method",
        Andrew V. Knyazev
        <10.1137/S1064827500366124>`
      """
      adjacency = check_symmetric(adjacency)
  
      try:
          from pyamg import smoothed_aggregation_solver
      except ImportError as e:
          if eigen_solver == "amg":
              raise ValueError(
                  "The eigen_solver was set to 'amg', but pyamg is not available."
              ) from e
  
      if eigen_solver is None:
          eigen_solver = "arpack"
      elif eigen_solver not in ("arpack", "lobpcg", "amg"):
          raise ValueError(
              "Unknown value for eigen_solver: '%s'."
              "Should be 'amg', 'arpack', or 'lobpcg'" % eigen_solver
          )
  
      random_state = check_random_state(random_state)
  
      n_nodes = adjacency.shape[0]
      # Whether to drop the first eigenvector
      if drop_first:
          n_components = n_components + 1
  
      if not _graph_is_connected(adjacency):
          warnings.warn(
              "Graph is not fully connected, spectral embedding may not work as expected."
          )
  
      laplacian, dd = csgraph_laplacian(
          adjacency, normed=norm_laplacian, return_diag=True
      )
      if (
          eigen_solver == "arpack"
          or eigen_solver != "lobpcg"
          and (
              not sparse.issparse(laplacian)
              or n_nodes < 5 * n_components
          )
      ):
          # lobpcg used with eigen_solver='amg' has bugs for low number of nodes
          # for details see the source code in scipy:
          # https://github.com/scipy/scipy/blob/v0.11.0/scipy/sparse/linalg/eigen
          # /lobpcg/lobpcg.py#L237
          # or matlab:
          # https://www.mathworks.com/matlabcentral/fileexchange/48-lobpcg-m
          laplacian = _set_diag(laplacian, 1, norm_laplacian)
  
          # Here we'll use shift-invert mode for fast eigenvalues
          # (see https://docs.scipy.org/doc/scipy/reference/tutorial/arpack.html
          #  for a short explanation of what this means)
          # Because the normalized Laplacian has eigenvalues between 0 and 2,
          # I - L has eigenvalues between -1 and 1.  ARPACK is most efficient
          # when finding eigenvalues of largest magnitude (keyword which='LM')
          # and when these eigenvalues are very large compared to the rest.
          # For very large, very sparse graphs, I - L can have many, many
          # eigenvalues very near 1.0.  This leads to slow convergence.  So
          # instead, we'll use ARPACK's shift-invert mode, asking for the
          # eigenvalues near 1.0.  This effectively spreads-out the spectrum
          # near 1.0 and leads to much faster convergence: potentially an
          # orders-of-magnitude speedup over simply using keyword which='LA'
          # in standard mode.
          try:
              # We are computing the opposite of the laplacian inplace so as
              # to spare a memory allocation of a possibly very large array
              tol = 0 if eigen_tol == "auto" else eigen_tol
              laplacian *= -1
              v0 = _init_arpack_v0(laplacian.shape[0], random_state)
              _, diffusion_map = eigsh(
                  laplacian,
                  k=n_components,
                  sigma=1.0,
                  which="LM",
                  tol=tol,
                  v0=v0,
              )
              embedding = diffusion_map.T[n_components::-1]
              if norm_laplacian:
                  # recover u = D^-1/2 x from the eigenvector output x
                  embedding = embedding / dd
          except RuntimeError:
              # When submatrices are exactly singular, an LU decomposition
              # in arpack fails. We fallback to lobpcg
              eigen_solver = "lobpcg"
              # Revert the laplacian to its opposite to have lobpcg work
              laplacian *= -1
  
      elif eigen_solver == "amg":
          # Use AMG to get a preconditioner and speed up the eigenvalue
          # problem.
          if not sparse.issparse(laplacian):
              warnings.warn("AMG works better for sparse matrices")
          laplacian = check_array(
              laplacian,
              dtype=[np.float64, np.float32],
              accept_sparse=True,
          )
          laplacian = _set_diag(laplacian, 1, norm_laplacian)
  
          # The Laplacian matrix is always singular, having at least one zero
          # eigenvalue, corresponding to the trivial eigenvector, which is a
          # constant. Using a singular matrix for preconditioning may result in
          # random failures in LOBPCG and is not supported by the existing
          # theory:
          #     see https://doi.org/10.1007/s10208-015-9297-1
          # Shift the Laplacian so its diagononal is not all ones. The shift
          # does change the eigenpairs however, so we'll feed the shifted
          # matrix to the solver and afterward set it back to the original.
          diag_shift = 1e-5 * sparse.eye(laplacian.shape[0])
          laplacian += diag_shift
          ml = smoothed_aggregation_solver(
              check_array(laplacian, accept_sparse="csr")
          )
          laplacian -= diag_shift
  
          M = ml.aspreconditioner()
          # Create initial approximation X to eigenvectors
          X = random_state.standard_normal(
              size=(laplacian.shape[0], n_components + 1)
          )
          X[:, 0] = dd.ravel()
          X = X.astype(laplacian.dtype)
  
          tol = None if eigen_tol == "auto" else eigen_tol
          _, diffusion_map = lobpcg(
              laplacian, X, M=M, tol=tol, largest=False
          )
          embedding = diffusion_map.T
          if norm_laplacian:
              # recover u = D^-1/2 x from the eigenvector output x
              embedding = embedding / dd
          if embedding.shape[0] == 1:
              raise ValueError
  
      if eigen_solver == "lobpcg":
          laplacian = check_array(
              laplacian,
              dtype=[np.float64, np.float32],
              accept_sparse=True,
          )
          if n_nodes < 5 * n_components + 1:
              # see note above under arpack why lobpcg has problems with small
              # number of nodes
              # lobpcg will fallback to eigh, so we short circuit it
              if sparse.issparse(laplacian):
                  laplacian = laplacian.toarray()
              _, diffusion_map = eigh(laplacian, check_finite=False)
              embedding = diffusion_map.T[:n_components]
              if norm_laplacian:
                  # recover u = D^-1/2 x from the eigenvector output x
                  embedding = embedding / dd
          else:
              laplacian = _set_diag(laplacian, 1, norm_laplacian)
              # We increase the number of eigenvectors requested, as lobpcg
              # doesn't behave well in low dimension and create initial
              # approximation X to eigenvectors
              X = random_state.standard_normal(
                  size=(laplacian.shape[0], n_components + 1)
              )
              X[:, 0] = dd.ravel()
              X = X.astype(laplacian.dtype)
              tol = None if eigen_tol == "auto" else eigen_tol
              _, diffusion_map = lobpcg(
                  laplacian, X, tol=tol, largest=False, maxiter=2000
              )
              embedding = diffusion_map.T[:n_components]
              if norm_laplacian:
                  # recover u = D^-1/2 x from the eigenvector output x
                  embedding = embedding / dd
              if embedding.shape[0] == 1:
                  raise ValueError
  
      embedding = _deterministic_vector_sign_flip(embedding)
      if drop_first:
          return embedding[1:n_components].T
      else:
          return embedding[:n_components].T
  
imports: import warnings
  from numbers import Integral, Real
  import numpy
  from scipy import sparse
  from scipy.linalg import eigh
  from scipy.sparse.csgraph import connected_components
  from scipy.sparse.csgraph import laplacian
  from scipy.sparse.linalg import eigsh, lobpcg
  from ..base import BaseEstimator, _fit_context
  from ..metrics.pairwise import rbf_kernel
  from ..neighbors import NearestNeighbors, kneighbors_graph
  from ..utils import check_array, check_random_state, check_symmetric
  from ..utils._arpack import _init_arpack_v0
  from ..utils._param_validation import Interval, StrOptions
  from ..utils.extmath import _deterministic_vector_sign_flip
label_desc: Use image node_id 4 for calling a global function with example usage: spectral_embedding(adjacency) and returns: embedding, embedding