Python sklearn.manifold.TSNE Examples

The following are 30 code examples of sklearn.manifold.TSNE(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sklearn.manifold , or try the search function .
Example #1
Source File: feature_vis.py    From transferlearning with MIT License 8 votes vote down vote up
def plot_tsne(self, save_eps=False):
        ''' Plot TSNE figure. Set save_eps=True if you want to save a .eps file.
        '''
        tsne = TSNE(n_components=2, init='pca', random_state=0)
        features = tsne.fit_transform(self.features)
        x_min, x_max = np.min(features, 0), np.max(features, 0)
        data = (features - x_min) / (x_max - x_min)
        del features
        for i in range(data.shape[0]):
            plt.text(data[i, 0], data[i, 1], str(self.labels[i]),
                     color=plt.cm.Set1(self.labels[i] / 10.),
                     fontdict={'weight': 'bold', 'size': 9})
        plt.xticks([])
        plt.yticks([])
        plt.title('T-SNE')
        if save_eps:
            plt.savefig('tsne.eps', dpi=600, format='eps')
        plt.show() 
Example #2
Source File: deepjdot_demo.py    From deepJDOT with MIT License 7 votes vote down vote up
def tsne_plot(xs, xt, xs_label, xt_label, subset=True, title=None, pname=None):

    num_test=100
    if subset:
        combined_imgs = np.vstack([xs[0:num_test, :], xt[0:num_test, :]])
        combined_labels = np.vstack([xs_label[0:num_test, :],xt_label[0:num_test, :]])
        combined_labels = combined_labels.astype('int')
            
    from sklearn.manifold import TSNE
    tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=3000)
    source_only_tsne = tsne.fit_transform(combined_imgs)
    plt.figure(figsize=(10, 10))
    plt.scatter(source_only_tsne[:num_test,0], source_only_tsne[:num_test,1],
                c=combined_labels[:num_test].argmax(1), s=75, marker='o', alpha=0.5, label='source train data')
    plt.scatter(source_only_tsne[num_test:,0], source_only_tsne[num_test:,1], 
                c=combined_labels[num_test:].argmax(1),s=50,marker='x',alpha=0.5,label='target train data')
    plt.legend(loc='best')
    plt.title(title)

#%% TSNE plots of source model and target model 
Example #3
Source File: line_wiki.py    From GraphEmbedding with MIT License 7 votes vote down vote up
def plot_embeddings(embeddings,):
    X, Y = read_node_label('../data/wiki/wiki_labels.txt')

    emb_list = []
    for k in X:
        emb_list.append(embeddings[k])
    emb_list = np.array(emb_list)

    model = TSNE(n_components=2)
    node_pos = model.fit_transform(emb_list)

    color_idx = {}
    for i in range(len(X)):
        color_idx.setdefault(Y[i][0], [])
        color_idx[Y[i][0]].append(i)

    for c, idx in color_idx.items():
        plt.scatter(node_pos[idx, 0], node_pos[idx, 1], label=c)
    plt.legend()
    plt.show() 
Example #4
Source File: deepwalk_wiki.py    From GraphEmbedding with MIT License 7 votes vote down vote up
def plot_embeddings(embeddings,):
    X, Y = read_node_label('../data/wiki/wiki_labels.txt')

    emb_list = []
    for k in X:
        emb_list.append(embeddings[k])
    emb_list = np.array(emb_list)

    model = TSNE(n_components=2)
    node_pos = model.fit_transform(emb_list)

    color_idx = {}
    for i in range(len(X)):
        color_idx.setdefault(Y[i][0], [])
        color_idx[Y[i][0]].append(i)

    for c, idx in color_idx.items():
        plt.scatter(node_pos[idx, 0], node_pos[idx, 1], label=c)
    plt.legend()
    plt.show() 
Example #5
Source File: sdne_wiki.py    From GraphEmbedding with MIT License 7 votes vote down vote up
def plot_embeddings(embeddings,):
    X, Y = read_node_label('../data/wiki/wiki_labels.txt')

    emb_list = []
    for k in X:
        emb_list.append(embeddings[k])
    emb_list = np.array(emb_list)

    model = TSNE(n_components=2)
    node_pos = model.fit_transform(emb_list)

    color_idx = {}
    for i in range(len(X)):
        color_idx.setdefault(Y[i][0], [])
        color_idx[Y[i][0]].append(i)

    for c, idx in color_idx.items():
        plt.scatter(node_pos[idx, 0], node_pos[idx, 1],
                    label=c)  # c=node_colors)
    plt.legend()
    plt.show() 
