def test_similar_graphics(self): """Tests if Displayer class is presenting a similar graphic from the one printed by the hard-coded lines bellow (manual checking). """ points = 1000 data, color = datasets.make_swiss_roll(points, random_state=0) neighbors = 10 to_dimension = 2 result = manifold.Isomap(neighbors, to_dimension).fit_transform(data) # Expected printing... Axes3D fig = plt.figure(figsize=(15, 8)) plt.suptitle("Expected image", fontsize=14) ax = fig.add_subplot(121, projection='3d') ax.scatter(data[:, 0], data[:, 1], data[:, 2], c=color, cmap=plt.cm.Spectral) ax.view_init(4, -72) ax = fig.add_subplot(122) plt.scatter(result[:, 0], result[:, 1], c=color, cmap=plt.cm.Spectral) plt.title("SKLearn's Isomap") ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) plt.axis('tight') # Actual printing... Displayer(title="Actual image", points=points, neighbors=neighbors) \ .load(data, color, title='Graphic I') \ .load(result, color, title='SKLearn\'s Isomap') \ .show()
def test_random_matrix(self): data = np.trunc(5 + 10 * np.random.rand(10, 3)) neighbors = 2 epsilon = 5. to_dimension = 2 d = Displayer(title="Isomap algorithms comparison") \ .load(title="Canonical data set.", data=data) start = time() result = manifold.Isomap(neighbors, to_dimension).fit_transform(data) elapsed = time() - start d.load(title="SKLearn's Isomap with %i neighbors, taking %.1fs." % (neighbors, elapsed), data=result) start = time() result = algorithms.Isomap(nearest_method='e', e=epsilon, n_components=to_dimension) \ .transform(data) elapsed = time() - start d.load(title="My E-Isomap with epsilon %i, taking %.1fs." % (epsilon, elapsed), data=result) start = time() result = algorithms.Isomap(k=neighbors, n_components=to_dimension) \ .transform(data) elapsed = time() - start d.load(title="My K-Isomap with %i neighbors, taking %.1fs." % (neighbors, elapsed), data=result) d.show()
def test_display_dimensions(self): data_dir = 'datasets/' data_set = 'glass/glass.data' file = os.path.join(data_dir, data_set) print('Displaying data set {%s} in the Rn' % file) glass = Retriever(file, delimiter=',') # Glass has the samples' ids in the first column. glass.split_column(0) # Additionally, its last column represents the target feature. glass.split_target() data, c = glass.retrieve() reduced_data = algorithms.Isomap(data, e=20).run() d = Displayer(title=data_set) # Scatter all dimensions (3-by-3), using as many graphs as necessary. for begin in range(0, glass.features_count, 3): end = min(glass.features_count, begin + 3) d.load(data[:, begin:end], color=c, title='Dimensions: d e [%i, %i]' % (begin + 1, end)) d \ .load('Reduced glass data-set', reduced_data, c) \ .show()
def test_swiss_roll(self): samples = 1000 neighbors = 10 n_components = 2 data, c = datasets.make_swiss_roll(n_samples=samples, random_state=0) displayer = Displayer(title="Isomap algorithms comparison") \ .load(title="Swiss roll from %i samples." % (samples,), data=data, color=c) start = time() result = manifold.Isomap(neighbors, n_components).fit_transform(data) elapsed = time() - start displayer \ .load( title="SKLearn's Isomap with %i neighbors, taking %.1fs." % (neighbors, elapsed), data=result, color=c) start = time() result = Isomap(k=neighbors, n_components=n_components).transform(data) elapsed = time() - start displayer \ .load( title="Isomap with %i neighbors, taking %.1fs" % (neighbors, elapsed), data=result, color=c) displayer.show()
def test_rendering(self): i = algorithms.Isomap(self.test_data, n_components=2) result = i.run() Displayer() \ .load(result, np.random.rand(4), title='K-Isomap') \ .show()
def test_random_generation(self): """Tests if the Displayer class is presenting 4 graphics correctly (manual checking). """ points = 1000 data, color = datasets.make_swiss_roll(points, random_state=0) neighbors = 10 d = Displayer(points=points, neighbors=neighbors) d \ .load(data, color, title='Graphic I') \ .load(data, color, title='Graphic II') \ .load(data, color, title='Graphic III') \ .load(data, color, title='Graphic IV') \ .show()
def test_canonical_data(self): data = np.array([ [0, 0, 0], [1, 0, 0], [1, 1, 0], [1, 1, 1], ]) c = np.array([0, 0, 1, 1]) neighbors = 2 epsilon = 1.5 to_dimension = 2 d = Displayer(title="Isomap algorithms comparison") \ .load(title="Canonical data set.", data=data, color=c) start = time() result = manifold.Isomap(neighbors, to_dimension).fit_transform(data) elapsed = time() - start d.load(title="SKLearn's Isomap with %i neighbors, taking %.1fs." % (neighbors, elapsed), data=result, color=c) start = time() result = algorithms.Isomap(nearest_method='e', e=epsilon, n_components=to_dimension) \ .transform(data) elapsed = time() - start d.load(title="My E-Isomap with epsilon %i, taking %.1fs." % (epsilon, elapsed), data=result, color=c) start = time() result = algorithms.Isomap(k=neighbors, n_components=to_dimension) \ .transform(data) elapsed = time() - start d.load(title="My K-Isomap with %i neighbors, taking %.1fs." % (neighbors, elapsed), data=result, color=c) d.show()
def displayer(self): self._displayer = self._displayer or Displayer(plotting=self.plotting) return self._displayer
def test_swiss_roll(self): samples = 1000 neighbors = 10 epsilon = 5 to_dimension = 2 data, c = datasets.make_swiss_roll(n_samples=samples, random_state=0) displayer = Displayer(title="Isomap algorithms comparison") \ .load(title="Swiss roll from %i samples." % (samples,), data=data, color=c) start = time() result = manifold.Isomap(neighbors, to_dimension, path_method='D').fit_transform(data) elapsed = time() - start displayer \ .load( title="SKLearn's Isomap (%i neighbors, dijkstra, taking %.1fs)" % (neighbors, elapsed), data=result, color=c) start = time() result = manifold.Isomap(neighbors, to_dimension, path_method='FW').fit_transform(data) elapsed = time() - start displayer \ .load( title="SKLearn's Isomap (%i neighbors, floyd-warshall, taking %.1fs)" % (neighbors, elapsed), data=result, color=c) start = time() result = algorithms \ .Isomap(nearest_method='e', e=epsilon, n_components=to_dimension) \ .transform(data) elapsed = time() - start displayer.load(title="E-Isomap (epsilon=%i, dijkstra, %.1fs)" % (epsilon, elapsed), data=result, color=c) start = time() result = algorithms \ .Isomap(nearest_method='e', shortest_path_method='fw', e=epsilon, n_components=to_dimension) \ .transform(data) elapsed = time() - start displayer.load(title="E-Isomap (epsilon=%i, floyd-warshall, %.1fs)" % (epsilon, elapsed), data=result, color=c) start = time() result = algorithms \ .Isomap(k=neighbors, n_components=to_dimension) \ .transform(data) elapsed = time() - start displayer.load(title="K-Isomap (%i neighbors, dijkstra, %.1fs)" % (neighbors, elapsed), data=result, color=c) start = time() result = algorithms \ .Isomap(k=neighbors, n_components=to_dimension, shortest_path_method='fw') \ .transform(data) elapsed = time() - start displayer.load(title="K-Isomap (%i neighbors, floyd-warshall, %.1fs)" % (neighbors, elapsed), data=result, color=c) displayer.show()