<s> from nose . tools import assert_true <EOL> from nose . tools import assert_equal <EOL> from scipy . sparse import csr_matrix <EOL> from scipy . sparse import csc_matrix <EOL> from scipy . sparse import coo_matrix <EOL> from scipy . linalg import eigh <EOL> import numpy as np <EOL> from numpy . testing import assert_array_almost_equal <EOL> from numpy . testing import assert_array_equal <EOL> from nose . tools import assert_raises <EOL> from nose . plugins . skip import SkipTest <EOL> from sklearn . manifold . spectral_embedding_ import SpectralEmbedding <EOL> from sklearn . manifold . spectral_embedding_ import _graph_is_connected <EOL> from sklearn . manifold . spectral_embedding_ import _graph_connected_component <EOL> from sklearn . manifold import spectral_embedding <EOL> from sklearn . metrics . pairwise import rbf_kernel <EOL> from sklearn . metrics import normalized_mutual_info_score <EOL> from sklearn . cluster import KMeans <EOL> from sklearn . datasets . samples_generator import make_blobs <EOL> from sklearn . utils . graph import graph_laplacian <EOL> from sklearn . utils . extmath import _deterministic_vector_sign_flip <EOL> centers = np . array ( [ <EOL> [ <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , <EOL> [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , <EOL> [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT:1.0> ] , <EOL> ] ) <EOL> n_samples = <NUM_LIT:1000> <EOL> n_clusters , n_features = centers . shape <EOL> S , true_labels = make_blobs ( n_samples = n_samples , centers = centers , <EOL> cluster_std = <NUM_LIT:1.> , random_state = <NUM_LIT> ) <EOL> def _check_with_col_sign_flipping ( A , B , tol = <NUM_LIT:0.0> ) : <EOL> """<STR_LIT>""" <EOL> sign = True <EOL> for column_idx in range ( A . shape [ <NUM_LIT:1> ] ) : <EOL> sign = sign and ( ( ( ( A [ : , column_idx ] - <EOL> B [ : , column_idx ] ) ** <NUM_LIT:2> ) . mean ( ) <= tol ** <NUM_LIT:2> ) or <EOL> ( ( ( A [ : , column_idx ] + <EOL> B [ : , column_idx ] ) ** <NUM_LIT:2> ) . mean ( ) <= tol ** <NUM_LIT:2> ) ) <EOL> if not sign : <EOL> return False <EOL> return True <EOL> def test_sparse_graph_connected_component ( ) : <EOL> rng = np . random . RandomState ( <NUM_LIT> ) <EOL> n_samples = <NUM_LIT> <EOL> boundaries = [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:200> , n_samples ] <EOL> p = rng . permutation ( n_samples ) <EOL> connections = [ ] <EOL> for start , stop in zip ( boundaries [ : - <NUM_LIT:1> ] , boundaries [ <NUM_LIT:1> : ] ) : <EOL> group = p [ start : stop ] <EOL> for i in range ( len ( group ) - <NUM_LIT:1> ) : <EOL> connections . append ( ( group [ i ] , group [ i + <NUM_LIT:1> ] ) ) <EOL> min_idx , max_idx = <NUM_LIT:0> , len ( group ) - <NUM_LIT:1> <EOL> n_random_connections = <NUM_LIT:1000> <EOL> source = rng . randint ( min_idx , max_idx , size = n_random_connections ) <EOL> target = rng . randint ( min_idx , max_idx , size = n_random_connections ) <EOL> connections . extend ( zip ( group [ source ] , group [ target ] ) ) <EOL> row_idx , column_idx = tuple ( np . array ( connections ) . T ) <EOL> data = rng . uniform ( <NUM_LIT> , <NUM_LIT> , size = len ( connections ) ) <EOL> affinity = coo_matrix ( ( data , ( row_idx , column_idx ) ) ) <EOL> affinity = <NUM_LIT:0.5> * ( affinity + affinity . T ) <EOL> for start , stop in zip ( boundaries [ : - <NUM_LIT:1> ] , boundaries [ <NUM_LIT:1> : ] ) : <EOL> component_1 = _graph_connected_component ( affinity , p [ start ] ) <EOL> component_size = stop - start <EOL> assert_equal ( component_1 . sum ( ) , component_size ) <EOL> component_2 = _graph_connected_component ( affinity , p [ stop - <NUM_LIT:1> ] ) <EOL> assert_equal ( component_2 . sum ( ) , component_size ) <EOL> assert_array_equal ( component_1 , component_2 ) <EOL> def test_spectral_embedding_two_components ( seed = <NUM_LIT> ) : <EOL> random_state = np . random . RandomState ( seed ) <EOL> n_sample = <NUM_LIT:100> <EOL> affinity = np . zeros ( shape = [ n_sample * <NUM_LIT:2> , n_sample * <NUM_LIT:2> ] ) <EOL> affinity [ <NUM_LIT:0> : n_sample , <EOL> <NUM_LIT:0> : n_sample ] = np . abs ( random_state . randn ( n_sample , n_sample ) ) + <NUM_LIT:2> <EOL> affinity [ n_sample : : , <EOL> n_sample : : ] = np . abs ( random_state . randn ( n_sample , n_sample ) ) + <NUM_LIT:2> <EOL> component = _graph_connected_component ( affinity , <NUM_LIT:0> ) <EOL> assert_true ( component [ : n_sample ] . all ( ) ) <EOL> assert_true ( not component [ n_sample : ] . any ( ) ) <EOL> component = _graph_connected_component ( affinity , - <NUM_LIT:1> ) <EOL> assert_true ( not component [ : n_sample ] . any ( ) ) <EOL> assert_true ( component [ n_sample : ] . all ( ) ) <EOL> affinity [ <NUM_LIT:0> , n_sample + <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> affinity [ n_sample + <NUM_LIT:1> , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> affinity . flat [ : : <NUM_LIT:2> * n_sample + <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> affinity = <NUM_LIT:0.5> * ( affinity + affinity . T ) <EOL> true_label = np . zeros ( shape = <NUM_LIT:2> * n_sample ) <EOL> true_label [ <NUM_LIT:0> : n_sample ] = <NUM_LIT:1> <EOL> se_precomp = SpectralEmbedding ( n_components = <NUM_LIT:1> , affinity = "<STR_LIT>" , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> embedded_coordinate = se_precomp . fit_transform ( affinity ) <EOL> embedded_coordinate = se_precomp . fit_transform ( affinity . astype ( np . float32 ) ) <EOL> label_ = np . array ( embedded_coordinate . ravel ( ) < <NUM_LIT:0> , dtype = "<STR_LIT:float>" ) <EOL> assert_equal ( normalized_mutual_info_score ( true_label , label_ ) , <NUM_LIT:1.0> ) <EOL> def test_spectral_embedding_precomputed_affinity ( seed = <NUM_LIT> ) : <EOL> gamma = <NUM_LIT:1.0> <EOL> se_precomp = SpectralEmbedding ( n_components = <NUM_LIT:2> , affinity = "<STR_LIT>" , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> se_rbf = SpectralEmbedding ( n_components = <NUM_LIT:2> , affinity = "<STR_LIT>" , <EOL> gamma = gamma , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> embed_precomp = se_precomp . fit_transform ( rbf_kernel ( S , gamma = gamma ) ) <EOL> embed_rbf = se_rbf . fit_transform ( S ) <EOL> assert_array_almost_equal ( <EOL> se_precomp . affinity_matrix_ , se_rbf . affinity_matrix_ ) <EOL> assert_true ( _check_with_col_sign_flipping ( embed_precomp , embed_rbf , <NUM_LIT> ) ) <EOL> def test_spectral_embedding_callable_affinity ( seed = <NUM_LIT> ) : <EOL> gamma = <NUM_LIT> <EOL> kern = rbf_kernel ( S , gamma = gamma ) <EOL> se_callable = SpectralEmbedding ( n_components = <NUM_LIT:2> , <EOL> affinity = ( <EOL> lambda x : rbf_kernel ( x , gamma = gamma ) ) , <EOL> gamma = gamma , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> se_rbf = SpectralEmbedding ( n_components = <NUM_LIT:2> , affinity = "<STR_LIT>" , <EOL> gamma = gamma , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> embed_rbf = se_rbf . fit_transform ( S ) <EOL> embed_callable = se_callable . fit_transform ( S ) <EOL> assert_array_almost_equal ( <EOL> se_callable . affinity_matrix_ , se_rbf . affinity_matrix_ ) <EOL> assert_array_almost_equal ( kern , se_rbf . affinity_matrix_ ) <EOL> assert_true ( <EOL> _check_with_col_sign_flipping ( embed_rbf , embed_callable , <NUM_LIT> ) ) <EOL> def test_spectral_embedding_amg_solver ( seed = <NUM_LIT> ) : <EOL> try : <EOL> from pyamg import smoothed_aggregation_solver <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> se_amg = SpectralEmbedding ( n_components = <NUM_LIT:2> , affinity = "<STR_LIT>" , <EOL> eigen_solver = "<STR_LIT>" , n_neighbors = <NUM_LIT:5> , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> se_arpack = SpectralEmbedding ( n_components = <NUM_LIT:2> , affinity = "<STR_LIT>" , <EOL> eigen_solver = "<STR_LIT>" , n_neighbors = <NUM_LIT:5> , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> embed_amg = se_amg . fit_transform ( S ) <EOL> embed_arpack = se_arpack . fit_transform ( S ) <EOL> assert_true ( _check_with_col_sign_flipping ( embed_amg , embed_arpack , <NUM_LIT> ) ) <EOL> def test_pipeline_spectral_clustering ( seed = <NUM_LIT> ) : <EOL> random_state = np . random . RandomState ( seed ) <EOL> se_rbf = SpectralEmbedding ( n_components = n_clusters , <EOL> affinity = "<STR_LIT>" , <EOL> random_state = random_state ) <EOL> se_knn = SpectralEmbedding ( n_components = n_clusters , <EOL> affinity = "<STR_LIT>" , <EOL> n_neighbors = <NUM_LIT:5> , <EOL> random_state = random_state ) <EOL> for se in [ se_rbf , se_knn ] : <EOL> km = KMeans ( n_clusters = n_clusters , random_state = random_state ) <EOL> km . fit ( se . fit_transform ( S ) ) <EOL> assert_array_almost_equal ( <EOL> normalized_mutual_info_score ( <EOL> km . labels_ , <EOL> true_labels ) , <NUM_LIT:1.0> , <NUM_LIT:2> ) <EOL> def test_spectral_embedding_unknown_eigensolver ( seed = <NUM_LIT> ) : <EOL> se = SpectralEmbedding ( n_components = <NUM_LIT:1> , affinity = "<STR_LIT>" , <EOL> random_state = np . random . RandomState ( seed ) , <EOL> eigen_solver = "<STR_LIT>" ) <EOL> assert_raises ( ValueError , se . fit , S ) <EOL> def test_spectral_embedding_unknown_affinity ( seed = <NUM_LIT> ) : <EOL> se = SpectralEmbedding ( n_components = <NUM_LIT:1> , affinity = "<STR_LIT>" , <EOL> random_state = np . random . RandomState ( seed ) ) <EOL> assert_raises ( ValueError , se . fit , S ) <EOL> def test_connectivity ( seed = <NUM_LIT> ) : <EOL> graph = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> assert_equal ( _graph_is_connected ( graph ) , False ) <EOL> assert_equal ( _graph_is_connected ( csr_matrix ( graph ) ) , False ) <EOL> assert_equal ( _graph_is_connected ( csc_matrix ( graph ) ) , False ) <EOL> graph = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ] ) <EOL> assert_equal ( _graph_is_connected ( graph ) , True ) <EOL> assert_equal ( _graph_is_connected ( csr_matrix ( graph ) ) , True ) <EOL> assert_equal ( _graph_is_connected ( csc_matrix ( graph ) ) , True ) <EOL> def test_spectral_embedding_deterministic ( ) : <EOL> random_state = np . random . RandomState ( <NUM_LIT> ) <EOL> data = random_state . randn ( <NUM_LIT:10> , <NUM_LIT:30> ) <EOL> sims = rbf_kernel ( data ) <EOL> embedding_1 = spectral_embedding ( sims ) <EOL> embedding_2 = spectral_embedding ( sims ) <EOL> assert_array_almost_equal ( embedding_1 , embedding_2 ) <EOL> def test_spectral_embedding_unnormalized ( ) : <EOL> random_state = np . random . RandomState ( <NUM_LIT> ) <EOL> data = random_state . randn ( <NUM_LIT:10> , <NUM_LIT:30> ) <EOL> sims = rbf_kernel ( data ) <EOL> n_components = <NUM_LIT:8> <EOL> embedding_1 = spectral_embedding ( sims , <EOL> norm_laplacian = False , <EOL> n_components = n_components , <EOL> drop_first = False ) <EOL> laplacian , dd = graph_laplacian ( sims , normed = False , return_diag = True ) <EOL> _ , diffusion_map = eigh ( laplacian ) <EOL> embedding_2 = diffusion_map . T [ : n_components ] * dd <EOL> embedding_2 = _deterministic_vector_sign_flip ( embedding_2 ) . T <EOL> assert_array_almost_equal ( embedding_1 , embedding_2 ) </s>