Example #6
Source File: utils.py    From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def learn_manifold(manifold_type, feats, n_components=2):
    if manifold_type == 'tsne':
        feats_fitted = manifold.TSNE(n_components=n_components, random_state=0).fit_transform(feats)
    elif manifold_type == 'isomap':
        feats_fitted = manifold.Isomap(n_components=n_components).fit_transform(feats)
    elif manifold_type == 'mds':
        feats_fitted = manifold.MDS(n_components=n_components).fit_transform(feats)
    elif manifold_type == 'spectral':
        feats_fitted = manifold.SpectralEmbedding(n_components=n_components).fit_transform(feats)
    else:
        raise Exception('wrong maniford type!')

    # methods = ['standard', 'ltsa', 'hessian', 'modified']
    # feats_fitted = manifold.LocallyLinearEmbedding(n_components=n_components, method=methods[0]).fit_transform(pred)

    return feats_fitted 
Example #7
Source File: node2vec_wiki.py    From GraphEmbedding with MIT License 7 votes vote down vote up
def plot_embeddings(embeddings,):
    X, Y = read_node_label('../data/wiki/wiki_labels.txt')

    emb_list = []
    for k in X:
        emb_list.append(embeddings[k])
    emb_list = np.array(emb_list)

    model = TSNE(n_components=2)
    node_pos = model.fit_transform(emb_list)

    color_idx = {}
    for i in range(len(X)):
        color_idx.setdefault(Y[i][0], [])
        color_idx[Y[i][0]].append(i)

    for c, idx in color_idx.items():
        plt.scatter(node_pos[idx, 0], node_pos[idx, 1], label=c)
    plt.legend()
    plt.show() 
Example #8
Source File: utils.py    From timeception with GNU General Public License v3.0 7 votes vote down vote up
def learn_manifold(manifold_type, feats, n_components=2):
    if manifold_type == 'tsne':
        feats_fitted = manifold.TSNE(n_components=n_components, random_state=0).fit_transform(feats)
    elif manifold_type == 'isomap':
        feats_fitted = manifold.Isomap(n_components=n_components).fit_transform(feats)
    elif manifold_type == 'mds':
        feats_fitted = manifold.MDS(n_components=n_components).fit_transform(feats)
    elif manifold_type == 'spectral':
        feats_fitted = manifold.SpectralEmbedding(n_components=n_components).fit_transform(feats)
    else:
        raise Exception('wrong maniford type!')

    # methods = ['standard', 'ltsa', 'hessian', 'modified']
    # feats_fitted = manifold.LocallyLinearEmbedding(n_components=n_components, method=methods[0]).fit_transform(pred)

    return feats_fitted 
Example #9
Source File: feature_extraction_yelp.py    From Projects with MIT License 7 votes vote down vote up
def visualize_embeddings(self):
        
        #get most common words
        print "getting common words"
        allwords = [word for sent in self.allsents for word in sent]
        counts = collections.Counter(allwords).most_common(500)

        #reduce embeddings to 2d using tsne
        print "reducing embeddings to 2D"
        embeddings = np.empty((500,embedding_size))
        for i in range(500):
            embeddings[i,:] = model[counts[i][0]]
        tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=7500)
        embeddings = tsne.fit_transform(embeddings)

        #plot embeddings
        print "plotting most common words"
        fig, ax = plt.subplots(figsize=(30, 30))
        for i in range(500):
            ax.scatter(embeddings[i,0],embeddings[i,1])
            ax.annotate(counts[i][0], (embeddings[i,0],embeddings[i,1]))
        plt.show() 
Example #10
Source File: models.py    From philo2vec with MIT License 7 votes vote down vote up
def plot(self, words, num_points=None):
        if not num_points:
            num_points = len(words)

        embeddings = self.get_words_embeddings(words)
        tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
        two_d_embeddings = tsne.fit_transform(embeddings[:num_points, :])

        assert two_d_embeddings.shape[0] >= len(words), 'More labels than embeddings'
        pylab.figure(figsize=(15, 15))  # in inches
        for i, label in enumerate(words[:num_points]):
            x, y = two_d_embeddings[i, :]
            pylab.scatter(x, y)
            pylab.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points',
                           ha='right', va='bottom')
        pylab.show() 
