【问题标题】:Extract document-topic matrix from Pyspark LDA Model从 Pyspark LDA 模型中提取文档主题矩阵
【发布时间】:2016-01-09 10:04:58
【问题描述】:

我已经通过 Python API 在 Spark 中成功训练了一个 LDA 模型:

from pyspark.mllib.clustering import LDA
model=LDA.train(corpus,k=10)

这完全没问题,但我现在需要 LDA 模型的 document-topic 矩阵,但据我所知,我能得到的只是 word -topic,使用model.topicsMatrix()。

是否有某种方法可以从 LDA 模型中获取文档主题矩阵,如果没有,是否有在 Spark 中运行 LDA 模型的替代方法(除了从头开始实现 LDA),它会给我我的结果需要吗?

编辑:

经过一番挖掘,我在 Java api 中找到了 DistributedLDAModel 的文档,其中有一个 topicDistributions() 我认为这正是我需要的(但我 100% 确定 Pyspark 中的 LDAModel实际上是一个 DistributedLDAModel ...)。

无论如何,我都可以像这样间接调用此方法,而不会出现任何明显的失败:

In [127]: model.call('topicDistributions')
Out[127]: MapPartitionsRDD[3156] at mapPartitions at PythonMLLibAPI.scala:1480

但如果我真的查看结果,我得到的只是字符串告诉我结果实际上是一个 Scala 元组(我认为):

In [128]: model.call('topicDistributions').take(5)
Out[128]:
[{u'__class__': u'scala.Tuple2'},
 {u'__class__': u'scala.Tuple2'},
 {u'__class__': u'scala.Tuple2'},
 {u'__class__': u'scala.Tuple2'},
 {u'__class__': u'scala.Tuple2'}]

也许这通常是正确的方法,但有没有办法获得实际结果?

【问题讨论】:

  • 我知道 Spark 中的 LDA 功能仍在开发中,但似乎很奇怪,没有直接的方法可以从模型中获取这些信息...
  • 我认为这里还有另一个问题。正如Jason LendermanSpark LDA 向我指出的那样,它没有实现 LSA,而是 PLSI 的一种变体。它使这个矩阵直接变得不那么有用。另见stackoverflow.com/a/32953813/1560062
  • 我明白了,但在这种情况下,或多或少等效的解决方案是预测原始培训文档的主题,类似于链接问题中描述的方法,但据我所知Python API 中没有实现方法。它们是隐藏在某个地方,还是在 Pyspark 中有其他方法可以实现这一点?
  • 据我所知,它无法从 Python 访问。
  • pyspark 2.0.0 这个问题有答案吗?

标签: python apache-spark pyspark lda


【解决方案1】:

以下内容扩展了 PySpark 和 Spark 2.0 的上述响应。

我希望您能原谅我将此作为回复而不是评论发布,但我目前缺少代表。

我假设您有一个训练有素的 LDA 模型,由这样的语料库制成:

lda = LDA(k=NUM_TOPICS, optimizer="em")
ldaModel = lda.fit(corpus) # Where corpus is a dataframe with 'features'.

为了将文档转换为主题分布,我们创建了文档 ID 的数据框和单词的向量(稀疏更好)。

documents = spark.createDataFrame([
    [123myNumericId, Vectors.sparse(len(words_in_our_corpus), {index_of_word:count}],
    [2, Vectors.sparse(len(words_in_our_corpus), {index_of_word:count, another:1.0}],
], schema=["id", "features"]
transformed = ldaModel.transform(documents)
dist = transformed.take(1)
# dist[0]['topicDistribution'] is now a dense vector of our topics.

【讨论】:

  • 你能分享你使用的完整代码吗?我在运行 hte 转换方法的 Pyspark 和 Spark 2.0 中遇到问题……(据说它不可用)
  • transform到dist对象中究竟返回了哪些数据?那里有所有的 scala API 输出,包括 topTopicsPerDocument 等吗?为什么看起来我们是第一个尝试使用 Spark 的 LDA 的人类?最好完全避免使用此代码.... 看起来很 alpha 级。
  • 显然,上面的示例代码通过 Spark 2+ 工作。更多信息请查看spark.apache.org/docs/2.1.0/api/python/…。
【解决方案2】:

从 Spark 2.0 开始,您可以使用 transform() 作为来自 pyspark.ml.clustering.DistributedLDAModel 的方法。我刚刚在 scikit-learn 的 20 个新闻组数据集上尝试了这个,它可以工作。查看返回的vectors,它是文档主题的分布。

>>> test_results = ldaModel.transform(wordVecs)
Row(filename='/home/jovyan/work/data/20news_home/20news-bydate-test/rec.autos/103343', target=7, text='I am a little confused on all of the models of the 88-89 bonnevilles.\nI have heard of the LE SE LSE SSE SSEI. Could someone tell me the\ndifferences are far as features or performance. I am also curious to\nknow what the book value is for prefereably the 89 model. And how much\nless than book value can you usually get them for. In other words how\nmuch are they in demand this time of year. I have heard that the mid-spring\nearly summer is the best time to buy.', tokens=['little', 'confused', 'models', 'bonnevilles', 'someone', 'differences', 'features', 'performance', 'curious', 'prefereably', 'usually', 'demand', 'spring', 'summer'], vectors=SparseVector(10977, {28: 1.0, 29: 1.0, 152: 1.0, 301: 1.0, 496: 1.0, 552: 1.0, 571: 1.0, 839: 1.0, 1114: 1.0, 1281: 1.0, 1288: 1.0, 1624: 1.0}), topicDistribution=DenseVector([0.0462, 0.0538, 0.045, 0.0473, 0.0545, 0.0487, 0.0529, 0.0535, 0.0467, 0.0549, 0.051, 0.0466, 0.045, 0.0487, 0.0482, 0.0509, 0.054, 0.0472, 0.0547, 0.0501]))

【讨论】:

  • 版主注意,我删除了我用这个答案创建的另一个帖子。
  • 你的例子中的 wordVecs 是什么?
【解决方案3】:

经过广泛研究,通过当前版本的 Spark (1.5.1) 上的 Python api 绝对不可能做到这一点。但在 Scala 中,它相当简单(给定一个 RDD documents 进行训练):

import org.apache.spark.mllib.clustering.{LDA, DistributedLDAModel}

// first generate RDD of documents...

val numTopics = 10
val lda = new LDA().setK(numTopics).setMaxIterations(10)
val ldaModel = lda.run(documents)

# then convert to distributed LDA model
val distLDAModel = ldaModel.asInstanceOf[DistributedLDAModel]

那么获取文档主题分布就这么简单:

distLDAModel.topicDistributions

【讨论】:

    猜你喜欢
    • 2015-11-15
    • 2016-07-11
    • 2015-01-29
    • 2020-07-07
    • 2022-01-14
    • 2017-02-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    相关资源
    最近更新 更多