# 加载训练好的Word2Vec模型
# 需要 4.0_训练word2vec模型.ipynb 的执行结果
from gensim.models import Word2Vec
w2v = Word2Vec.load('output_word2vec/model.w2v')
# 使用文章中所有词的平均词向量作为文章的向量
from gensim.models import Word2Vec
w2v = Word2Vec.load('output_word2vec/model.w2v')
import jieba
def compute_doc_vec_single(article):
vec = np.zeros((w2v.layer1_size,), dtype=np.float32)
n = 0
for word in jieba.cut(article):
if word in w2v:
vec += w2v[word]#求所有词向量的和
n += 1#计算词的个数
return vec / n#求平均值
错误是这样的
TypeError Traceback (most recent call last)
<ipython-input-16-86dff7917d17> in <module>
----> 1 x = compute_doc_vec(train_data[u'文章'])
<ipython-input-13-a2efc13466bf> in compute_doc_vec(articles)
1 def compute_doc_vec(articles):
----> 2 return np.row_stack([compute_doc_vec_single(x) for x in articles])
<ipython-input-13-a2efc13466bf> in <listcomp>(.0)
1 def compute_doc_vec(articles):
----> 2 return np.row_stack([compute_doc_vec_single(x) for x in articles])
<ipython-input-12-42416e5a7df9> in compute_doc_vec_single(article)
7 n = 0
8 for word in jieba.cut(article):
----> 9 if word in w2v:
10 vec += w2v[word]#求所有词向量的和
11 n += 1#计算词的个数
TypeError: argument of type 'Word2Vec' is not iterable