来自 RDD 的 PySpark LDA 模型密集向量

PySpark LDA Model Dense Vector from RDD

我将数据设置为输入 Apache Spark LDA 模型。我遇到的一个问题是将列表转换为 Dense Vector,因为我的 RDD 中有一些字母数字值。我在尝试 运行 示例代码时收到的错误是关于将字符串转换为浮点数。

我知道这个错误,因为我知道我对密集向量和浮点数的了解,但必须有一种方法可以将这些字符串值加载到 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.

因此,您需要先从自己的语料库中获取这些字数统计向量,然后再继续尝试。