def spectral_embedding(geom, n_components=<NUM_LIT:8>, eigen_solver='<STR_LIT>',<EOL>random_state=None, drop_first=True,<EOL>diffusion_maps = False, diffusion_time = <NUM_LIT:0>, solver_kwds = None):
random_state = check_random_state(random_state)<EOL>if geom.affinity_matrix is None:<EOL><INDENT>geom.compute_affinity_matrix()<EOL><DEDENT>if not _graph_is_connected(geom.affinity_matrix):<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>if geom.laplacian_matrix is None:<EOL><INDENT>laplacian = geom.compute_laplacian_matrix(copy=False,<EOL>return_lapsym=True)<EOL><DEDENT>else:<EOL><INDENT>laplacian = geom.laplacian_matrix<EOL><DEDENT>n_nodes = laplacian.shape[<NUM_LIT:0>]<EOL>lapl_type = geom.laplacian_method<EOL>eigen_solver, solver_kwds = check_eigen_solver(eigen_solver,solver_kwds,<EOL>size=laplacian.shape[<NUM_LIT:0>],<EOL>nvec=n_components + <NUM_LIT:1>)<EOL>re_normalize = False<EOL>PD_solver = False<EOL>if eigen_solver in ['<STR_LIT>', '<STR_LIT>']: <EOL><INDENT>epsilon = <NUM_LIT:2><EOL>PD_solver = True<EOL>if lapl_type not in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>re_normalize = True<EOL>if geom.laplacian_weights is None: <EOL><INDENT>geom.compute_laplacian_matrix(copy = False, return_lapsym = True)<EOL><DEDENT>w = np.array(geom.laplacian_weights)<EOL>symmetrized_laplacian = geom.laplacian_symmetric.copy()<EOL>if sparse.isspmatrix(symmetrized_laplacian):<EOL><INDENT>symmetrized_laplacian.data /= np.sqrt(w[symmetrized_laplacian.row])<EOL>symmetrized_laplacian.data /= np.sqrt(w[symmetrized_laplacian.col])<EOL>symmetrized_laplacian = (<NUM_LIT:1>+epsilon)*sparse.identity(n_nodes) - symmetrized_laplacian<EOL><DEDENT>else:<EOL><INDENT>symmetrized_laplacian /= np.sqrt(w)<EOL>symmetrized_laplacian /= np.sqrt(w[:,np.newaxis])<EOL>symmetrized_laplacian = (<NUM_LIT:1>+epsilon)*np.identity(n_nodes) - symmetrized_laplacian<EOL><DEDENT><DEDENT>else: <EOL><INDENT>symmetrized_laplacian = geom.laplacian_matrix.copy()<EOL>if sparse.isspmatrix(symmetrized_laplacian):<EOL><INDENT>symmetrized_laplacian = (<NUM_LIT:1>+epsilon)*sparse.identity(n_nodes) - symmetrized_laplacian<EOL><DEDENT>else:<EOL><INDENT>symmetrized_laplacian = (<NUM_LIT:1>+epsilon)*np.identity(n_nodes) - symmetrized_laplacian<EOL><DEDENT><DEDENT><DEDENT>if PD_solver: <EOL><INDENT>lambdas, diffusion_map = eigen_decomposition(symmetrized_laplacian, n_components+<NUM_LIT:1>, eigen_solver=eigen_solver,<EOL>random_state=random_state, drop_first=drop_first, largest = False,<EOL>solver_kwds=solver_kwds)<EOL>lambdas = -lambdas + epsilon<EOL><DEDENT>else:<EOL><INDENT>lambdas, diffusion_map = eigen_decomposition(laplacian, n_components+<NUM_LIT:1>, eigen_solver=eigen_solver,<EOL>random_state=random_state, drop_first=drop_first, largest = True,<EOL>solver_kwds=solver_kwds)<EOL><DEDENT>if re_normalize:<EOL><INDENT>diffusion_map /= np.sqrt(w[:, np.newaxis]) <EOL>diffusion_map /= np.linalg.norm(diffusion_map, axis = <NUM_LIT:0>) <EOL><DEDENT>ind = np.argsort(lambdas); ind = ind[::-<NUM_LIT:1>]<EOL>lambdas = lambdas[ind]; lambdas[<NUM_LIT:0>] = <NUM_LIT:0><EOL>diffusion_map = diffusion_map[:, ind]<EOL>eigenvalues = lambdas.copy()<EOL>eigenvectors = diffusion_map.copy()<EOL>if diffusion_maps:<EOL><INDENT>diffusion_map = compute_diffusion_maps(lapl_type, diffusion_map, lambdas, diffusion_time)<EOL><DEDENT>if drop_first:<EOL><INDENT>embedding = diffusion_map[:, <NUM_LIT:1>:(n_components+<NUM_LIT:1>)]<EOL>eigenvectors = eigenvectors[:, <NUM_LIT:1>:(n_components+<NUM_LIT:1>)]<EOL>eigenvalues = eigenvalues[<NUM_LIT:1>:(n_components+<NUM_LIT:1>)]<EOL><DEDENT>else:<EOL><INDENT>embedding = diffusion_map[:, :n_components]<EOL>eigenvectors = eigenvectors[:, :(n_components)]<EOL>eigenvalues = eigenvalues[:(n_components)]<EOL><DEDENT>return embedding, eigenvalues, eigenvectors<EOL>
Project the sample on the first eigen vectors of the graph Laplacian.