Example #11
Source File: plotting.py    From d-SNE with Apache License 2.0 6 votes vote down vote up
def cal_tsne_embeds(X, y, n_components=2, text=None, save_path=None):
    """
    Plot using tSNE
    :param X: embedding
    :param y: label
    :param n_components: number of components
    :param text: text for plot
    :param save_path: save path
    :return:
    """
    X = X[: 500]
    y = y[: 500]

    tsne = manifold.TSNE(n_components=n_components)
    X_tsne = tsne.fit_transform(X, y)

    plot_2d_embeds(X_tsne, y, text, save_path) 
Example #12
Source File: activity_model.py    From ad_examples with MIT License 6 votes vote down vote up
def plot_original_feature_tsne(ts, n_lags):
    """ plot t-SNE for original feature space """
    x = y = None
    for x, y in ts.get_batches(n_lags, -1, single_output_only=True):
        x = np.reshape(x, newshape=(x.shape[0], -1))
        y = np.reshape(y, newshape=(y.shape[0], -1))
    logger.debug("computing t-SNE for original space...")
    embed = manifold.TSNE(n_components=2, init='pca', random_state=0)
    x_tr = embed.fit_transform(x)
    y_tr = y[:, -1]
    pdfpath = "temp/timeseries/activity_tsne_orig_%s.pdf" % (args.algo)
    dp = DataPlotter(pdfpath=pdfpath, rows=1, cols=1)
    pl = dp.get_next_plot()
    dp.plot_points(x_tr, pl, labels=y_tr, marker='o',
                   lbl_color_map={0: "blue", 1: "red", 2: "green", 3: "orange"}, s=12)
    dp.close() 
Example #13
Source File: visualize.py    From KATE with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def word_cloud(word_embedding_matrix, vocab, s, save_file='scatter.png'):
    words = [(i, vocab[i]) for i in s]
    model = TSNE(n_components=2, random_state=0)
    #Note that the following line might use a good chunk of RAM
    tsne_embedding = model.fit_transform(word_embedding_matrix)
    words_vectors = tsne_embedding[np.array([item[1] for item in words])]

    plt.subplots_adjust(bottom = 0.1)
    plt.scatter(
        words_vectors[:, 0], words_vectors[:, 1], marker='o', cmap=plt.get_cmap('Spectral'))

    for label, x, y in zip(s, words_vectors[:, 0], words_vectors[:, 1]):
        plt.annotate(
            label,
            xy=(x, y), xytext=(-20, 20),
            textcoords='offset points', ha='right', va='bottom',
            fontsize=20,
            # bbox=dict(boxstyle='round,pad=1.', fc='yellow', alpha=0.5),
            arrowprops=dict(arrowstyle = '<-', connectionstyle='arc3,rad=0')
            )
    plt.show()
    # plt.savefig(save_file) 
Example #14
Source File: utlis.py    From deepJDOT with MIT License 6 votes vote down vote up
def tsne_plot(xs, xt, xs_label, xt_label, map_xs=None, title=None, pname=None):

    num_test=1000
    if map_xs is not None:
        combined_imgs = np.vstack([xs[0:num_test, :], xt[0:num_test, :], map_xs[0:num_test,:]])
        combined_labels = np.vstack([xs_label[0:num_test, :],xt_label[0:num_test, :], xs_label[0:num_test,:]])
        combined_labels = combined_labels.astype('int')
        combined_domain = np.vstack([np.zeros((num_test,1)),np.ones((num_test,1)),np.ones((num_test,1))*2])

    from sklearn.manifold import TSNE

    tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=3000)
    source_only_tsne = tsne.fit_transform(combined_imgs)


    plot_embedding(source_only_tsne, combined_labels.argmax(1), combined_domain,
                   title, save_fig=1, pname=pname) 
Example #15
Source File: embedding.py    From DeepDIVA with GNU Lesser General Public License v3.0 6 votes vote down vote up
def tsne(features, n_components=2):
    """
    Returns the embedded points for TSNE.
    Parameters
    ----------
    features: numpy.ndarray
        contains the input feature vectors.
    n_components: int
        number of components to transform the features into

    Returns
    -------
    embedding: numpy.ndarray
        x,y(z) points that the feature vectors have been transformed into
    """
    embedding = TSNE(n_components=n_components).fit_transform(features)
    return embedding 
