GloVe学习之Python中简单的词向量SVD分解

GloVe原理还没搞清楚,先用Python学着跑跑小程序好了。


语料:

I like deep learning. 

I like NLP.

I enjoy flying.

其中words=["I","like","enjoy","deep","learning","NLP","flying","."]


上下文相关矩阵:

X=np.array([[0,2,1,0,0,0,0,0],[2,0,0,1,0,1,0,0],[1,0,0,0,0,0,1,0],[0,1,0,0,1,0,0,0],[0,0,0,1,0,0,0,1],[0,1,0,0,0,0,0,1],[0,0,1,0,0,0,0,1],[0,0,0,0,1,1,1,0]])


code:

import numpy as np
import matplotlib.pyplot as plt

la = np.linalg
words = ["I","like","enjoy","deep","learning","NLP","flying","."]

X=np.array([[0,2,1,0,0,0,0,0],[2,0,0,1,0,1,0,0],[1,0,0,0,0,0,1,0],[0,1,0,0,1,0,0,0],[0,0,0,1,0,0,0,1],[0,1,0,0,0,0,0,1],[0,0,1,0,0,0,0,1],[0,0,0,0,1,1,1,0]])

U,s,Vh=la.svd(X,full_matrices=False)

plt.axis([-0.8,0.2,-0.8,0.8])

for i in xrange(len(words)):
	plt.text(U[i,0],U[i,1],words[i])
plt.show()


注意点:

1.画图需要matplotlib,可以pip install matplotlib.

安装中途出现缺少python-tk的错误提示,可以先pip install python-tk,在执行上面的安装,若提示权限不够,前面加sudo

2.画图的时候横坐标和纵坐标范围要确定

plt.axis([Xmin,Xmax,Ymin,Ymax])

3.使用语句plt.show()才会显示出图

GloVe学习之Python中简单的词向量SVD分解_第1张图片

原理什么的还不懂。。。







你可能感兴趣的:(python)