2013-07-15 7 views
6

lda.show_topics निम्नलिखित कोड से मॉड्यूल केवल प्रत्येक विषय के लिए शीर्ष 10 शब्दों के वितरण को प्रिंट करता है, मैं कॉर्पस में सभी शब्दों के पूर्ण वितरण को कैसे प्रिंट करूं?gensim में एलडीए विषय में शब्दों के पूर्ण वितरण को मुद्रित करने के लिए कैसे?

from gensim import corpora, models 

documents = ["Human machine interface for lab abc computer applications", 
"A survey of user opinion of computer system response time", 
"The EPS user interface management system", 
"System and human system engineering testing of EPS", 
"Relation of user perceived response time to error measurement", 
"The generation of random binary unordered trees", 
"The intersection graph of paths in trees", 
"Graph minors IV Widths of trees and well quasi ordering", 
"Graph minors A survey"] 

stoplist = set('for a of the and to in'.split()) 
texts = [[word for word in document.lower().split() if word not in stoplist] 
     for document in documents] 

dictionary = corpora.Dictionary(texts) 
corpus = [dictionary.doc2bow(text) for text in texts] 

lda = models.ldamodel.LdaModel(corpus_tfidf, id2word=dictionary, num_topics=2) 

for i in lda.show_topics(): 
    print i 
+0

आप hacky बात कर सकता है, और साइट-संकुल में lda पैकेज बदल (या जहाँ भी यह आपके कंप्यूटर पर है) अपने कार्यक्रम में उन सभी को प्रिंट, या इसके लिए अपने कोड को कॉपी, और इसे बदलने के 10. – debianplebian

+0

के बजाय सभी को प्रिंट करने के लिए अभी जवाब मिला है, यह api = में छिपा हुआ है)। – alvas

+0

नीचे दिए गए उत्तर को अपना उत्तर ढूंढने के लिए अच्छा काम देखें। – debianplebian

उत्तर

8

show_topics() में एक चर कॉल topn जहां शीर्ष N शब्द आप को प्रत्येक विषय से अधिक शब्द वितरण से की आवश्यकता होती है की संख्या निर्दिष्ट कर सकते है। http://radimrehurek.com/gensim/models/ldamodel.html

तो डिफ़ॉल्ट lda.show_topics() के बजाय देखें। num_words सबसे महत्वपूर्ण शब्द (प्रति विषय 10 शब्दों,

for i in lda.show_topics(topn=len(dictionary)): 
    print i 
3

वहाँ विषयों की num_topics संख्या के लिए, दो चर कॉल num_topics और show_topics() में num_words कर रहे हैं, से वापसी: आप प्रत्येक विषय के लिए पूर्ण शब्द वितरण के लिए उपयोग कर सकते हैं len(dictionary) चूक)। देख http://radimrehurek.com/gensim/models/ldamodel.html#gensim.models.ldamodel.LdaModel.show_topics

तो आप अपने lda मॉडल में प्रत्येक विषय के लिए पूर्ण शब्द वितरण के लिए len(lda.id2word), और lda.num_topics सभी विषयों के लिए उपयोग कर सकते हैं।

for i in lda.show_topics(formatted=False,num_topics=lda.num_topics,num_words=len(lda.id2word)): 
    print i 
+0

कृपया अपना उत्तर दें। एसओ सवालों के जवाब देने के लिए अस्तित्व में नहीं है, बल्कि लोगों को सीखने में मदद करने के लिए मौजूद है। कोड केवल उत्तर कम गुणवत्ता माना जाता है – Machavity

0

नीचे दिया गया कोड आपके शब्दों को और साथ ही उनकी संभावनाओं को प्रिंट करेगा। मैंने शीर्ष 10 शब्दों को मुद्रित किया है। आप प्रति विषय अधिक शब्दों को मुद्रित करने के लिए num_words = 10 बदल सकते हैं।

for words in lda.show_topics(formatted=False,num_words=10): 
    print(words[0]) 
    print("******************************") 
    for word_prob in words[1]: 
     print("(",dictionary[int(word_prob[0])],",",word_prob[1],")",end = "") 
    print("") 
    print("******************************") 
संबंधित मुद्दे