2017-01-31 9 views
4

私は、ppparkのLDAModelを使用して、コーパスからトピックを取得しています。私の目標はのというトピックを各文書に関連付けることです。その目的のために、ドキュメントごとにtopicDistributionColを設定しようとしました。私はこれに新しいので、私はこのコラムの目的が何であるか分かりません。pyspark(2.1.0)LdAを使用して各文書に関連するトピックを取得するには?

from pyspark.ml.clustering import LDA 
lda_model = LDA(k=10, optimizer="em").setTopicDistributionCol("topicDistributionCol") 
// documents is valid dataset for this lda model 
lda_model = lda_model.fit(documents) 
transformed = lda_model.transform(documents) 

topics = lda_model.describeTopics(maxTermsPerTopic=num_words_per_topic) 
print("The topics described by their top-weighted terms:") 
print topics.show(truncate=False) 

termIndicesとtermWeightsですべてのトピックをリストします。コードの下

enter image description here

私をtopicDistributionCol与えます。ここで、各行は各文書に対応しています。

print transformed.select("topicDistributionCol").show(truncate=False) 

enter image description here

私はこのような文書のトピック行列を取得したいです。 pysparks LDAモデルで可能ですか?

doc | topic 
1 | [2,4] 
2 | [3,4,6] 

注:私はこれまで、次のコードでgensims LDAモデルを使用しました。しかし、私はpysparks LDAモデルを使用する必要があります。

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] 
doc_topics = LdaModel(corpus=corpus, id2word=dictionary, num_topics=10, passes=10) 
## to fetch topics for one document 
vec_bow = dictionary.doc2bow(text[0]) 
Topics = doc_topics[vec_bow] 
Topic_list = [x[0] for x in Topics] 
## topic list is [1,5] 

答えて

0

この質問には簡単な回答があると思います。次の操作を行います。

transformed.take(10) 

出力の最後の列は、ドキュメントトピックの配布である「topicDistribution」になります。

関連する問題