Gensim 是一款开源的第三方Python工具包,用于从原始的非结构化的文本中,无监督地学习到文本隐层的主题向量表达。它支持包括 TF-IDF,LSA,LDA,和word2vec 在内的多种主题模型算法,支持流式训练,并提供了诸如相似度计算,信息检索等一些常用任务的API接口。
基本概念包括:
texts=[[‘A‘,‘a‘],[‘B‘,‘b‘]]
from gensim import corpora
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
class MyCorpus(object):
def __iter__(self):
# assume there‘s one document per line, tokens separated by whitespace
with open(‘mycorpus.txt‘) as f:
for line in f:
yield dictionary.doc2bow(line.lower().split())
对文本向量的变换是 Gensim 的核心。通过挖掘语料中隐藏的语义结构特征,我们最终可以变换出一个简洁高效的文本向量。
tfidf = models.TfidfModel(MyCorpus)
# 将任意一段语料( bow 向量的迭代器)转化成 TFIDF 向量(的迭代器)
tfidf[bow_corpus]
prepath=‘./‘
tfidf.save(prepath+"tfidf")
tfidf = models.TfidfModel.load(prepath+"tfidf")
# 构造LSI模型并将待检索的query和文本转化为LSI主题向量
# 转换之前的corpus和query均是BOW向量
lsi_model = models.LsiModel(corpus, id2word=dictionary, num_topics=2)
documents = lsi_model[corpus]
query_vec = lsi_model[query]
index = similarities.MatrixSimilarity(documents)
# 省内存
index = similarities.Similarity(documents)
# 计算
# return: an iterator of tuple (idx, sim)
sims = index[query_vec]
index.save(prepath+‘lsi‘)
index = similarities.MatrixSimilarity.load(prepath+‘lsi‘)
把压缩包保存为稀疏矩阵
python -m gensim.scripts.make_wiki source_wiki.bz2 zhwiki
# 生成5个文件
-rw-r--r-- 1 chenbingjin data 172M 7月 1 12:10 zhwiki_bow.mm
-rw-r--r-- 1 chenbingjin data 1.3M 7月 1 12:10 zhwiki_bow.mm.index
-rw-r--r-- 1 chenbingjin data 333M 7月 1 12:16 zhwiki_tfidf.mm
-rw-r--r-- 1 chenbingjin data 1.3M 7月 1 12:16 zhwiki_tfidf.mm.index
-rw-r--r-- 1 chenbingjin data 1.9M 7月 1 12:10 zhwiki_wordids.txt
from gensim import corpora, models
# 语料导入
id2word = corpora.Dictionary.load_from_text(‘zhwiki_wordids.txt‘)
mm = corpora.MmCorpus(‘zhwiki_tfidf.mm‘)
# 模型训练,耗时28m
lda = models.ldamodel.LdaModel(corpus=mm, id2word=id2word, num_topics=100)
# 打印前20个topic的词分布
lda.print_topics(20)
# 打印id为20的topic的词分布
lda.print_topic(20)
lda.save(‘zhwiki_lda.model‘)
lda = models.ldamodel.LdaModel.load(‘zhwiki_lda.model‘)
Gensim 封装了 word2vec 工具的扩展,就不使用 word2vec 包了。
class MySentences(object):
def __init__(self, dirname):
self.dirname = dirname
def __iter__(self):
for fname in os.listdir(self.dirname):
for line in open(os.path.join(self.dirname, fname)):
yield line.split()
sentences = MySentences(prepath+‘copas‘)
w2v = gensim.models.Word2Vec(sentences)
# online training
model = gensim.models.Word2Vec.load(‘/tmp/mymodel‘)
model.train(more_sentences)
class gensim.models.word2vec.Word2Vec(sentences=None, corpus_file=None, size=100, alpha=0.025, window=5, min_count=5, max_vocab_size=None, sample=0.001, seed=1, workers=3, min_alpha=0.0001, sg=0, hs=0, negative=5, ns_exponent=0.75, cbow_mean=1, hashfxn=<built-in function hash>, iter=5, null_word=0, trim_rule=None, sorted_vocab=1, batch_words=10000, compute_loss=False, callbacks=(), max_final_vocab=None)
Doc2vec 也是继承于 word2vec 的一个子类,用于计算长文本向量,与 word2vec 主要的区别是在对输入数据的预处理上。
Doc2vec 接受一个由 LabeledSentence 对象组成的迭代器作为其构造函数的输入参数。其中,LabeledSentence 是 Gensim 内建的一个类,它接受两个List作为其初始化的参数:word list 和 label list。
from gensim.models.doc2vec import LabeledSentence
sentence = LabeledSentence(words=[u‘some‘, u‘words‘, u‘here‘], tags=[u‘SENT_1‘])
class LabeledLineSentence(object):
def __init__(self, filename):
self.filename = filename
def __iter__(self):
for uid, line in enumerate(open(filename)):
yield LabeledSentence(words=line.split(), labels=[‘SENT_%s‘ % uid])
同时训练了 word 和 sentence label 的语义向量。
如果我们只想训练 label 向量,可以传入参数train_words=False以固定词向量参数。参考API文档
from gensim.models import Doc2Vec
d2v = Doc2Vec(dm=1, size=100, window=5, negative=5, hs=0, min_count=2, workers=4)
原文:https://www.cnblogs.com/gxli/p/13160612.html