Example #16
Source File: shifted_delta_cepstra.py    From hunspeech with MIT License 6 votes vote down vote up
def get_classer(self, algo_name, classer, algo_dir):
        if not os.path.exists(algo_dir):
            os.mkdir(algo_dir)
        classer_fn = '{}_classer.npy'.format(os.path.join(algo_dir, algo_name))
        trafoed_fn = '{}_trafoed.npy'.format(os.path.join(algo_dir, algo_name))
        if os.path.isfile(classer_fn):
            return pickle.load(open(classer_fn, mode='rb'))
        else:
            if algo_name == 'DBSCAN':
                self.loop_estimate_bandwidth()
            logger.info('clustering all speech with {}'.format(algo_name))
            if hasattr(classer, 'fit') and hasattr(classer, 'predict'):
                classer.fit(self.sdc_all_speech)
            elif hasattr(classer, 'fit_transform'): # TSNE
                all_speech_trafoed = classer.fit_transform(self.sdc_all_speech)
                np.save(open(trafoed_fn, mode='wb'), all_speech_trafoed)
            else: # DBSCAN
                classer.fit_predict(self.sdc_all_speech)
            logger.info(classer.get_params())
            logger.info('dumping classifier')
            pickle.dump(classer, open(classer_fn, mode='wb'))
            return classer 
Example #17
Source File: infer.py    From NLP_Toolkit with Apache License 2.0 6 votes vote down vote up
def plot_TSNE(self, plot=True):
        '''
        TSNE plot
        '''
        tsne = TSNE()
        tsne_embeddings = tsne.fit_transform(self.embeddings)
        
        if plot:
            fig = plt.figure(figsize=(13,13))
            ax = fig.add_subplot(111)
            ax.scatter(tsne_embeddings[:,0], tsne_embeddings[:,1], c="red", marker="v", \
                       label="embedded")
            ax.set_xlabel("dim-1", fontsize=15)
            ax.set_ylabel("dim-2", fontsize=15)
            ax.set_title("TSNE plot", fontsize=20)
            ax.legend(fontsize=20)
            plt.show()
            plt.close()
        return tsne_embeddings 
Example #18
Source File: plotting.py    From d-SNE with Apache License 2.0 6 votes vote down vote up
def cal_tsne_embeds_src_tgt(Xs, ys, Xt, yt, n_components=2, text=None, save_path=None, n_samples=1000, names=None):
    """
    Plot embedding for both source and target domain using tSNE
    :param Xs:
    :param ys:
    :param Xt:
    :param yt:
    :param n_components:
    :param text:
    :param save_path:
    :return:
    """
    Xs = Xs[: min(len(Xs), n_samples)]
    ys = ys[: min(len(ys), n_samples)]
    Xt = Xt[: min(len(Xt), n_samples)]
    yt = yt[: min(len(Xt), n_samples)]
    
    X = np.concatenate((Xs, Xt), axis=0)
    tsne = manifold.TSNE(n_components=n_components)
    X = tsne.fit_transform(X)
    Xs = X[: len(Xs)]
    Xt = X[len(Xs):]

    plot_embedding_src_tgt(Xs, ys, Xt, yt, text, save_path, names=names) 
Example #19
Source File: visualize_utils.py    From embedding with MIT License 6 votes vote down vote up
def visualize_words(words, vecs, palette="Viridis256", filename="/notebooks/embedding/words.png",
                    use_notebook=False):
    tsne = TSNE(n_components=2)
    tsne_results = tsne.fit_transform(vecs)
    df = pd.DataFrame(columns=['x', 'y', 'word'])
    df['x'], df['y'], df['word'] = tsne_results[:, 0], tsne_results[:, 1], list(words)
    source = ColumnDataSource(ColumnDataSource.from_df(df))
    labels = LabelSet(x="x", y="y", text="word", y_offset=8,
                      text_font_size="15pt", text_color="#555555",
                      source=source, text_align='center')
    color_mapper = LinearColorMapper(palette=palette, low=min(tsne_results[:, 1]), high=max(tsne_results[:, 1]))
    plot = figure(plot_width=900, plot_height=900)
    plot.scatter("x", "y", size=12, source=source, color={'field': 'y', 'transform': color_mapper}, line_color=None,
                 fill_alpha=0.8)
    plot.add_layout(labels)
    if use_notebook:
        output_notebook()
        show(plot)
    else:
        export_png(plot, filename)
        print("save @ " + filename) 
