我正在执行主题建模并使用函数来获取主题模型中的顶级关键字,如下所示。
def getTopKWords(self, K):
results = []
"""
returns top K discriminative words for topic t
ie words v for which p(v|t) is maximum
"""
index = []
key_terms = []
pseudocounts = np.copy(self.n_vt)
normalizer = np.sum(pseudocounts, (0))
pseudocounts /= normalizer[np.newaxis, :]
for t in range(self.numTopics):
topWordIndices = pseudocounts[:, t].argsort()[-1:-(K+1):-1]
vocab = self.vectorizer.get_feature_names()
print (t, [vocab[i] for i in topWordIndices])
## Code for storing the values in a single list
return results
上面的函数给了我如图所示的代码
0 ['computer', 'laptop', 'mac', 'use', 'bought', 'like', 'warranty', 'screen', 'way', 'just']
1 ['laptop', 'computer', 'use', 'just', 'like', 'time', 'great', 'windows', 'macbook', 'months']
2 ['computer', 'great', 'laptop', 'mac', 'buy', 'just', 'macbook', 'use', 'pro', 'windows']
3 ['laptop', 'computer', 'great', 'time', 'battery', 'use', 'apple', 'love', 'just', 'work']
它是循环运行 4 次并打印索引和每个词汇中的所有关键字的结果。
现在,我想从返回以下输出的函数中返回一个列表。
return [keyword1, keyword2, keyword3, keyword4]
其中,keyword1/2/3/4 是在输出中索引为 0、1、2、3 的词汇表中出现次数最多的单词。
临摹微笑
相关分类