def spectral_clustering(geom, K, eigen_solver = '<STR_LIT>', random_state = None, solver_kwds = None, <EOL>renormalize = True, stabalize = True, additional_vectors = <NUM_LIT:0>):
<EOL>if geom.affinity_matrix is None:<EOL><INDENT>S = geom.compute_affinity_matrix()<EOL><DEDENT>else:<EOL><INDENT>S = geom.affinity_matrix<EOL><DEDENT>if eigen_solver in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>stabalize = True<EOL><DEDENT>if stabalize:<EOL><INDENT>geom.laplacian_type = '<STR_LIT>'<EOL>return_lapsym = True<EOL><DEDENT>else:<EOL><INDENT>geom.laplacian_type = '<STR_LIT>'<EOL>return_lapsym = False<EOL><DEDENT>P = geom.compute_laplacian_matrix(return_lapsym = return_lapsym)<EOL>P += identity(P.shape[<NUM_LIT:0>])        <EOL>if eigen_solver in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>n_components = <NUM_LIT:2>*int(np.log(P.shape[<NUM_LIT:0>]))*K + <NUM_LIT:1><EOL>n_components += int(additional_vectors)<EOL><DEDENT>else:<EOL><INDENT>n_components = K<EOL><DEDENT>n_components = min(n_components, P.shape[<NUM_LIT:0>])<EOL>(lambdas, eigen_vectors) = eigen_decomposition(P, n_components=n_components, eigen_solver=eigen_solver, <EOL>random_state=random_state, drop_first = True,<EOL>solver_kwds=solver_kwds)<EOL>if eigen_solver in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if np.abs(lambdas[<NUM_LIT:0>] - <NUM_LIT:1>) > <NUM_LIT>:<EOL><INDENT>warnings.warn("<STR_LIT>")<EOL><DEDENT><DEDENT>eigen_vectors = eigen_vectors[:, <NUM_LIT:1>:K]<EOL>lambdas = lambdas[<NUM_LIT:1>:K]<EOL>if stabalize:<EOL><INDENT>w = np.array(geom.laplacian_weights)<EOL>eigen_vectors /= np.sqrt(w[:,np.newaxis])<EOL>eigen_vectors /= np.linalg.norm(eigen_vectors, axis = <NUM_LIT:0>)    <EOL><DEDENT>if renormalize:<EOL><INDENT>norms = np.linalg.norm(eigen_vectors, axis=<NUM_LIT:1>)<EOL>eigen_vectors /= norms[:,np.newaxis]<EOL><DEDENT>labels =  k_means_clustering(eigen_vectors,K)    <EOL>return labels, eigen_vectors, P<EOL>
Spectral clustering for find K clusters by using the eigenvectors of a 
matrix which is derived from a set of similarities S.

Parameters
-----------
S: array-like,shape(n_sample,n_sample)
    similarity matrix 
K: integer
    number of K clusters
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 : numpy.RandomState or int, optional
    The generator or seed used to determine the starting vector for arpack
    iterations.  Defaults to numpy.random.RandomState    
solver_kwds : any additional keyword arguments to pass to the selected eigen_solver 
renormalize : (bool) whether or not to set the rows of the eigenvectors to have norm 1 
             this can improve label quality
stabalize : (bool) whether or not to compute the (more stable) eigenvectors of L = D^-1/2*S*D^-1/2
            instead of P = D^-1*S 
additional_vectors : (int) compute additional eigen vectors when computing eigen decomposition.
    When eigen_solver = 'amg' or 'lopcg' often if a small number of eigen values is sought the
    largest eigenvalue returned is *not* equal to 1 (it should be). This can usually be fixed
    by requesting more than K eigenvalues until the first eigenvalue is close to 1 and then
    omitted. The remaining K-1 eigenvectors should be informative. 
Returns
-------
labels: array-like, shape (1,n_samples)