def test_lle_simple_grid(): rng = np.random.RandomState(0) # grid of equidistant points in 2D, out_dim = n_dim X = np.array(list(product(range(5), repeat=2))) out_dim = 2 clf = manifold.LocallyLinearEmbedding(n_neighbors=5, out_dim=out_dim) tol = .1 N = barycenter_kneighbors_graph(X, clf.n_neighbors).todense() reconstruction_error = np.linalg.norm(np.dot(N, X) - X, 'fro') assert_lower(reconstruction_error, tol) for solver in eigen_solvers: clf.set_params(eigen_solver=solver) clf.fit(X) assert clf.embedding_.shape[1] == out_dim reconstruction_error = np.linalg.norm( np.dot(N, clf.embedding_) - clf.embedding_, 'fro')**2 # FIXME: ARPACK fails this test ... if solver != 'arpack': assert_lower(reconstruction_error, tol) assert_almost_equal(clf.reconstruction_error_, reconstruction_error, decimal=4) # re-embed a noisy version of X using the transform method noise = rng.randn(*X.shape) / 100 X_reembedded = clf.transform(X + noise) assert_lower(np.linalg.norm(X_reembedded - clf.embedding_), tol)
def test_lle_simple_grid(): rng = np.random.RandomState(0) # grid of equidistant points in 2D, out_dim = n_dim X = np.array(list(product(range(5), repeat=2))) out_dim = 2 clf = manifold.LocallyLinearEmbedding(n_neighbors=5, out_dim=out_dim) tol = .1 N = barycenter_kneighbors_graph(X, clf.n_neighbors).todense() reconstruction_error = np.linalg.norm(np.dot(N, X) - X, 'fro') assert_lower(reconstruction_error, tol) for solver in eigen_solvers: clf.set_params(eigen_solver=solver) clf.fit(X) assert clf.embedding_.shape[1] == out_dim reconstruction_error = np.linalg.norm( np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2 # FIXME: ARPACK fails this test ... if solver != 'arpack': assert_lower(reconstruction_error, tol) assert_almost_equal(clf.reconstruction_error_, reconstruction_error, decimal=4) # re-embed a noisy version of X using the transform method noise = rng.randn(*X.shape) / 100 X_reembedded = clf.transform(X + noise) assert_lower(np.linalg.norm(X_reembedded - clf.embedding_), tol)
def test_lle_manifold(): # similar test on a slightly more complex manifold X = np.array(list(product(range(20), repeat=2))) X = np.c_[X, X[:, 0] ** 2 / 20] out_dim = 2 clf = manifold.LocallyLinearEmbedding(n_neighbors=5, out_dim=out_dim) tol = 1.5 N = barycenter_kneighbors_graph(X, clf.n_neighbors).toarray() reconstruction_error = np.linalg.norm(np.dot(N, X) - X) assert_lower(reconstruction_error, tol) for solver in eigen_solvers: clf.set_params(eigen_solver=solver) clf.fit(X) assert clf.embedding_.shape[1] == out_dim reconstruction_error = np.linalg.norm( np.dot(N, clf.embedding_) - clf.embedding_, 'fro') ** 2 details = "solver: " + solver assert_lower(reconstruction_error, tol, details=details) assert_lower(np.abs(clf.reconstruction_error_ - reconstruction_error), tol * reconstruction_error, details=details)
def test_isomap_reconstruction_error(): # Same setup as in test_isomap_simple_grid, with an added dimension N_per_side = 5 Npts = N_per_side**2 n_neighbors = Npts - 1 # grid of equidistant points in 2D, out_dim = n_dim X = np.array(list(product(range(N_per_side), repeat=2))) # add noise in a third dimension rng = np.random.RandomState(0) noise = 0.1 * rng.randn(Npts, 1) X = np.concatenate((X, noise), 1) # compute input kernel G = neighbors.kneighbors_graph(X, n_neighbors, mode='distance').toarray() centerer = preprocessing.KernelCenterer() K = centerer.fit_transform(-0.5 * G**2) for eigen_solver in eigen_solvers: for path_method in path_methods: clf = manifold.Isomap(n_neighbors=n_neighbors, out_dim=2, eigen_solver=eigen_solver, path_method=path_method) clf.fit(X) # compute output kernel G_iso = neighbors.kneighbors_graph(clf.embedding_, n_neighbors, mode='distance').toarray() K_iso = centerer.fit_transform(-0.5 * G_iso**2) # make sure error agrees reconstruction_error = np.linalg.norm(K - K_iso) / Npts assert_almost_equal(reconstruction_error, clf.reconstruction_error())
def test_isomap_reconstruction_error(): # Same setup as in test_isomap_simple_grid, with an added dimension N_per_side = 5 Npts = N_per_side ** 2 n_neighbors = Npts - 1 # grid of equidistant points in 2D, out_dim = n_dim X = np.array(list(product(range(N_per_side), repeat=2))) # add noise in a third dimension rng = np.random.RandomState(0) noise = 0.1 * rng.randn(Npts, 1) X = np.concatenate((X, noise), 1) # compute input kernel G = neighbors.kneighbors_graph(X, n_neighbors, mode='distance').toarray() centerer = preprocessing.KernelCenterer() K = centerer.fit_transform(-0.5 * G ** 2) for eigen_solver in eigen_solvers: for path_method in path_methods: clf = manifold.Isomap(n_neighbors=n_neighbors, out_dim=2, eigen_solver=eigen_solver, path_method=path_method) clf.fit(X) # compute output kernel G_iso = neighbors.kneighbors_graph(clf.embedding_, n_neighbors, mode='distance').toarray() K_iso = centerer.fit_transform(-0.5 * G_iso ** 2) # make sure error agrees reconstruction_error = np.linalg.norm(K - K_iso) / Npts assert_almost_equal(reconstruction_error, clf.reconstruction_error())
def test_lle_manifold(): # similar test on a slightly more complex manifold X = np.array(list(product(range(20), repeat=2))) X = np.c_[X, X[:, 0]**2 / 20] out_dim = 2 clf = manifold.LocallyLinearEmbedding(n_neighbors=5, out_dim=out_dim) tol = 1.5 N = barycenter_kneighbors_graph(X, clf.n_neighbors).toarray() reconstruction_error = np.linalg.norm(np.dot(N, X) - X) assert_lower(reconstruction_error, tol) for solver in eigen_solvers: clf.set_params(eigen_solver=solver) clf.fit(X) assert clf.embedding_.shape[1] == out_dim reconstruction_error = np.linalg.norm( np.dot(N, clf.embedding_) - clf.embedding_, 'fro')**2 details = "solver: " + solver assert_lower(reconstruction_error, tol, details=details) assert_lower(np.abs(clf.reconstruction_error_ - reconstruction_error), tol * reconstruction_error, details=details)
def test_isomap_simple_grid(): # Isomap should preserve distances when all neighbors are used N_per_side = 5 Npts = N_per_side**2 n_neighbors = Npts - 1 # grid of equidistant points in 2D, out_dim = n_dim X = np.array(list(product(range(N_per_side), repeat=2))) # distances from each point to all others G = neighbors.kneighbors_graph(X, n_neighbors, mode='distance').toarray() for eigen_solver in eigen_solvers: for path_method in path_methods: clf = manifold.Isomap(n_neighbors=n_neighbors, out_dim=2, eigen_solver=eigen_solver, path_method=path_method) clf.fit(X) G_iso = neighbors.kneighbors_graph(clf.embedding_, n_neighbors, mode='distance').toarray() assert_array_almost_equal(G, G_iso)
def test_isomap_simple_grid(): # Isomap should preserve distances when all neighbors are used N_per_side = 5 Npts = N_per_side ** 2 n_neighbors = Npts - 1 # grid of equidistant points in 2D, out_dim = n_dim X = np.array(list(product(range(N_per_side), repeat=2))) # distances from each point to all others G = neighbors.kneighbors_graph(X, n_neighbors, mode='distance').toarray() for eigen_solver in eigen_solvers: for path_method in path_methods: clf = manifold.Isomap(n_neighbors=n_neighbors, out_dim=2, eigen_solver=eigen_solver, path_method=path_method) clf.fit(X) G_iso = neighbors.kneighbors_graph(clf.embedding_, n_neighbors, mode='distance').toarray() assert_array_almost_equal(G, G_iso)