Esempio n. 1
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
    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
Esempio n. 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)) + '%')
Esempio n. 4
0
    def run(self):
        mat, labels = fun.readMatrix(self.path_to_file + self.currentFile)
        X = np.array(mat, dtype=np.float64)

        # setting distance_threshold=0 ensures we compute the full tree.
        model = AgglomerativeClustering(n_clusters=None, affinity="precomputed", linkage="average",
                                        distance_threshold=0)

        model = model.fit(X)

        # plot the top three levels of the dendrogram
        self.plot_dendrogram(model, labels, truncate_mode='level', p=7)
Esempio n. 5
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):
        mat, labels = fun.readMatrix(self.path_to_file + self.currentFile)
        mat = np.array(mat, dtype=np.float64)

        # Visualización 0 creacion de un grafo para observar las agrupaciones
        Graph(labels).generate(mat, normalized=True, show=True)

        #Visualización 6 hierarchical

        hierarchical.Hierarchical().run()

        [medoids, map, cost] = k_medoids.Kmedoids().run(mat)
        #Reduction(method='sne', seed=3, labels=medoids).run()
        labels_pred = []
        for item in map:
            elemento = labels[map[item]].split(".")
            labels_pred.append(elemento[0])
        new_labels = []
        for i in range(len(labels)):
            if i != 0:
                elemento = labels[i].split(".")
                new_labels.append(elemento[0])

        print(metrics.adjusted_rand_score(new_labels, labels_pred))
        print(metrics.homogeneity_score(new_labels, labels_pred))
        print(metrics.completeness_score(new_labels, labels_pred))
        print(metrics.v_measure_score(new_labels, labels_pred))

        # visualización 1 matriz con mapa de calor
        plt.imshow(mat)
        plt.colorbar()
        plt.show()

        # Visualización 3 mds raw y luego k medoids
        [medoids, map, cost] = k_medoids.Kmedoids().run(mat)
        Reduction(method='sne', seed=2, labels=medoids).run()

        # Visualización 5 t-sne y luego k medoids

        [medoids, map, cost] = k_medoids.Kmedoids().run(mat)
        Reduction(method='mds', seed=3, labels=medoids).run()
Esempio n. 7
0
    def run(self):
        mat, labels = fun.readMatrix(self.path_to_file + self.currentFile)
        mat = np.array(mat, dtype=np.float64)

        kmeans = KMeans(n_clusters=self.clusters).fit(mat)
        centroids = kmeans.cluster_centers_

        d = {}
        for i in range(self.clusters):
            d[i] = 0
        for k in kmeans.labels_:
            d[k] += 1

        seed = np.random.RandomState(seed=3)
        embedding = MDS(n_components=2,
                        dissimilarity='precomputed',
                        random_state=seed)

        X_transformed = embedding.fit_transform(mat)

        plt = fun.plot2(kmeans.labels_, X_transformed)
        plt.savefig(self.path_to_results + 'mds_kmeans.png')
        print('Error: ', str(er.error(mat, 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
    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)
        mat = np.array(mat, dtype=np.float64)
        ti = fun.to_distance_matrix(mat)

        mds = mds_corrected.main(0)
        tsne = t_sne.main(0)
        iso = isomap.main(0)
        scaler = MinMaxScaler()
        scaler.fit(mds)
        mds = scaler.transform(mds)

        scaler = MinMaxScaler()
        scaler.fit(tsne)
        tsne = scaler.transform(tsne)

        scaler = MinMaxScaler()
        scaler.fit(iso)
        iso = scaler.transform(iso)

        fun.plot3together(labels, mds, tsne, iso, "MDS", "TSNE", "ISOMAP")

        inter_mds, intra_mds, average_pos1 = self.intra_inter_class(
            "mds", mds, labels)
        inter_tsne, intra_tsne, average_pos2 = self.intra_inter_class(
            "tsne", tsne, labels)
        inter_iso, intra_iso, average_pos3 = self.intra_inter_class(
            "isomap", iso, labels)
        self.plot3Inter(average_pos1, average_pos2, average_pos3)
        self.plot_quality(intra_mds, intra_tsne, intra_iso)

        Qmds = coranking.coranking_matrix(ti, mds)
        Qtsne = coranking.coranking_matrix(mat, tsne)
        Qiso = coranking.coranking_matrix(mat, iso)

        lcm_mds = coranking.metrics.LCMC(Qmds, 1, 50)
        lcm_tsne = coranking.metrics.LCMC(Qtsne, 1, 50)
        lcm_iso = coranking.metrics.LCMC(Qiso, 1, 50)
        fig, ax = plt.subplots(1, 3)
        ax[0].set_title("LCMC")
        ax[0].set(xlabel='K', ylabel='LCMC')
        ax[0].plot(lcm_mds)
        ax[0].plot(lcm_tsne)
        ax[0].plot(lcm_iso)
        ax[0].legend(["MDS", "TSNE", "ISOMAP"])
        #plt.show()

        t_mds = trustworthiness(Qmds, 1, 50)
        t_tsne = trustworthiness(Qtsne, 1, 50)
        t_iso = trustworthiness(Qiso, 1, 50)
        ax[1].set_title("Trustworthiness")
        ax[1].set(xlabel='K', ylabel='Trustworthiness')
        ax[1].plot(t_mds)
        ax[1].plot(t_tsne)
        ax[1].plot(t_iso)
        ax[1].legend(["MDS", "TSNE", "ISOMAP"])

        c_mds = continuity(Qmds, 1, 50)
        c_tsne = continuity(Qtsne, 1, 50)
        c_iso = continuity(Qiso, 1, 50)
        ax[2].set_title("Continuity")
        ax[2].set(xlabel='K', ylabel='Continuity')
        ax[2].plot(c_mds)
        ax[2].plot(c_tsne)
        ax[2].plot(c_iso)
        ax[2].legend(["MDS", "TSNE", "ISOMAP"])

        plt.show()

        print("max K LCMC")
        print("    MDS:" + str(np.argmax(lcm_mds)))
        print("    tsne:" + str(np.argmax(lcm_tsne)))
        print("    isomap:" + str(np.argmax(lcm_iso)))
        """