我正在尝试可视化我从亚马逊评论语料库创建的word2vec .....我采样了大约5k个正面和5k个负面rows....the分数列,其中包含评论是正面还是负面.下面是我的代码:**
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)**
现在我想根据分数来绘制这些图,即蓝点代表正面,红色点代表负面......我不知道怎么做!!.....如有任何帮助,将不胜感激..
发布于 2018-05-24 16:00:08
如果我理解正确的话,您基本上想要创建一个散点图TSNE组件1,Y= TSNE组件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()示例结果如下(针对我的数据)

发布于 2018-05-24 16:28:59
您已经将数据集映射到二维,因此,数据集可以转换为3列,其中x,y将来自t-SNE和正负分类列
您可以使用matplotlib的散点图绘制相同的图
https://stackoverflow.com/questions/50501364
复制相似问题