Doc2vec:聚类结果向量

时间:2017-12-21 18:34:45

标签: python nlp gensim doc2vec

在doc2vec模型中,我们可以将这些载体本身聚类吗?我们应该将每个生成的model.docvecs[1]向量聚类吗?如何实现聚类模型?

 model = gensim.models.doc2vec.Doc2Vec(size= 100, min_count = 5,window=4, iter = 50, workers=cores)
    model.build_vocab(res) 
    model.train(res, total_examples=model.corpus_count, epochs=model.iter)


    # each of length 100
    len(model.docvecs[1])

1 个答案:

答案 0 :(得分:1)

您可以直接使用模型中的文档向量来拟合(例如)k-means聚类算法。然后使用质心标记文档。

from scipy.cluster.vq import kmeans,vq

NUMBER_OF_CLUSTERS = 15

centroids, _ = kmeans(model.docvecs, NUMBER_OF_CLUSTERS)

# computes cluster Id for document vectors
doc_ids,_ = vq(model.docvecs,centroids)

# zips cluster Ids back to document labels 
doc_labels = zip(model.docvecs.doctags.keys(), doc_ids)

# outputs document label and the corresponding cluster label
[('doc_216', 0),
 ('doc_217', 12),
 ('doc_214', 13),
 ('doc_215', 11),
 ('doc_212', 13),
 ('doc_213', 11),
 ('doc_210', 5),
 ('doc_211', 13),
 ('doc_165', 0),
 ... ]

如果不需要将每个文档与群集匹配,则可以使用质心进行检索(使用gensim)。例如,将最近的10个文档放到质心(簇)1。

model.docvecs.most_similar(positive = [centroids[1]], topn = 10)

# outputs document label and a similarity score
[('doc_243', 0.9186744689941406),
 ('doc_74', 0.9134798049926758),
 ('doc_261', 0.8858329057693481),
 ('doc_88', 0.8851054906845093),
 ('doc_276', 0.8691701292991638),
 ('doc_249', 0.8666893243789673),
 ('doc_233', 0.8334537148475647),
 ('doc_292', 0.8269758224487305),
 ('doc_98', 0.8193566799163818),
 ('doc_82', 0.808419942855835)]