예제 #1
0
def test_diffusion_embedding_two_components_no_diffusion_time(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
    geom_params = {'laplacian_method': 'geometric'}
    se_precomp = SpectralEmbedding(n_components=1,
                                   random_state=np.random.RandomState(seed),
                                   eigen_solver='arpack',
                                   diffusion_maps=True,
                                   geom=geom_params)
    embedded_coordinate = se_precomp.fit_transform(affinity,
                                                   input_type='affinity')

    # 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_diffusion_embedding_two_components_no_diffusion_time(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
    geom_params = {'laplacian_method':'geometric'}
    se_precomp = SpectralEmbedding(n_components=1,
                                   random_state=np.random.RandomState(seed),
                                   eigen_solver = 'arpack',
                                   diffusion_maps = True,
                                   geom = geom_params)
    embedded_coordinate = se_precomp.fit_transform(affinity,
                                                   input_type='affinity')

    # 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)
예제 #3
0
def test_predict_error_no_data(seed=36):
    """ Test predict raises an error when data X are not passed"""
    radius = 4.0
    se = SpectralEmbedding(n_components=2,
                           random_state=np.random.RandomState(seed))
    G = geom.Geometry(adjacency_method = 'brute', adjacency_kwds = {'radius':radius},
                      affinity_kwds = {'radius':radius})
    G.set_data_matrix(S)
    S_test = S[-100:, :]
    A = G.compute_affinity_matrix()
    embed = se.fit_transform(A, input_type = 'affinity')
    msg = 'method only implemented when X passed as data'
    assert_raise_message(NotImplementedError, msg, se.predict, S_test)
예제 #4
0
def test_predict_error_no_data(seed=36):
    """ Test predict raises an error when data X are not passed"""
    radius = 4.0
    se = SpectralEmbedding(n_components=2,
                           random_state=np.random.RandomState(seed))
    G = geom.Geometry(adjacency_method='brute',
                      adjacency_kwds={'radius': radius},
                      affinity_kwds={'radius': radius})
    G.set_data_matrix(S)
    S_test = S[-100:, :]
    A = G.compute_affinity_matrix()
    embed = se.fit_transform(A, input_type='affinity')
    msg = 'method only implemented when X passed as data'
    assert_raise_message(NotImplementedError, msg, se.predict, S_test)
예제 #5
0
 def check_size(diffusion_maps):
     radius = 4.0
     geom_params = {'affinity_kwds':{'radius':radius}, 'adjacency_kwds':{'radius':radius}, 'adjacency_method':'brute',
                 'laplacian_method':'geometric'}
     se = SpectralEmbedding(n_components=2,eigen_solver="arpack",
                            random_state=np.random.RandomState(seed), geom = geom_params)
     S_train = S[:900,:]
     S_test = S[-100:, :]
     embed_train= se.fit_transform(S_train)
     embed_test, embed_total = se.predict(S_test)
     assert(embed_test.shape[0] == S_test.shape[0])
     assert(embed_test.shape[1] == embed_train.shape[1])
     assert(embed_total.shape[0] == S.shape[0])
     assert(embed_total.shape[1] == embed_train.shape[1])
def test_spectral_embedding_symmetrzation(seed=36):
    """Test spectral embedding with amg solver vs arpack using non symmetric laplacian"""
    radius = 4.0
    geom_params = {'affinity_kwds':{'radius':radius}, 'adjacency_kwds':{'radius':radius}, 'adjacency_method':'brute',
                   'laplacian_method':'geometric'}
    try:
        import pyamg
    except ImportError:
        raise SkipTest("pyamg not available.")
    se_amg = SpectralEmbedding(n_components=2,eigen_solver="amg",
                               random_state=np.random.RandomState(seed), geom = geom_params)
    se_arpack = SpectralEmbedding(n_components=2, eigen_solver="arpack", geom = geom_params,
                                  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 = 4.0
    se_precomp = SpectralEmbedding(n_components=2,
                                   random_state=np.random.RandomState(seed))
    geom_params = {'affinity_kwds':{'radius':radius}, 'adjacency_kwds':{'radius':radius},
                   'adjacency_method':'brute'}
    se_rbf = SpectralEmbedding(n_components=2, random_state=np.random.RandomState(seed),
                               geom = geom_params)
    G = geom.Geometry(adjacency_method = 'brute', adjacency_kwds = {'radius':radius},
                      affinity_kwds = {'radius':radius})
    G.set_data_matrix(S)
    A = G.compute_affinity_matrix()
    embed_precomp = se_precomp.fit_transform(A, input_type = 'affinity')
    embed_rbf = se_rbf.fit_transform(S, input_type = 'data')
    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))
