了解 Spark MLlib LDA 输入格式

Understanding Spark MLlib LDA input format

我正在尝试使用 Spark MLlib 实现 LDA。

但是我很难理解输入格式。我能够 运行 它的示例实现从一个只包含数字的文件中获取输入,如图所示:

1 2 6 0 2 3 1 1 0 0 3
1 3 0 1 3 0 0 2 0 0 1
1 4 1 0 0 4 9 0 1 2 0
2 1 0 3 0 0 5 0 2 3 9
3 1 1 9 3 0 2 0 0 1 3
4 2 0 3 4 5 1 1 1 4 0
2 1 0 3 0 0 5 0 2 2 9
1 1 1 9 2 1 2 0 0 1 3
4 4 0 3 4 2 1 3 0 0 0
2 8 2 0 3 0 2 0 2 7 2
1 1 1 9 0 2 2 0 0 3 3
4 1 0 0 4 5 1 3 0 1 0

我关注了 http://spark.apache.org/docs/latest/mllib-clustering.html#latent-dirichlet-allocation-lda

我理解它的输出格式,正如所解释的那样 here

我的用例很简单,我有一个包含一些句子的数据文件。 我想将此文件转换为语料库,以便将其传递给 org.apache.spark.mllib.clustering.LDA.run().

我的疑问是输入中的那些数字代表什么,然后是 zipWithIndex 并传递给 LDA?到处出现的数字1是代表同一个词还是某种计数?

首先你需要将你的句子转换成向量。

val documents: RDD[Seq[String]] = sc.textFile("yourfile").map(_.split("      ").toSeq)

val hashingTF = new HashingTF()
val tf: RDD[Vector] = hashingTF.transform(documents)
val idf = new IDF().fit(tf)
val tfidf: RDD[Vector] = idf.transform(tf)
val corpus = tfidf.zipWithIndex.map(_.swap).cache()

 // Cluster the documents into three topics using LDA
val ldaModel = new LDA().setK(3).run(corpus)

详细了解 TF_IDF 矢量化 here