def test_laplacian_eigenmap_precomputed_affinity(seed=36):
    # Test laplacian eigenmap with precomputed kernel
    gamma = 1.0
    embedding_precomp = LaplacianEigenmap(n_components=2, affinity="precomputed",
                                   random_state=np.random.RandomState(seed))
    embedding_rbf = LaplacianEigenmap(n_components=2, affinity="rbf",
                               gamma=gamma,
                               random_state=np.random.RandomState(seed))
    embed_precomp = embedding_precomp.fit_transform(rbf_kernel(S, gamma=gamma))
    embed_rbf = embedding_rbf.fit_transform(S)
    assert_array_almost_equal(
        embedding_precomp.affinity_matrix_, embedding_rbf.affinity_matrix_)
    assert_true(_check_with_col_sign_flipping(embed_precomp, embed_rbf, 0.05))
def test_laplacian_eigenmap_amg_solver(seed=36):
    # Test laplacian eigenmap with amg solver
    try:
        from pyamg import smoothed_aggregation_solver
    except ImportError:
        raise SkipTest("pyamg not available.")

    embedding_amg = LaplacianEigenmap(n_components=2, affinity="nearest_neighbors",
                               eigen_solver="amg", n_neighbors=5,
                               random_state=np.random.RandomState(seed))
    embedding_arpack = LaplacianEigenmap(n_components=2, affinity="nearest_neighbors",
                                  eigen_solver="arpack", n_neighbors=5,
                                  random_state=np.random.RandomState(seed))
    embed_amg = embedding_amg.fit_transform(S)
    embed_arpack = embedding_arpack.fit_transform(S)
    assert_true(_check_with_col_sign_flipping(embed_amg, embed_arpack, 0.05))
def test_laplacian_eigenmap_callable_affinity(seed=36):
    # Test laplacian eigenmap with callable affinity
    gamma = 0.9
    kern = rbf_kernel(S, gamma=gamma)
    embedding_callable = LaplacianEigenmap(n_components=2,
                                    affinity=(
                                        lambda x: rbf_kernel(x, gamma=gamma)),
                                    gamma=gamma,
                                    random_state=np.random.RandomState(seed))
    embedding_rbf = LaplacianEigenmap(n_components=2, affinity="rbf",
                               gamma=gamma,
                               random_state=np.random.RandomState(seed))
    embed_rbf = embedding_rbf.fit_transform(S)
    embed_callable = embedding_callable.fit_transform(S)
    assert_array_almost_equal(
        embedding_callable.affinity_matrix_, embedding_rbf.affinity_matrix_)
    assert_array_almost_equal(kern, embedding_rbf.affinity_matrix_)
    assert_true(
        _check_with_col_sign_flipping(embed_rbf, embed_callable, 0.05))
def test_laplacian_eigenmap_two_components(seed=36):
    # Test laplacian eigenmap 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

    # Test of internal _graph_connected_component before connection
    component = _graph_connected_component(affinity, 0)
    assert_true(component[:n_sample].all())
    assert_true(not component[n_sample:].any())
    component = _graph_connected_component(affinity, -1)
    assert_true(not component[:n_sample].any())
    assert_true(component[n_sample:].all())

    # 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

    embedding_precomp = LaplacianEigenmap(n_components=1, affinity="precomputed",
                                   random_state=np.random.RandomState(seed))
    embedded_coordinate = embedding_precomp.fit_transform(affinity)
    # Some numpy versions are touchy with types
    embedded_coordinate = \
        embedding_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)