def test_spectral_embedding_two_components(seed=36): """Test spectral embedding with two components""" random_state = np.random.RandomState(seed) n_sample = 100 affinity = np.zeros(shape=[n_sample * 2, n_sample * 2]) # first component affinity[0:n_sample, 0:n_sample] = np.abs(random_state.randn(n_sample, n_sample)) + 2 # second component affinity[n_sample::, n_sample::] = np.abs(random_state.randn(n_sample, n_sample)) + 2 # connection affinity[0, n_sample + 1] = 1 affinity[n_sample + 1, 0] = 1 affinity.flat[::2 * n_sample + 1] = 0 affinity = 0.5 * (affinity + affinity.T) true_label = np.zeros(shape=2 * n_sample) true_label[0:n_sample] = 1 se_precomp = SpectralEmbedding(n_components=1, input_type = 'affinity', random_state=np.random.RandomState(seed), eigen_solver = 'arpack') embedded_coordinate = se_precomp.fit_transform(affinity) # Some numpy versions are touchy with types embedded_coordinate = \ se_precomp.fit_transform(affinity.astype(np.float32)) # thresholding on the first components using 0. label_ = np.array(embedded_coordinate.ravel() < 0, dtype="float") assert_equal(normalized_mutual_info_score(true_label, label_), 1.0)
def test_spectral_embedding_amg_solver(seed=36): """Test spectral embedding with amg solver""" try: from pyamg import smoothed_aggregation_solver except ImportError: raise SkipTest("pyagm not available.") se_amg = SpectralEmbedding(n_components=2,eigen_solver="amg", neighborhood_radius = 1.0, random_state=np.random.RandomState(seed)) se_arpack = SpectralEmbedding(n_components=2, eigen_solver="arpack", neighborhood_radius = 1.0, random_state=np.random.RandomState(seed)) embed_amg = se_amg.fit_transform(S) embed_arpack = se_arpack.fit_transform(S) assert_true(_check_with_col_sign_flipping(embed_amg, embed_arpack, 0.05))
def test_spectral_embedding_precomputed_affinity(seed=36,almost_equal_decimals=5): """Test spectral embedding with precomputed kernel""" radius = 1.0 se_precomp = SpectralEmbedding(n_components=2, input_type = 'affinity', random_state=np.random.RandomState(seed)) se_rbf = SpectralEmbedding(n_components=2, neighborhood_radius = radius, affinity_radius = radius, input_type = 'data', random_state=np.random.RandomState(seed), distance_method = 'brute') G = geom.Geometry(S, input_type = 'data', neighborhood_radius = radius, affinity_radius = radius, distance_method = 'brute') A = G.get_affinity_matrix() embed_precomp = se_precomp.fit_transform(A) embed_rbf = se_rbf.fit_transform(S) assert_array_almost_equal( se_precomp.affinity_matrix_.todense(), se_rbf.affinity_matrix_.todense(), almost_equal_decimals) assert_true(_check_with_col_sign_flipping(embed_precomp, embed_rbf, 0.05))
def time_function(X, method, radius, d = None, random_state = None): if method == 'distance_cy': t0 = time.time() dmat = distance_matrix(X, method = 'cyflann', radius = radius) t1 = time.time() return (t1 - t0) if method == 'distance_py': path_to_flann = '/homes/jmcq/flann-1.8.4-src/src/python' sys.path.insert(0, path_to_flann) import pyflann as pyf flindex = pyf.FLANN() flparams = flindex.build_index(X, algorithm = 'kmeans', target_precision = 0.9) t0 = time.time() dmat = distance_matrix(X, method = 'pyflann', radius = radius, flindex = flindex) t1 = time.time() return (t1 - t0) if method == 'distance_brute': t0 = time.time() dmat = distance_matrix(X, method = 'brute', radius = radius) t1 = time.time() return (t1 - t0) elif method == 'spectral': from Mmani.embedding.spectral_embedding import SpectralEmbedding t0 = time.time() SE = SpectralEmbedding(n_components=d, eigen_solver='amg',random_state=random_state, neighborhood_radius=radius, distance_method='cyflann', input_type='data',laplacian_type='geometric') embedding = SE.fit_transform(X) t1 = time.time() return (t1 - t0) elif method == 'isomap': from Mmani.embedding.isomap import Isomap t0 = time.time() try: isomap = Isomap(n_components=d, eigen_solver='amg',random_state=random_state, neighborhood_radius=radius, distance_method='cyflann', input_type='data') embedding = isomap.fit_transform(X) except: isomap = Isomap(n_components=d, eigen_solver='arpack',random_state=random_state, neighborhood_radius=radius, distance_method='cyflann', input_type='data') embedding = isomap.fit_transform(X) t1 = time.time() return (t1 - t0) elif method == 'ltsa': from Mmani.embedding.ltsa import LTSA t0 = time.time() ltsa = LTSA(n_components=d, eigen_solver='amg',random_state=random_state, neighborhood_radius=radius, distance_method='cyflann', input_type='data') embedding = ltsa.fit_transform(X) t1 = time.time() return (t1 - t0) elif method == 'lle': from Mmani.embedding.locally_linear import LocallyLinearEmbedding t0 = time.time() lle = LocallyLinearEmbedding(n_components=d, eigen_solver='amg',random_state=random_state, neighborhood_radius=radius, distance_method='cyflann', input_type='data') embedding = lle.fit_transform(X) t1 = time.time() return (t1 - t0) else: raise ValueError('benchmark method: ' + str(method) + ' not found.') return None