The adjacency matrix is used to compute a normalized graph Laplacian
whose  principal eigenvectors (associated to the
smallest eigen values) represent the embedding coordinates of the data.

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).
The Laplacian must be symmetric so that the eigen vector decomposition works as expected.
This is ensured by the default setting (for more details,
see the documentation in geometry.py).

The data and generic geometric parameters are passed via a Geometry object, which also
computes the Laplacian. By default, the 'geometric' Laplacian (or "debiased", or "renormalized" with
alpha=1) is used. This is the Laplacian construction defined in [Coifman and Lafon, 2006] (see also
documentation in laplacian.py). Thus, with diffusion_maps=False, spectral embedding is a modification
of the Laplacian Eigenmaps algorithm of [Belkin and Nyiogi, 2002], with diffusion_maps=False, geom.laplacian_method
='symmetricnormalized' it is exactly the Laplacian Eigenmaps, with diffusion_maps=True, diffusion_time>0 it
is the Diffusion Maps algorithm of [Coifman and Lafon 2006]; diffusion_maps=True and diffusion_time=0 is the same
as diffusion_maps=False and default geom.laplacian_method.

Parameters
----------
geom : a Geometry object from megaman.embedding.geometry
n_components : integer, optional
    The dimension of the projection subspace.
eigen_solver : {'auto', 'dense', 'arpack', 'lobpcg', or 'amg'}
    'auto' :
        algorithm will attempt to choose the best method for input data
    'dense' :
        use standard dense matrix operations for the eigenvalue decomposition.
        For this method, M must be an array or matrix type.  This method should be avoided for large problems.
    'arpack' :
        use arnoldi iteration in shift-invert mode. For this method,
        M may be a dense matrix, sparse matrix, or general linear operator.
        Warning: ARPACK can be unstable for some problems.  It is best to
        try several random seeds in order to check results.
    'lobpcg' :
        Locally Optimal Block Preconditioned Conjugate Gradient Method.
        A preconditioned eigensolver for large symmetric positive definite
        (SPD) generalized eigenproblems.
    'amg' :
        AMG requires pyamg to be installed. It can be faster on very large,
        sparse problems, but may also lead to instabilities.
random_state : int seed, RandomState instance, or None (default)
    A pseudo random number generator used for the initialization of the
    lobpcg eigen vectors decomposition when eigen_solver == 'amg'.
    By default, arpack is used.
drop_first : bool, optional, 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.
diffusion_map : boolean, optional. Whether to return the diffusion map
    version by re-scaling the embedding coordinate by the eigenvalues to the power
    diffusion_time.
diffusion_time: if diffusion_map=True, the eigenvectors of the Laplacian are rescaled by
    (1-lambda)^diffusion_time, where lambda is the corresponding eigenvalue.
    diffusion_time has the role of scale parameter. One of the main ideas of diffusion framework is
    that running the diffusion forward in time (taking larger and larger
    powers of the Laplacian/transition matrix) reveals the geometric structure of X at larger and
    larger scales (the diffusion process).
    diffusion_time = 0 empirically provides a reasonable balance from a clustering
    perspective. Specifically, the notion of a cluster in the data set
    is quantified as a region in which the probability of escaping this
    region is low (within a certain time t).
    Credit to Satrajit Ghosh (http://satra.cogitatum.org/) for description
solver_kwds : any additional keyword arguments to pass to the selected eigen_solver

Returns
-------
embedding : array, shape=(n_samples, n_components)
    The reduced samples.

Notes
-----
Spectral embedding 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
----------
* http://en.wikipedia.org/wiki/LOBPCG

* Toward the Optimal Preconditioned Eigensolver: Locally Optimal
  Block Preconditioned Conjugate Gradient Method
  Andrew V. Knyazev
  http://dx.doi.org/10.1137%2FS1064827500366124