【问题标题】:Understanding Spark MLlib LDA input format了解 Spark MLlib LDA 输入格式
【发布时间】:2016-10-18 13:25:49
【问题描述】:

我正在尝试使用 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 出现在各处代表同一个词还是某种计数?

【问题讨论】:

    标签: apache-spark-mllib 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

    【讨论】:

    • 谢谢我终于明白了。
    猜你喜欢
    • 1970-01-01
    • 2016-01-24
    • 2014-09-30
    • 2016-02-05
    • 2017-11-15
    • 2017-07-04
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多