Пример #1
0
    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()
Пример #2
0
    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()
Пример #3
0
    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()
Пример #4
0
    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()
Пример #5
0
    def reduce(self, evaluate=False, verbose=True):
        assert self.reduction_method in ('pca', 'mds', 'isomap', 'skisomap'), \
            'Error: unknown reduction method.'

        print('Reducing...')

        to_dimension = self.n_dimensions_()
        data = self.original_data

        print('\tMethod: %s' % self.reduction_method)
        print('\tR^%i --> R^%i' % (data.shape[1], to_dimension))

        start = time.time()

        if self.reduction_method == 'pca':
            self.reducer = decomposition.PCA(**self.reduction_params)
            self.data = self.reducer.fit_transform(data)

        elif self.reduction_method == 'mds':
            self.reducer = algorithms.MDS(verbose=verbose,
                                          **self.reduction_params)
            self.data = self.reducer.transform(data)

        elif self.reduction_method == 'isomap':
            self.reducer = algorithms.Isomap(verbose=verbose,
                                             **self.reduction_params)
            self.data = self.reducer.transform(data)

        elif self.reduction_method == 'skisomap':
            self.reducer = manifold.Isomap(**self.reduction_params)
            self.data = self.reducer.fit_transform(data)

        end = time.time()

        if evaluate:
            self.evaluate()

        self.displayer.load(self.data, self.target)

        print('Done. (%.2f s)\n' % (end - start))