【发布时间】:2017-12-23 12:24:42
【问题描述】:
上下文
我正在尝试使用来自Scikit-Learn's decomposition module 的Latent Dirichlet allocation 从一组文本中提取主题。 除了找到/选择的主题词的质量之外,这非常有效。
在Li et al (2017) 的一篇文章中,作者描述了使用先前的主题词作为 LDA 的输入。他们手动选择 4 个主题以及与这些主题相关/属于这些主题的主要词。对于这些词,他们将相关主题的默认值设置为大数字,而将其他主题的默认值设置为 0。对于所有主题 (1),所有其他词(不是为主题手动选择的)都被赋予相同的值。此值矩阵用作 LDA 的输入。
我的问题
如何使用 Scikit-Learn 的 LatentDirichletAllocation 模块使用自定义默认值矩阵(先前主题词)作为输入创建类似的分析?
(我知道有一个topic_word_prior 参数,但它只需要一个浮点数而不是具有不同“默认值”的矩阵。)
编辑
解决方案
使用@Anis 的帮助,我创建了原始模块的子类,并编辑了设置起始值矩阵的函数。对于您希望作为输入提供的所有先前主题词,它通过将值与该(先前)词的主题值相乘来转换 components_ 矩阵。
这是代码:
# List with prior topic words as tuples
# (word index, [topic values])
prior_topic_words = []
# Example (word at index 3000 belongs to topic with index 0)
prior_topic_words.append(
(3000, [(np.finfo(np.float64).max/4),0.,0.,0.,0.])
)
# Custom subclass for PTW-guided LDA
from sklearn.utils import check_random_state
from sklearn.decomposition._online_lda import _dirichlet_expectation_2d
class PTWGuidedLatentDirichletAllocation(LatentDirichletAllocation):
def __init__(self, n_components=10, doc_topic_prior=None, topic_word_prior=None, learning_method=’batch’, learning_decay=0.7, learning_offset=10.0, max_iter=10, batch_size=128, evaluate_every=-1, total_samples=1000000.0, perp_tol=0.1, mean_change_tol=0.001, max_doc_update_iter=100, n_jobs=None, verbose=0, random_state=None, n_topics=None, ptws=None):
super(PTWGuidedLatentDirichletAllocation, self).__init__(n_components, doc_topic_prior, topic_word_prior, learning_method, learning_decay, learning_offset, max_iter, batch_size, evaluate_every, total_samples, perp_tol, mean_change_tol, max_doc_update_iter, n_jobs, verbose, random_state, n_topics)
self.ptws = ptws
def _init_latent_vars(self, n_features):
"""Initialize latent variables."""
self.random_state_ = check_random_state(self.random_state)
self.n_batch_iter_ = 1
self.n_iter_ = 0
if self.doc_topic_prior is None:
self.doc_topic_prior_ = 1. / self.n_topics
else:
self.doc_topic_prior_ = self.doc_topic_prior
if self.topic_word_prior is None:
self.topic_word_prior_ = 1. / self.n_topics
else:
self.topic_word_prior_ = self.topic_word_prior
init_gamma = 100.
init_var = 1. / init_gamma
# In the literature, this is called `lambda`
self.components_ = self.random_state_.gamma(
init_gamma, init_var, (self.n_topics, n_features))
# Transform topic values in matrix for prior topic words
if self.ptws is not None:
for ptw in self.ptws:
word_index = ptw[0]
word_topic_values = ptw[1]
self.components_[:, word_index] *= word_topic_values
# In the literature, this is `exp(E[log(beta)])`
self.exp_dirichlet_component_ = np.exp(
_dirichlet_expectation_2d(self.components_))
启动与原始LatentDirichletAllocation 类相同,但现在您可以使用ptws 参数提供先前的主题词。
【问题讨论】:
-
您是否尝试过手动编辑模型的 components_ 矩阵的系数?在我看来,这就是您想要实现的目标。
-
感谢您的快速回复,这就是我想要弄清楚的。 (我不确定我必须/可以调整哪个(内部)属性,以及我可以在其中放入什么范围的值?
-
在我看来,它是您模型的 components_ 矩阵,因为它直接用于训练。您可以使用
model.components_[i, j] = aij为主题 i 和特征 j 设置值 aij。 -
我假设这应该在拟合模型之前发生?值的范围是否重要? (例如,我可以使用 0、1 和大的正浮点数吗?)
标签: python scikit-learn nlp topic-modeling