2016-07-22 76 views
0

我正在做一些主題建模,並期待存儲我的分析結果。將生成器表達式(<genexpr>)轉換爲列表?

import pandas as pd, numpy as np, scipy 
import sklearn.feature_extraction.text as text 
from sklearn import decomposition 

descs = ["You should not go there", "We may go home later", "Why should we do your chores", "What should we do"] 

vectorizer = text.CountVectorizer() 

dtm = vectorizer.fit_transform(descs).toarray() 

vocab = np.array(vectorizer.get_feature_names()) 

nmf = decomposition.NMF(3, random_state = 1) 

topic = nmf.fit_transform(dtm) 

topic_words = [] 

for t in nmf.components_: 
    word_idx = np.argsort(t)[::-1][:20] 
    topic_words.append(vocab[i] for i in word_idx) 

for t in range(len(topic_words)): 
    print("Topic {}: {}\n".format(t, " ".join([word for word in topic_words[t]]))) 

打印:

Topic 0: do we should your why chores what you there not may later home go 

Topic 1: should you there not go what do we your why may later home chores 

Topic 2: we may later home go what do should your you why there not chores 

我想寫這些主題的文件,所以我想將它們存儲在一個列表可能會奏效,像這樣:

l = [] 
for t in range(len(topic_words)): 
    l.append([word for word in topic_words[t]]) 
    print("Topic {}: {}\n".format(t, " ".join([word for word in topic_words[t]]))) 

l只是最後一個空的數組。我如何將這些單詞存儲在列表中?

+0

'[在topic_words一個字一個字[T]] = = list(topic_words [t])' –

+0

題外話題:不要使用'l'作爲變量名,它看起來太像數字'1'。 –

回答

3

您正在將生成器表達式附加到列表topic_words,因此第一次打印時,生成器表達式已經用盡。你可以做,而不是:

topic_words = [] 

for t in nmf.components_: 
    word_idx = np.argsort(t)[::-1][:20] 
    topic_words.append([vocab[i] for i in word_idx]) 
#     ^      ^

有了這個,你顯然不會需要一個新的列表,你可以打印出:

for t, words in enumerate(topic_words, 1): 
    print("Topic {}: {}\n".format(t, " ".join(words))) 
相關問題