Example #20
Source File: visualize_utils.py    From embedding with MIT License 6 votes vote down vote up
def visualize_sentences(vecs, sentences, palette="Viridis256", filename="/notebooks/embedding/sentences.png",
                        use_notebook=False):
    tsne = TSNE(n_components=2)
    tsne_results = tsne.fit_transform(vecs)
    df = pd.DataFrame(columns=['x', 'y', 'sentence'])
    df['x'], df['y'], df['sentence'] = tsne_results[:, 0], tsne_results[:, 1], sentences
    source = ColumnDataSource(ColumnDataSource.from_df(df))
    labels = LabelSet(x="x", y="y", text="sentence", y_offset=8,
                      text_font_size="12pt", text_color="#555555",
                      source=source, text_align='center')
    color_mapper = LinearColorMapper(palette=palette, low=min(tsne_results[:, 1]), high=max(tsne_results[:, 1]))
    plot = figure(plot_width=900, plot_height=900)
    plot.scatter("x", "y", size=12, source=source, color={'field': 'y', 'transform': color_mapper}, line_color=None, fill_alpha=0.8)
    plot.add_layout(labels)
    if use_notebook:
        output_notebook()
        show(plot)
    else:
        export_png(plot, filename)
        print("save @ " + filename) 
Example #21
Source File: tsne_visualization.py    From face-recognition with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def main():
    args = parse_args()
    X, labels = np.loadtxt(args.embeddings_path), np.loadtxt(args.labels_path, dtype=np.str)
    tsne = TSNE(n_components=2, n_iter=10000, perplexity=5, init='pca', learning_rate=200, verbose=1)
    transformed = tsne.fit_transform(X)

    y = set(labels)
    labels = np.array(labels)
    plt.figure(figsize=(20, 14))
    colors = cm.rainbow(np.linspace(0, 1, len(y)))
    for label, color in zip(y, colors):
        points = transformed[labels == label, :]
        plt.scatter(points[:, 0], points[:, 1], c=[color], label=label, s=200, alpha=0.5)
        for p1, p2 in random.sample(list(zip(points[:, 0], points[:, 1])), k=min(1, len(points))):
            plt.annotate(label, (p1, p2), fontsize=30)

    plt.savefig('tsne_visualization.png', transparent=True, bbox_inches='tight', pad_inches=0)
    plt.show() 
Example #22
Source File: visualize_syn.py    From gwl with GNU General Public License v3.0 6 votes vote down vote up
def plot_results(gwl_model, index_s, index_t, epoch):
    # tsne
    embs_s = gwl_model.emb_model[0](index_s)
    embs_t = gwl_model.emb_model[1](index_t)
    embs = np.concatenate((embs_s.cpu().data.numpy(), embs_t.cpu().data.numpy()), axis=0)
    embs = TSNE(n_components=2).fit_transform(embs)
    plt.figure(figsize=(5, 5))
    plt.scatter(embs[:embs_s.size(0), 0], embs[:embs_s.size(0), 1],
                marker='x', s=14, c='b', edgecolors='b', label='Email Net')
    plt.scatter(embs[-embs_t.size(0):, 0], embs[-embs_t.size(0):, 1],
                marker='o', s=12, c='', edgecolors='r', label='Call Net')
    leg = plt.legend(loc='upper left', ncol=1, shadow=True, fancybox=True)
    leg.get_frame().set_alpha(0.5)
    plt.xlabel('T-SNE of node embeddings')
    plt.savefig('emb2_epoch{}.pdf'.format(epoch))
    plt.close("all") 
