【问题标题】:Print document wise topics in Gensim在 Gensim 中打印文档明智的主题
【发布时间】:2017-04-27 22:12:17
【问题描述】:

我正在尝试使用 Gensim python 库来学习主题建模。 我尝试了很多不同的教程,包括官方教程。

问题: 如何使用 Gensim 获得文档明智的主题分布。

我当前的输出是主题列表及其关键字和概率,如下所示。

(0, u'0.086*good + 0.086*brocolli + 0.086*health + 0.061*eat)
(1, u'0.068*mother + 0.068*brother + 0.068*drive + 0.041*pressur)

我想知道是否可以列出每个文档以及该特定文档的热门主题?

我的代码如下:

tokenizer = RegexpTokenizer(r'\w+')

# create English stop words list
en_stop = get_stop_words('en')

# Create p_stemmer of class PorterStemmer
p_stemmer = PorterStemmer()

# create sample documents
doc_a = "Brocolli is good to eat. My brother likes to eat good brocolli, but not my mother."
doc_b = "My mother spends a lot of time driving my brother around to baseball practice."
doc_c = "Some health experts suggest that driving may cause increased tension and blood pressure."
doc_d = "I often feel pressure to perform well at school, but my mother never seems to drive my brother to do better."
doc_e = "Health professionals say that brocolli is good for your health." 

# compile sample documents into a list
doc_set = [doc_a, doc_b, doc_c, doc_d, doc_e]
num_topics=2;

# list for tokenized documents in loop
texts = []

# loop through document list
for i in doc_set:

    # clean and tokenize document string
    raw = i.lower()
    tokens = tokenizer.tokenize(raw)

    # remove stop words from tokens
    stopped_tokens = [i for i in tokens if not i in en_stop]
    #print(stopped_tokens)

    # stem tokens
    stemmed_tokens = [p_stemmer.stem(i) for i in stopped_tokens]
    #print("printing stemmed tokens")
    #print(stemmed_tokens)

    # add tokens to list
    texts.append(stemmed_tokens)

# turn our tokenized documents into a id <-> term dictionary
dictionary = corpora.Dictionary(texts)
print("printing each token/words along with unique integer id..")
print(dictionary.token2id)

# convert tokenized documents into a document-term matrix
corpus = [dictionary.doc2bow(text) for text in texts]
print("printing sample bag of words")
print(corpus[0])

# generate LDA model
ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics=2, id2word = dictionary, passes=20)
#print(ldamodel.print_topics(num_topics=3, num_words=3))
#print ldamodel.top_topics(corpus,2)
print(ldamodel.show_topics(num_topics=2, num_words=3, log=False, formatted=True))
print(ldamodel.show_topics())
print("from for loop.")
for i in ldamodel.show_topics(len(dictionary)):
    print i

【问题讨论】:

    标签: python gensim topic-modeling


    【解决方案1】:
    for i in range(len(corpus)):
        print 'doc id:'+str(i)
        print (ldamodel[corpus[i]])
        i=i+1
    

    如果要保存主题分布,请使用以下

    topicsDist=[]
    for x in corpus:
        topics = ldamodel[x]
        temp=[0]* num_topics
        for t in topics:
            temp[t[0]]=t[1]
        topicsDist.append(temp)
    print("topics generated")
    

    第二个代码基本上会为语料库中的每个文档创建一个vectors=num_topics,并在特定主题编号索引中填充概率。..

    【讨论】:

    • 但它没有给出主题名称,只是给出了概率分布
    猜你喜欢
    • 1970-01-01
    • 2013-02-22
    • 2020-12-25
    • 2014-05-26
    • 1970-01-01
    • 2023-04-06
    • 2017-08-17
    • 2022-01-14
    • 2018-03-16
    相关资源
    最近更新 更多