def test_barycenter_kneighbors_graph(): X = np.array([[0, 1], [1.01, 1.], [2, 0]]) distance_matrix = squareform(pdist(X)) A = lle.barycenter_graph(distance_matrix, X) # check that columns sum to one assert_array_almost_equal(np.sum(A.toarray(), 1), np.ones(3)) pred = np.dot(A.toarray(), X) assert(np.linalg.norm(pred - X) / X.shape[0] < 1)
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 Geometry = geom.Geometry(X, neighborhood_radius = 3) distance_matrix = Geometry.get_distance_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, Geometry = Geometry, 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)
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 Geometry = geom.Geometry(X, neighborhood_radius = 3) tol = 0.1 distance_matrix = Geometry.get_distance_matrix() N = lle.barycenter_graph(distance_matrix, X).todense() reconstruction_error = np.linalg.norm(np.dot(N, X) - X, 'fro') assert(reconstruction_error < tol) for eigen_solver in eigen_solvers: clf = lle.LocallyLinearEmbedding(n_components = n_components, Geometry = Geometry, 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)