我设置了数据以输入 Apache Spark LDA 模型。我遇到的一个难题是将列表转换为密集向量,因为我的 RDD 中有一些字母数字值。我在尝试运行示例代码时收到的错误是将字符串转换为 float 。
我了解这个错误,知道我对密集向量和 float 的了解,但必须有一种方法将这些字符串值加载到 LDA 模型中,因为这是一个主题模型。
我应该在开头声明我是 Python 和 Spark 的新手,所以如果我误解了某些内容,我深表歉意。我将在下面添加我的代码。预先感谢您!
示例
https://spark.apache.org/docs/latest/mllib-clustering.html#latent-dirichlet-allocation-lda
代码:
>>> rdd = rdd5.take(3)
[[u'11394071', u'11052103', u'11052101'], [u'11847272', u'11847272',
u'11847272', u'11847272', u'11847272', u'11847272', u'11847272',
u'11847272', u'11847272', u'11847272', u'999999', u'11847272',
u'11847272', u'11847272', u'11847272', u'11847272', u'11847272',
u'11847272', u'11847272', u'11847272', u'11847272'], [u'af1lowprm1704',
u'af1lowprm1704', u'af1lowprm1704', u'af1lowprm1704', u'af1lowprm1704',
u'am1prm17', u'am1prm17', u'af1highprm1704', u'af1highprm1704']]
>>> parsedData = rdd.map(lambda line: Vectors.dense([float(x) for x in
line]))
ValueError: could not convert string to float: af1lowprm1704
代码修复后的后续步骤:
# Index Document with Unique ID's
corpus = parsedData.zipWithIndex().map(lambda x: [x[1], x[0]]).cache()
# Cluster the documents into three topics using LDA
ldaModel = LDA.train(corpus, k=3)
最佳答案
您确实误解了该示例:文件 sample_lda_data.txt
不包含文本 ( check it ),而是包含已从语料库中提取的字数向量。示例前面的文本已表明这一点:
In the following example, we load word count vectors representing a corpus of documents.
因此,您需要先从自己的语料库中获取这些字数向量,然后再继续尝试。
关于apache-spark - 来自 RDD 的 PySpark LDA 模型密集向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45641892/