【问题标题】:Word2vec tsne plotWord2vec tsne 图
【发布时间】:2018-11-03 05:02:44
【问题描述】:

我正在尝试可视化我从亚马逊评论语料库创建的 word2vec .....我采样了大约 5k 正面和 5k 负面行....分数列包含评论是正面还是负面.... 这是我的代码: **

For avg w2v i did this…(list of sent contains the avg w2v for each review)
w2v_model=gensim.models.Word2Vec(list_of_sent,min_count=5,size=50, workers=4)
Y = w2v_model[w2v_model.wv.vocab]
tsne = TSNE(n_components=2, perplexity = 30)
tsne_data = tsne.fit_transform(Y)

**

现在我想根据分数来绘制这些图,即蓝点表示阳性,红色表示阴性.......我不知道该怎么做!!...... 任何帮助将不胜感激..

【问题讨论】:

    标签: python plot visualization word2vec dimensionality-reduction


    【解决方案1】:

    如果我理解正确,您基本上是想创建一个散点图 X= TSNE Component 1, Y= TSNE Component 2 并由目标变量(正或负)着色

    以下示例代码实现了这一点::

    tsneDf = pd.DataFrame(data = tsne_data ,columns = ['TSNE component 1', 
    'TSNE component 2'])
     #Create a dataframe of TSNE Compoenent and the Score Column
    finalDf = pd.concat([tsneDf, df[['ScoreColumn']]], axis = 1)
    
    #Now we jsut plot a scatter plot
    fig = plt.figure(figsize = (8,8))
    ax = fig.add_subplot(1,1,1) 
    ax.set_xlabel('TSNE Component 1', fontsize = 15)
    ax.set_ylabel('TSNE Component 2', fontsize = 15)
    ax.set_title('2 component TSNE', fontsize = 20)
    
    #In this example 0:Negative and 1:Positive and we map respective colour
    targets = [0, 1] 
    colors = ['r', 'g']
    for target, color in zip(targets,colors):
    indicesToKeep = finalDf['ScoreColumn'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'TSNE component 1']
               , finalDf.loc[indicesToKeep, 'TSNE component 2']
               , c = color
               , s = 25,alpha=0.4)
    ax.legend(targets)
    ax.grid()
    

    示例结果如下(我的数据)

    【讨论】:

    • 感谢您提供的信息...成功了...!!
    【解决方案2】:

    您已将数据集映射为二维,因此,数据集可以转换为 3 列,其中您的 x,y 将来自 t-SNE 和正负分类列

    您可以使用 matplotlib 的 Scatterplot 进行相同的绘制

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 2020-05-12
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2017-09-28
      • 2016-08-20
      相关资源
      最近更新 更多