2017-03-24 1 views
0

Gensim는 문서/쿼리 문자열 주어, 다른 문서가 내림차순으로, 그것은 가장 유사한 무슨 말을하는 방법을 말하는 튜토리얼이 있습니다항목 표시

http://radimrehurek.com/gensim/tut3.html

또한 모든에서 전체 모델 과 관련된 어떤 주제를 표시 할 수 있습니다

How to print the LDA topics models from gensim? Python

을하지만 당신은 찾을 어떻게 무엇 주제과 관련된 문서/쿼리 문자열과 관련이 있습니까? 이상적으로 각 주제에 대한 숫자 유사성 측정 항목이 있습니까? 나는 그것에 관해 무엇인가를 발견 할 수 없었다. 당신이 보이지 않는 문서의 주제 분포를 찾으려면

+0

토픽이 쿼리 문자열에 포함되거나 상호 배타적 일 수 있습니까? –

+0

@ NathanMcCoy 상호 배타적입니다. gensim이 주제에 관해 이야기 할 때 단어의 일반적인 영어 의미를 의미하는 것은 아니며 부동 소수점 가중치와 함께 단어 벡터로 구성된 데이터 구조를 의미합니다. – rwallace

답변

1

는 당신이 인덱스가를 대표하는 스파 스 벡터를 얻을이 코드에서 단어 표현

from gensim import utils, models 
from gensim.corpora import Dictionary 
lda = models.LdaModel.load('saved_lda.model') # load saved model 
dictionary = Dictionary.load('saved_dictionary.dict') # load saved dict 
text = ' ' 
with open('document', 'r') as inp: # convert file to string 
    for line in inp: 
     text += line + ' ' 
tkn_doc = utils.simple_preprocess(text) # filter & tokenize words 
doc_bow = dictionary.doc2bow(tkn_doc) # use dictionary to create bow 
doc_vec = lda[doc_bow] # this is the topic probability distribution for the document of interest 

의 가방에 관심있는 문서를 변환 할 필요가 주제 0 .... n이고 각 '가중치'는 문서의 단어가 모델의 해당 주제에 속할 확률입니다. matplotlib를 사용하여 막대 그래프를 작성하여 분포를 시각화 할 수 있습니다.

enter image description here

당신이 print them like this 할 수있는 각 항목의을 TopN 조건을보고 싶다면

y_axis = [] 
x_axis = [] 
for topic_id, dist in enumerate(doc_vec): 
    x_axis.append(topic_id + 1) 
    y_axis.append(dist) 
width = 1 
plt.bar(x_axis, y_axis, width, align='center', color='r') 
plt.xlabel('Topics') 
plt.ylabel('Probability') 
plt.title('Topic Distribution for doc') 
plt.xticks(np.arange(2, len(x_axis), 2), rotation='vertical', fontsize=7) 
plt.subplots_adjust(bottom=0.2) 
plt.ylim([0, np.max(y_axis) + .01]) 
plt.xlim([0, len(x_axis) + 1]) 
plt.savefig(output_path) 
plt.close() 
. 그래프를 참조하여 인쇄 한 상위 단어를 찾아 모델이 문서를 해석 한 방법을 결정할 수 있습니다. hellinger distance, euclidean, jensen shannon 등과 같은 벡터 계산을 사용하여 두 가지 문서 확률 분포 벡터 간의 거리를 찾을 수도 있습니다.