Exemplo n.º 1
0
    def test_affinity(self):
        init = initialization.random(self.x, random_state=0)

        for aff in [
                affinity.PerplexityBasedNN(self.x, perplexity=30),
                affinity.Uniform(self.x, k_neighbors=30),
                affinity.FixedSigmaNN(self.x, sigma=1, k=30),
                affinity.Multiscale(self.x, perplexities=[10, 30]),
                affinity.MultiscaleMixture(self.x, perplexities=[10, 30]),
        ]:
            # Without initilization
            embedding = TSNE().fit(affinities=aff)
            self.eval_embedding(embedding, self.y, aff.__class__.__name__)
            new_embedding = embedding.prepare_partial(self.x)
            new_embedding.optimize(10, learning_rate=0.1, inplace=True)
            self.eval_embedding(new_embedding, self.y,
                                f"transform::{aff.__class__.__name__}")

            # With initilization
            embedding = TSNE().fit(affinities=aff, initialization=init)
            self.eval_embedding(embedding, self.y, aff.__class__.__name__)
            new_embedding = embedding.prepare_partial(self.x)
            new_embedding.optimize(10, learning_rate=0.1, inplace=True)
            self.eval_embedding(new_embedding, self.y,
                                f"transform::{aff.__class__.__name__}")
Exemplo n.º 2
0
    def test_affinity_with_precomputed_neighbors(self):
        nn = NearestNeighbors(n_neighbors=30)
        nn.fit(self.x)
        distances, neighbors = nn.kneighbors(n_neighbors=30)

        knn_index = nearest_neighbors.PrecomputedNeighbors(
            neighbors, distances)
        init = initialization.random(self.x, random_state=0)

        for aff in [
                affinity.PerplexityBasedNN(knn_index=knn_index, perplexity=30),
                affinity.Uniform(knn_index=knn_index, k_neighbors=30),
                affinity.FixedSigmaNN(knn_index=knn_index, sigma=1),
                affinity.Multiscale(knn_index=knn_index, perplexities=[10,
                                                                       20]),
                affinity.MultiscaleMixture(knn_index=knn_index,
                                           perplexities=[10, 20]),
        ]:
            # Without initilization
            embedding = TSNE().fit(affinities=aff)
            self.eval_embedding(embedding, self.y, aff.__class__.__name__)

            # With initilization
            embedding = TSNE().fit(affinities=aff, initialization=init)
            self.eval_embedding(embedding, self.y, aff.__class__.__name__)
Exemplo n.º 3
0
def tsneScatter(associations,
                perplexity,
                weigthMin=0,
                random=0,
                openTsne=True):
    if isinstance(associations, nx.Graph):
        matrix = nx.to_numpy_matrix(associations)
    elif isinstance(associations, pd.DataFrame):
        matrix = associations.values
    matrix[matrix < weigthMin] = 0
    max = np.amax(matrix)
    matrixD = max - matrix
    np.fill_diagonal(matrixD, 0)
    if not openTsne:
        tsne = TSNE(metric='precomputed',
                    perplexity=perplexity,
                    random_state=random).fit_transform(matrixD)
        x = tsne[:, 0]
        y = tsne[:, 1]
    else:
        n = matrixD.shape[0]
        narrayD = np.zeros((n, n))
        for j in range(n):
            narrayD[j, :] = matrixD[j, :]
        affinities_train = PerplexityBasedNN(narrayD,
                                             perplexity=perplexity,
                                             method='predefined',
                                             metric="predefined",
                                             random_state=random)
        init_train = initialization.random(narrayD, random_state=random)
        embedding_train = TSNEEmbedding(init_train,
                                        affinities=affinities_train)
        embedding = embedding_train.optimize(n_iter=250,
                                             exaggeration=12,
                                             momentum=0.5)
        x = [x[0] for x in embedding]
        y = [x[1] for x in embedding]

        for i in range(len(matrixD) - 1):
            for j in range(i + 1, len(matrixD)):
                if matrixD[i, j] < max:
                    plt.plot([x[i], x[j]], [y[i], y[j]],
                             'bo-',
                             linewidth=0.5,
                             markersize=1)
Exemplo n.º 4
0
    def test_affinity_with_precomputed_distances(self):
        d = squareform(pdist(self.x))
        knn_index = nearest_neighbors.PrecomputedDistanceMatrix(d, k=30)
        init = initialization.random(self.x, random_state=0)

        for aff in [
                affinity.PerplexityBasedNN(knn_index=knn_index, perplexity=30),
                affinity.Uniform(knn_index=knn_index, k_neighbors=30),
                affinity.FixedSigmaNN(knn_index=knn_index, sigma=1),
                affinity.Multiscale(knn_index=knn_index, perplexities=[10,
                                                                       20]),
                affinity.MultiscaleMixture(knn_index=knn_index,
                                           perplexities=[10, 20]),
        ]:
            # Without initilization
            embedding = TSNE().fit(affinities=aff)
            self.eval_embedding(embedding, aff.__class__.__name__)

            # With initilization
            embedding = TSNE().fit(affinities=aff, initialization=init)
            self.eval_embedding(embedding, aff.__class__.__name__)