Example #23
Source File: visualize_mimic3.py    From gwl with GNU General Public License v3.0 6 votes vote down vote up
def plot_results(gwl_model, index_s, index_t, epoch):
    # tsne
    embs_s = gwl_model.emb_model[0](index_s)
    embs_t = gwl_model.emb_model[1](index_t)
    embs = np.concatenate((embs_s.cpu().data.numpy(), embs_t.cpu().data.numpy()), axis=0)
    embs = TSNE(n_components=2).fit_transform(embs)
    plt.figure(figsize=(5, 5))
    plt.scatter(embs[:embs_s.size(0), 0], embs[:embs_s.size(0), 1],
                marker='x', s=10, c='b', edgecolors='b', label='Diseases')
    plt.scatter(embs[-embs_t.size(0):, 0], embs[-embs_t.size(0):, 1],
                marker='o', s=10, c='', edgecolors='r', label='Procedures')
    leg = plt.legend(loc='upper left', ncol=1, shadow=True, fancybox=True)
    leg.get_frame().set_alpha(0.5)
    plt.xlabel('T-SNE of node embeddings')
    plt.savefig('mimic3_epoch{}.pdf'.format(epoch))
    plt.close("all") 
Example #24
Source File: GromovWassersteinLearning.py    From gwl with GNU General Public License v3.0 6 votes vote down vote up
def plot_result(self, index_s, index_t, epoch, prefix):
        # tsne
        embs_s = self.gwl_model.emb_model[0](index_s)
        embs_t = self.gwl_model.emb_model[1](index_t)
        embs = np.concatenate((embs_s.cpu().data.numpy(), embs_t.cpu().data.numpy()), axis=0)
        embs = TSNE(n_components=2).fit_transform(embs)
        plt.figure(figsize=(5, 5))
        plt.scatter(embs[:embs_s.size(0), 0], embs[:embs_s.size(0), 1],
                    marker='.', s=0.5, c='b', edgecolors='b', label='graph 1')
        plt.scatter(embs[-embs_t.size(0):, 0], embs[-embs_t.size(0):, 1],
                    marker='o', s=8, c='', edgecolors='r', label='graph 2')
        leg = plt.legend(loc='upper left', ncol=1, shadow=True, fancybox=True)
        leg.get_frame().set_alpha(0.5)
        plt.title('T-SNE of node embeddings')
        plt.savefig('{}/emb_epoch{}_{}_{}.pdf'.format(prefix, epoch, self.ot_method, self.cost_type))
        plt.close("all")

        trans_b = np.zeros(self.trans.shape)
        for i in range(trans_b.shape[0]):
            idx = np.argmax(self.trans[i, :])
            trans_b[i, idx] = 1
        plt.imshow(trans_b)
        plt.savefig('{}/trans_epoch{}_{}_{}.png'.format(prefix, epoch, self.ot_method, self.cost_type))
        plt.close('all') 
Example #25
Source File: plot.py    From pytorch-sgns with MIT License 6 votes vote down vote up
def plot(args):
    wc = pickle.load(open(os.path.join(args.data_dir, 'wc.dat'), 'rb'))
    words = sorted(wc, key=wc.get, reverse=True)[:args.top_k]
    if args.model == 'pca':
        model = PCA(n_components=2)
    elif args.model == 'tsne':
        model = TSNE(n_components=2, perplexity=30, init='pca', method='exact', n_iter=5000)
    word2idx = pickle.load(open('data/word2idx.dat', 'rb'))
    idx2vec = pickle.load(open('data/idx2vec.dat', 'rb'))
    X = [idx2vec[word2idx[word]] for word in words]
    X = model.fit_transform(X)
    plt.figure(figsize=(18, 18))
    for i in range(len(X)):
        plt.text(X[i, 0], X[i, 1], words[i], bbox=dict(facecolor='blue', alpha=0.1))
    plt.xlim((np.min(X[:, 0]), np.max(X[:, 0])))
    plt.ylim((np.min(X[:, 1]), np.max(X[:, 1])))
    if not os.path.isdir(args.result_dir):
        os.mkdir(args.result_dir)
    plt.savefig(os.path.join(args.result_dir, args.model) + '.png') 
Example #26
Source File: embedding.py    From voice-vector with MIT License 6 votes vote down vote up
def plot_embedding(embedding, annotation=None, filename='outputs/embedding.png'):
    reduced = TSNE(n_components=2).fit_transform(embedding)
    plt.figure(figsize=(20, 20))
    max_x = np.amax(reduced, axis=0)[0]
    max_y = np.amax(reduced, axis=0)[1]
    plt.xlim((-max_x, max_x))
    plt.ylim((-max_y, max_y))

    plt.scatter(reduced[:, 0], reduced[:, 1], s=20, c=["r"] + ["b"] * (len(reduced) - 1))

    # Annotation
    if annotation:
        for i in range(embedding.shape[0]):
            target = annotation[i]
            x = reduced[i, 0]
            y = reduced[i, 1]
            plt.annotate(target, (x, y))

    plt.savefig(filename)
    # plt.show() 
