def test_swiss_roll(self): X = sr.swiss_roll(6, 10) self.assertEqual(X.shape, (10, 3)) X, theta = sr.swiss_roll(3.0, 25, theta_noise=0, radius_noise=0, return_theta=True) self.assertEqual(X.shape, (25, 3)) self.assertEqual(theta.shape, (25,)) self.assertAlmostEqual(theta.max(), 3.0)
def main(): X, theta = swiss_roll(8, 500, return_theta=True) D = pairwise_distances(X) graph_info = [ _c('5-NN', neighbor_graph, D, k=6, precomputed=True), _c('b-matching', b_matching, D, 6), _c('gabriel', gabriel_graph, X), _c('rel. neighborhood', relative_neighborhood_graph,D,metric='precomputed'), _c('manifold spanning', manifold_spanning_graph, X, 2), _c('L1', sparse_regularized_graph, X, k=10, alpha=0.0005), _c('SAFFRON', saffron, X, q=15, k=5, tangent_dim=2), _c('MST', mst, D, metric='precomputed'), _c('dMST', disjoint_mst, D, metric='precomputed'), ] print('Plotting graphs & embeddings') fig1, axes1 = plt.subplots(nrows=3, ncols=3, subplot_kw=dict(projection='3d')) fig2, axes2 = plt.subplots(nrows=3, ncols=3) fig1.suptitle('Original Coordinates') fig2.suptitle('Isomap Embeddings') plot_kwargs = dict(directed=False, weighted=False, edge_style='k-') for ax1, ax2, info in zip(axes1.flat, axes2.flat, graph_info): label, G, gg, emb, mask = info G.plot(X, ax=ax1, title=label, vertex_style=dict(c=theta), **plot_kwargs) gg.plot(emb, ax=ax2, title=label, vertex_style=dict(c=theta[mask]), **plot_kwargs) ax1.view_init(elev=5, azim=70) ax1.set_axis_off() ax2.set_axis_off() plt.show()
def main(): np.random.seed(1234) X, theta = swiss_roll(8, 500, return_theta=True) print('Figure 1 of 3: bare coordinates in 3d') ax = Axes3D(plt.figure()) ax.scatter(*X.T, c=theta) print('Figure 2 of 3: 5-NN graph in original coordinates') g = neighbor_graph(X, k=5).symmetrize('max') g.plot(X, directed=False, weighted=False, fig='new', edge_style='k-', vertex_style=dict(c=theta)) print('Writing swiss_roll.html for force-directed layout demo') g.to_html('swiss_roll.html', directed=False, weighted=False, vertex_colors=theta) print('Figure 3 of 3: 2d Isomap embedding of 5-NN graph') emb = g.isomap(num_dims=2) _, ax = plt.subplots(figsize=(10, 5)) g.plot(emb, directed=False, weighted=False, ax=ax, edge_style='k-', vertex_style=dict(c=theta)) ax.xaxis.set_ticks([]) ax.yaxis.set_ticks([]) plt.show()
def test_swiss_roll(self): np.random.seed(1234) X, theta = swiss_roll(6, 120, radius=4.8, return_theta=True) GT = np.hstack((theta[:, None], X[:, 1:2])) GT -= GT.min(axis=0) GT /= GT.max(axis=0) G = manifold_spanning_graph(X, 2) self.assertEqual(error_ratio(G, GT), 0.0)