コード例 #1
0
    def run(self, plot_flag=1):
        mat, labels = fun.readMatrix(self.path_to_file + self.currentFile)
        if self.custom_labels:
            labels = self.labels
        mat = np.array(mat, dtype=np.float64)
        seed = np.random.RandomState(seed=2)
        X_embedded = TSNE(n_components=2,
                          random_state=seed,
                          metric='precomputed').fit_transform(mat)
        # X_embedded = SNE_RAW().fit(mat)
        if plot_flag == 1:
            plt = fun.plot(labels, X_embedded)

            plt.savefig(self.path_to_results + 't-sne.png')

        print('Error TSNE 2D: ', str(er.error(mat, X_embedded)) + '%')

        seed3d = np.random.RandomState(seed=5)
        embedding3d = TSNE(n_components=3,
                           random_state=seed3d,
                           metric='precomputed')
        X_transformed3d = embedding3d.fit_transform(mat)
        if plot_flag == 1:
            plt3d = fun.plot(labels, X_transformed3d, components=3)
            plt3d.savefig(self.path_to_results + 't-sne3D.png')
        print('Error TSNE 3D: ',
              str(er.error(mat, X_transformed3d, components=3)) + '%')
        return X_embedded
コード例 #2
0
    def run(self, plot_flag=1):
        seed = np.random.RandomState(seed=2)
        mat, labels = fun.readMatrix(self.path_to_file + self.currentFile)
        mat = np.array(mat, dtype=np.float64)
        #best=1
        #minErr=10000
        #for i in range (1,92):

        #   embedding = Isomap(n_neighbors=i, n_components=2, metric='precomputed')
        #  X_transformed = embedding.fit_transform(mat)

        # erAct=er.error(mat, X_transformed)
        #if (erAct<minErr):
        #   best=i
        #  minErr=erAct
        #print(best)
        embedding = Isomap(n_neighbors=29,
                           n_components=2,
                           metric='precomputed')
        X_transformed = embedding.fit_transform(mat)
        if plot_flag == 1:
            plt = fun.plot(labels, X_transformed)

            plt.savefig(self.path_to_results + 'isomap.png')
        print('Error ISOMAP 2D: ', str(er.error(mat, X_transformed)) + '%')

        embedding3d = Isomap(n_components=3)
        X_transformed3d = embedding3d.fit_transform(mat)
        if plot_flag == 1:
            plt3d = fun.plot(labels, X_transformed3d, components=3)
            plt3d.savefig(self.path_to_results + 'isomap3D.png')
        print('Error ISOMAP 3D: ',
              str(er.error(mat, X_transformed3d, components=3)) + '%')
        return X_transformed
コード例 #3
0
    def run(self):
        mat, labels = fun.readMatrix(self.path_to_file + self.currentFile)
        mat = fun.diag_zeros(mat)
        mat = np.array(mat, dtype=np.float64)

        seed = np.random.RandomState(seed=3)
        seed3d = np.random.RandomState(seed=5)
        embedding = MDS(n_components=2,
                        metric=False,
                        max_iter=3000,
                        eps=1e-12,
                        dissimilarity="precomputed",
                        random_state=seed,
                        n_jobs=1,
                        n_init=1)

        X_transformed = embedding.fit_transform(mat)

        plt = fun.plot(labels, X_transformed)
        plt.savefig(self.path_to_results + 'mds.png')

        print('Error: ', str(er.error(mat, X_transformed)) + '%')

        embedding3d = MDS(n_components=3,
                          dissimilarity='precomputed',
                          random_state=seed3d,
                          metric=False)
        X_transformed3d = embedding3d.fit_transform(mat)
        plt3d = fun.plot(labels, X_transformed3d, components=3)
        plt3d.savefig(self.path_to_results + 'mds3D.png')
        print('Error: ',
              str(er.error(mat, X_transformed3d, components=3)) + '%')
コード例 #4
0
    def run(self):
        mat, labels = fun.readMatrix(self.path_to_file + self.currentFile)
        mat = np.array(mat, dtype=np.float64)
        pca = PCA(n_components=2)
        principalComponents = pca.fit_transform(mat)

        plt = fun.plot(labels, principalComponents)

        plt.savefig(self.path_to_results + 'pca.png')
        print('Error: ', str(er.error(mat, principalComponents)) + '%')

        seed3d = np.random.RandomState(seed=5)
        embedding3d = PCA(n_components=3, random_state=seed3d)
        X_transformed3d = embedding3d.fit_transform(mat)
        plt3d = fun.plot(labels, X_transformed3d, components=3)
        plt3d.savefig(self.path_to_results + 'pca3D.png')
        print('Error: ',
              str(er.error(mat, X_transformed3d, components=3)) + '%')
    def run(self, plot_flag=1):
        mat, labels = fun.readMatrix(self.path_to_file + self.currentFile)
        mat = np.array(mat, dtype=np.float64)

        fun.triangle_inequality(mat)

        ti = fun.to_distance_matrix(mat)

        fun.triangle_inequality(ti)

        #print(ti)
        seed = np.random.RandomState(seed=5)
        seed3d = np.random.RandomState(seed=4)
        embedding = MDS(n_components=2,
                        dissimilarity='precomputed',
                        random_state=seed,
                        metric=True)

        X_transformed = embedding.fit_transform(ti)
        if plot_flag == 1:
            plt = fun.plot(labels, X_transformed)
            plt.savefig(self.path_to_results + 'mdsMetric.png')

        print('Error MDS Corrected 2D: ',
              str(er.error(ti, X_transformed)) + '%')

        embedding3d = MDS(n_components=3,
                          dissimilarity='precomputed',
                          random_state=seed3d,
                          metric=False)
        X_transformed3d = embedding3d.fit_transform(mat)
        if plot_flag == "1":
            plt3d = fun.plot(labels, X_transformed3d, components=3)
            plt3d.savefig(self.path_to_results + 'mds_raw3D.png')
        print('Error MDS Corrected 3D: ',
              str(er.error(mat, X_transformed3d, components=3)) + '%')
        return X_transformed
    def run(self):
        mat, labels = fun.readMatrix(self.path_to_file + self.currentFile)
        if self.custom_labels:
            labels = self.labels
        mat = np.array(mat, dtype=np.float64)
        seed = np.random.RandomState(seed=self.seed)
        # MAGIA
        if self.method == 'sne':
            X = TSNE(n_components=2, random_state=seed, metric='precomputed').fit_transform(mat)
        elif self.method == 'mds':
            embedding = MDS(n_components=2, metric=False, max_iter=3000, eps=1e-12,
                            dissimilarity="precomputed", random_state=seed, n_jobs=1,
                            n_init=1)

            X = embedding.fit_transform(mat)

        if self.plot:
            plt = fun.plot(labels, X)

        # plt.savefig(self.path_to_results + 't-sne.png')
        print('Error: ', str(er.error(mat, X)) + '%')

        return X