#NEWLINE# def spectral_embedding( geom , n_components = 8 , eigen_solver = 'auto' , random_state = None , eigen_tol = 0.0 , drop_first = True , diffusion_maps = False ) : #NEWLINE# #INDENT# "\n    Project the sample on the first eigen vectors of the graph Laplacian.\n\n    The adjacency matrix is used to compute a normalized graph Laplacian\n    whose spectrum (especially the eigen vectors associated to the\n    smallest eigen values) has an interpretation in terms of minimal\n    number of cuts necessary to split the graph into comparably sized\n    components.\n\n    This embedding can also 'work' even if the ``adjacency`` variable is\n    not strictly the adjacency matrix of a graph but more generally\n    an affinity or similarity matrix between samples (for instance the\n    heat kernel of a euclidean distance matrix or a k-NN matrix).\n\n    However care must taken to always make the affinity matrix symmetric\n    so that the eigen vector decomposition works as expected.\n\n    Parameters\n    ----------\n    geom : a Geometry object from megaman.embedding.geometry\n    n_components : integer, optional\n        The dimension of the projection subspace.\n    eigen_solver : {'auto', 'dense', 'arpack', 'lobpcg', or 'amg'}\n        'auto' :\n            algorithm will attempt to choose the best method for input data\n        'dense' :\n            use standard dense matrix operations for the eigenvalue decomposition.\n            For this method, M must be an array or matrix type.  This method should be avoided for large problems.\n        'arpack' :\n            use arnoldi iteration in shift-invert mode. For this method,\n            M may be a dense matrix, sparse matrix, or general linear operator.\n            Warning: ARPACK can be unstable for some problems.  It is best to\n            try several random seeds in order to check results.\n        'lobpcg' :\n            Locally Optimal Block Preconditioned Conjugate Gradient Method.\n            A preconditioned eigensolver for large symmetric positive definite\n            (SPD) generalized eigenproblems.\n        'amg' :\n            AMG requires pyamg to be installed. It can be faster on very large,\n            sparse problems, but may also lead to instabilities.\n    random_state : int seed, RandomState instance, or None (default)\n        A pseudo random number generator used for the initialization of the\n        lobpcg eigen vectors decomposition when eigen_solver == 'amg'.\n        By default, arpack is used.\n    eigen_tol : float, optional, default=0.0\n        Stopping criterion for eigendecomposition of the Laplacian matrix\n        when using arpack eigen_solver.\n    drop_first : bool, optional, default=True\n        Whether to drop the first eigenvector. For spectral embedding, this\n        should be True as the first eigenvector should be constant vector for\n        connected graph, but for spectral clustering, this should be kept as\n        False to retain the first eigenvector.\n    diffusion_map : boolean, optional. Whether to return the diffusion map\n        version by re-scaling the embedding by the eigenvalues.\n\n    Returns\n    -------\n    embedding : array, shape=(n_samples, n_components)\n        The reduced samples.\n\n    Notes\n    -----\n    Spectral embedding is most useful when the graph has one connected\n    component. If there graph has many components, the first few eigenvectors\n    will simply uncover the connected components of the graph.\n\n    References\n    ----------\n    * http://en.wikipedia.org/wiki/LOBPCG\n\n    * Toward the Optimal Preconditioned Eigensolver: Locally Optimal\n      Block Preconditioned Conjugate Gradient Method\n      Andrew V. Knyazev\n      http://dx.doi.org/10.1137%2FS1064827500366124\n    " #NEWLINE# random_state = check_random_state ( random_state ) #NEWLINE# if ( geom . affinity_matrix is None ) : #NEWLINE# #INDENT# geom . compute_affinity_matrix ( ) #NEWLINE# #UNINDENT# if ( not _graph_is_connected ( geom . affinity_matrix ) ) : #NEWLINE# #INDENT# warnings . warn ( 'Graph is not fully connected: spectral embedding may not work as expected.' ) #NEWLINE# #UNINDENT# if ( geom . laplacian_matrix is None ) : #NEWLINE# #INDENT# laplacian = geom . compute_laplacian_matrix ( copy = False , return_lapsym = True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# laplacian = geom . laplacian_matrix #NEWLINE# #UNINDENT# n_nodes = laplacian . shape [ 0 ] #NEWLINE# lapl_type = geom . laplacian_method #NEWLINE# eigen_solver = check_eigen_solver ( eigen_solver , size = laplacian . shape [ 0 ] , nvec = ( n_components + 1 ) ) #NEWLINE# re_normalize = False #NEWLINE# PD_solver = False #NEWLINE# if ( eigen_solver in [ 'amg' , 'lobpcg' ] ) : #NEWLINE# #INDENT# epsilon = 2 #NEWLINE# PD_solver = True #NEWLINE# if ( lapl_type not in [ 'symmetricnormalized' , 'unnormalized' ] ) : #NEWLINE# #INDENT# re_normalize = True #NEWLINE# if ( geom . laplacian_weights is None ) : #NEWLINE# #INDENT# geom . compute_laplacian_matrix ( copy = False , return_lapsym = True ) #NEWLINE# #UNINDENT# w = np . array ( geom . laplacian_weights ) #NEWLINE# symmetrized_laplacian = geom . laplacian_symmetric . copy ( ) #NEWLINE# if sparse . isspmatrix ( symmetrized_laplacian ) : #NEWLINE# #INDENT# symmetrized_laplacian . data /= np . sqrt ( w [ symmetrized_laplacian . row ] ) #NEWLINE# symmetrized_laplacian . data /= np . sqrt ( w [ symmetrized_laplacian . col ] ) #NEWLINE# symmetrized_laplacian = ( ( ( 1 + epsilon ) * sparse . identity ( n_nodes ) ) - symmetrized_laplacian ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# symmetrized_laplacian /= np . sqrt ( w ) #NEWLINE# symmetrized_laplacian /= np . sqrt ( w [ : , np . newaxis ] ) #NEWLINE# symmetrixed_laplacian = ( ( ( 1 + epsilon ) * np . identity ( n_nodes ) ) - symmetrized_laplacian ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# symmetrized_laplacian = geom . laplacian_matrix . copy ( ) #NEWLINE# if sparse . isspmatrix ( symmetrized_laplacian ) : #NEWLINE# #INDENT# symmetrized_laplacian = ( ( ( 1 + epsilon ) * sparse . identity ( n_nodes ) ) - symmetrized_laplacian ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# symmetrixed_laplacian = ( ( ( 1 + epsilon ) * np . identity ( n_nodes ) ) - symmetrized_laplacian ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if PD_solver : #NEWLINE# #INDENT# ( lambdas , diffusion_map ) = eigen_decomposition ( symmetrized_laplacian , ( n_components + 1 ) , eigen_solver , random_state , eigen_tol , symmetrixed_laplacian , largest = False ) #NEWLINE# lambdas = ( ( - lambdas ) + epsilon ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ( lambdas , diffusion_map ) = eigen_decomposition ( laplacian , ( n_components + 1 ) , eigen_solver , random_state , eigen_tol , drop_first , largest = True ) #NEWLINE# #UNINDENT# if re_normalize : #NEWLINE# #INDENT# diffusion_map /= np . sqrt ( w [ : , np . newaxis ] ) #NEWLINE# diffusion_map /= np . linalg . norm ( diffusion_map , axis = 0 ) #NEWLINE# #UNINDENT# ind = np . argsort ( lambdas ) #NEWLINE# ind = ind [ : : ( - 1 ) ] #NEWLINE# lambdas = lambdas [ ind ] #NEWLINE# lambdas [ 0 ] = 0 #NEWLINE# diffusion_map = diffusion_map [ : , ind ] #NEWLINE# if diffusion_maps : #NEWLINE# #INDENT# diffusion_map = ( diffusion_map * np . sqrt ( lambdas ) ) #NEWLINE# #UNINDENT# if drop_first : #NEWLINE# #INDENT# embedding = diffusion_map [ : , 1 : ( n_components + 1 ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# embedding = diffusion_map [ : , : n_components ] #NEWLINE# #UNINDENT# return embedding