예제 #8
0
def test_spectral_embedding_symmetrzation(seed=36):
    """Test spectral embedding with amg solver vs arpack using non symmetric laplacian"""
    radius = 4.0
    geom_params = {
        'affinity_kwds': {
            'radius': radius
        },
        'adjacency_kwds': {
            'radius': radius
        },
        'adjacency_method': 'brute',
        'laplacian_method': 'geometric'
    }
    try:
        import pyamg
    except ImportError:
        raise SkipTest("pyamg not available.")
    se_amg = SpectralEmbedding(n_components=2,
                               eigen_solver="amg",
                               random_state=np.random.RandomState(seed),
                               geom=geom_params)
    se_arpack = SpectralEmbedding(n_components=2,
                                  eigen_solver="arpack",
                                  geom=geom_params,
                                  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))
예제 #9
0
def test_spectral_embedding_precomputed_affinity(seed=36,
                                                 almost_equal_decimals=5):
    """Test spectral embedding with precomputed kernel"""
    radius = 4.0
    se_precomp = SpectralEmbedding(n_components=2,
                                   random_state=np.random.RandomState(seed))
    geom_params = {
        'affinity_kwds': {
            'radius': radius
        },
        'adjacency_kwds': {
            'radius': radius
        },
        'adjacency_method': 'brute'
    }
    se_rbf = SpectralEmbedding(n_components=2,
                               random_state=np.random.RandomState(seed),
                               geom=geom_params)
    G = geom.Geometry(adjacency_method='brute',
                      adjacency_kwds={'radius': radius},
                      affinity_kwds={'radius': radius})
    G.set_data_matrix(S)
    A = G.compute_affinity_matrix()
    embed_precomp = se_precomp.fit_transform(A, input_type='affinity')
    embed_rbf = se_rbf.fit_transform(S, input_type='data')
    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))
예제 #10
0
 def check_size(diffusion_maps):
     radius = 4.0
     geom_params = {
         'affinity_kwds': {
             'radius': radius
         },
         'adjacency_kwds': {
             'radius': radius
         },
         'adjacency_method': 'brute',
         'laplacian_method': 'geometric'
     }
     se = SpectralEmbedding(n_components=2,
                            eigen_solver="arpack",
                            random_state=np.random.RandomState(seed),
                            geom=geom_params)
     S_train = S[:900, :]
     S_test = S[-100:, :]
     embed_train = se.fit_transform(S_train)
     embed_test, embed_total = se.predict(S_test)
     assert (embed_test.shape[0] == S_test.shape[0])
     assert (embed_test.shape[1] == embed_train.shape[1])
     assert (embed_total.shape[0] == S.shape[0])
     assert (embed_total.shape[1] == embed_train.shape[1])
예제 #11
0
def test_predict_error_not_fitted(seed=36):
    """ Test predict function raises an error when .fit() has not been called"""
    radius = 4.0
    geom_params = {
        'affinity_kwds': {
            'radius': radius
        },
        'adjacency_kwds': {
            'radius': radius
        },
        'adjacency_method': 'brute',
        'laplacian_method': 'geometric'
    }
    se = SpectralEmbedding(n_components=2,
                           eigen_solver="arpack",
                           random_state=np.random.RandomState(seed),
                           geom=geom_params)
    S_train = S[:900, :]
    S_test = S[-100:, :]
    msg = 'the .fit() function must be called before the .predict() function'
    assert_raise_message(RuntimeError, msg, se.predict, S_test)
예제 #12
0
def test_spectral_embedding_unknown_eigensolver(seed=36):
    """Test that SpectralClustering fails with an unknown eigensolver"""
    se = SpectralEmbedding(n_components=1,
                           random_state=np.random.RandomState(seed),
                           eigen_solver="<unknown>")
    assert_raises(ValueError, se.fit, S)