Ejemplo n.º 1
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))
Ejemplo n.º 2
0
def test_lle_simple_grid():
    # note: ARPACK is numerically unstable, so this test will fail for
    #       some random seeds.  We choose 20 because the tests pass.
    rng = np.random.RandomState(20)
    tol = 0.1
    # grid of equidistant points in 2D, n_components = n_dim
    X = np.array(list(product(range(5), repeat=2)))
    X = X + 1e-10 * rng.uniform(size=X.shape)
    n_components = 2
    G = geom.Geometry(adjacency_kwds={'radius': 3})
    G.set_data_matrix(X)
    tol = 0.1
    distance_matrix = G.compute_adjacency_matrix()
    N = lle.barycenter_graph(distance_matrix, X).todense()
    reconstruction_error = np.linalg.norm(np.dot(N, X) - X, 'fro')
    print('*************')
    print('Reconstruction error for {}: {}'.format('numpy',
                                                   reconstruction_error))
    print('*************')
    assert (reconstruction_error < tol)
    for eigen_solver in EIGEN_SOLVERS:
        clf = lle.LocallyLinearEmbedding(n_components=n_components,
                                         geom=G,
                                         eigen_solver=eigen_solver,
                                         random_state=rng)
        clf.fit(X)
        assert (clf.embedding_.shape[1] == n_components)
        reconstruction_error = np.linalg.norm(
            np.dot(N, clf.embedding_) - clf.embedding_, 'fro')**2
        print('*************')
        print('Reconstruction error for {}: {}'.format(eigen_solver,
                                                       reconstruction_error))
        print('*************')
        assert (reconstruction_error < tol)
Ejemplo n.º 3
0
def test_ltsa_eigendecomps():
    N = 10
    X, color = datasets.samples_generator.make_s_curve(N, random_state=0)
    n_components = 2
    G = geom.Geometry(adjacency_method = 'brute', adjacency_kwds = {'radius':2})
    G.set_data_matrix(X)
    mm_ltsa_ref, err_ref = ltsa.ltsa(G, n_components,
                                     eigen_solver=EIGEN_SOLVERS[0])
    for eigen_solver in EIGEN_SOLVERS[1:]:
        mm_ltsa, err = ltsa.ltsa(G, n_components, eigen_solver=eigen_solver)
        assert(_check_with_col_sign_flipping(mm_ltsa, mm_ltsa_ref, 0.05))
Ejemplo n.º 4
0
def test_isomap_with_sklearn():
    N = 10
    X, color = datasets.samples_generator.make_s_curve(N, random_state=0)
    n_components = 2
    n_neighbors = 3
    knn = NearestNeighbors(n_neighbors + 1).fit(X)
    # Assign the geometry matrix to get the same answer since sklearn using k-neighbors instead of radius-neighbors
    g = geom.Geometry(X)
    g.set_adjacency_matrix(knn.kneighbors_graph(X, mode = 'distance'))
    # test Isomap with sklearn
    sk_Y_iso = manifold.Isomap(n_neighbors, n_components, eigen_solver = 'arpack').fit_transform(X)
    mm_Y_iso = iso.isomap(g, n_components)
    assert(_check_with_col_sign_flipping(sk_Y_iso, mm_Y_iso, 0.05))
Ejemplo n.º 5
0
def test_lle_with_sklearn():
    N = 10
    X, color = datasets.samples_generator.make_s_curve(N, random_state=0)
    n_components = 2
    n_neighbors = 3
    knn = NearestNeighbors(n_neighbors + 1).fit(X)
    G = geom.Geometry()
    G.set_data_matrix(X)
    G.set_adjacency_matrix(knn.kneighbors_graph(X, mode='distance'))
    sk_Y_lle = manifold.LocallyLinearEmbedding(
        n_neighbors, n_components, method='standard').fit_transform(X)
    (mm_Y_lle, err) = lle.locally_linear_embedding(G, n_components)
    assert (_check_with_col_sign_flipping(sk_Y_lle, mm_Y_lle, 0.05))
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
def test_isomap_simple_grid():
    # Isomap should preserve distances when all neighbors are used
    N_per_side = 5
    Npts = N_per_side ** 2
    radius = 10
    # grid of equidistant points in 2D, n_components = n_dim
    X = np.array(list(product(range(N_per_side), repeat=2)))
    # distances from each point to all others
    G = squareform(pdist(X))
    g = geom.Geometry(adjacency_kwds = {'radius':radius})
    for eigen_solver in EIGEN_SOLVERS:
        clf = iso.Isomap(n_components = 2, eigen_solver = eigen_solver, geom=g)
        clf.fit(X)
        G_iso = squareform(pdist(clf.embedding_))
        assert_array_almost_equal(G, G_iso)
Ejemplo n.º 8
0
def test_ltsa_manifold():
    rng = np.random.RandomState(0)
    # similar test on a slightly more complex manifold
    X = np.array(list(product(np.arange(18), repeat=2)))
    X = np.c_[X, X[:, 0] ** 2 / 18]
    X = X + 1e-10 * rng.uniform(size=X.shape)
    n_components = 2
    G = geom.Geometry(adjacency_kwds = {'radius':3})
    G.set_data_matrix(X)
    distance_matrix = G.compute_adjacency_matrix()
    tol = 1.5
    N = barycenter_graph(distance_matrix, X).todense()
    reconstruction_error = np.linalg.norm(np.dot(N, X) - X)
    assert(reconstruction_error < tol)
    for eigen_solver in EIGEN_SOLVERS:
        clf = ltsa.LTSA(n_components = n_components, geom = G,
                        eigen_solver = eigen_solver, random_state = rng)
        clf.fit(X)
        assert(clf.embedding_.shape[1] == n_components)
        reconstruction_error = np.linalg.norm(
            np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2
        assert(reconstruction_error < tol)