Example #27
Source File: plot.py    From ML_CIA with MIT License 6 votes vote down vote up
def plot(args):
    wc = pickle.load(open(os.path.join(args.data_dir, 'wc.dat'), 'rb'))
    words = sorted(wc, key=wc.get, reverse=True)[:args.top_k]
    if args.model == 'pca':
        model = PCA(n_components=2)
    elif args.model == 'tsne':
        model = TSNE(n_components=2, perplexity=30, init='pca', method='exact', n_iter=5000)
    word2idx = pickle.load(open('data/word2idx.dat', 'rb'))
    idx2vec = pickle.load(open('data/idx2vec.dat', 'rb'))
    X = [idx2vec[word2idx[word]] for word in words]
    X = model.fit_transform(X)
    plt.figure(figsize=(18, 18))
    for i in range(len(X)):
        plt.text(X[i, 0], X[i, 1], words[i], bbox=dict(facecolor='blue', alpha=0.1))
    plt.xlim((np.min(X[:, 0]), np.max(X[:, 0])))
    plt.ylim((np.min(X[:, 1]), np.max(X[:, 1])))
    if not os.path.isdir(args.result_dir):
        os.mkdir(args.result_dir)
    plt.savefig(os.path.join(args.result_dir, args.model) + '.png') 
Example #28
Source File: AE_ts_model.py    From AE_ts with MIT License 6 votes vote down vote up
def plot_z_run(z_run, label, ):
    f1, ax1 = plt.subplots(2, 1)

    # First fit a PCA
    PCA_model = TruncatedSVD(n_components=3).fit(z_run)
    z_run_reduced = PCA_model.transform(z_run)
    ax1[0].scatter(z_run_reduced[:, 0], z_run_reduced[:, 1], c=label, marker='*', linewidths=0)
    ax1[0].set_title('PCA on z_run')

    # THen fit a tSNE
    tSNE_model = TSNE(verbose=2, perplexity=80, min_grad_norm=1E-12, n_iter=3000)
    z_run_tsne = tSNE_model.fit_transform(z_run)
    ax1[1].scatter(z_run_tsne[:, 0], z_run_tsne[:, 1], c=label, marker='*', linewidths=0)
    ax1[1].set_title('tSNE on z_run')

    plt.show()
    return 
Example #29
Source File: pseudo_pretrain_cifar.py    From Pseudo-Label-Keras with MIT License 5 votes vote down vote up
def on_train_end(self, logs):
        y_true = np.ravel(self.y_test)
        emb_model = Model(self.model.input, self.model.layers[-2].output)
        embedding = emb_model.predict(self.X_test / 255.0)
        proj = TSNE(n_components=2).fit_transform(embedding)
        cmp = plt.get_cmap("tab10")
        plt.figure()
        for i in range(10):
            select_flag = y_true == i
            plt_latent = proj[select_flag, :]
            plt.scatter(plt_latent[:,0], plt_latent[:,1], color=cmp(i), marker=".")
        plt.savefig(f"result_pseudo/embedding_{self.n_labeled_sample:05}.png") 
Example #30
Source File: mobilenet_transfer_pseudo_cifar.py    From Pseudo-Label-Keras with MIT License 5 votes vote down vote up
def on_train_end(self, logs):
        y_true = np.ravel(self.y_test)
        emb_model = Model(self.model.input, self.model.layers[-2].output)
        embedding = emb_model.predict(self.X_test / 255.0)
        proj = TSNE(n_components=2).fit_transform(embedding)
        cmp = plt.get_cmap("tab10")
        plt.figure()
        for i in range(10):
            select_flag = y_true == i
            plt_latent = proj[select_flag, :]
            plt.scatter(plt_latent[:,0], plt_latent[:,1], color=cmp(i), marker=".")
        plt.savefig(f"result_pseudo_trans_mobile/embedding_{self.n_labeled_sample:05}.png")