【问题标题】:Cannot import name LDA MLlib in Spark无法在 Spark 中导入名称 LDA MLlib
【发布时间】:2016-02-05 15:08:16
【问题描述】:

我正在尝试使用 Spark 实现 LDA 并收到此错误。我对 Spark 完全不熟悉,因此感谢您提供任何帮助。

[root@sandbox ~]# spark-submit ./lda.py
Traceback (most recent call last):
  File "/root/./lda.py", line 3, in <module>
    from pyspark.mllib.clustering import LDA, LDAModel
ImportError: cannot import name LDA

代码如下:

from pyspark.sql import SQLContext
from pyspark import SparkContext
from pyspark.mllib.clustering import LDA, LDAModel
from pyspark.mllib.linalg import Vectors
import numpy
sc = SparkContext(appName="PythonLDA")
data = sc.textFile("/tutorial/input/askreddit20150801.txt")
parsedData = data.map(lambda line: Vectors.dense([float(x) for x in line.strip().split(' ')]))
# Index documents with unique IDs
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)

# Output topics. Each is a distribution over words (matching word count vectors)
print("Learned topics (as distributions over vocab of " + str(ldaModel.vocabSize()) + " words):")
topics = ldaModel.topicsMatrix()
for topic in range(3):
    print("Topic " + str(topic) + ":")
    for word in range(0, ldaModel.vocabSize()):
        print(" " + str(topics[word][topic]))

# Save and load model
model.save(sc, "myModelPath")
sameModel = LDAModel.load(sc, "myModelPath")

当我尝试安装 pyspark.mllib.clustering 时:

[root@sandbox ~]# pip install spark.mllib.clustering
Collecting spark.mllib.clustering
/usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Could not find a version that satisfies the requirement spark.mllib.clustering (from versions: )
No matching distribution found for spark.mllib.clustering

【问题讨论】:

    标签: python apache-spark pyspark lda apache-spark-mllib


    【解决方案1】:

    用于 LDA 的 PySpark 包装器已在 Spark 1.5.0 中引入。假设您的安装没有损坏,您可能使用 Spark

    【讨论】:

    • 所以我应该将我的 Spark 更新到 1.5.0 吗?我怎么做?我按照以下说明在 EC2 上运行它:d396qusza40orc.cloudfront.net/cloudapplications/tutorials/week1/…d396qusza40orc.cloudfront.net/cloudapplications/tutorials/week2/…
    • 如果你想将 LDA 与 PySpark 一起使用,是的,但老实说 Python 接口相当有限。我暂时不会打扰它。
    • 嗯好的谢谢你的信息。那我应该使用Java吗? LDA 可以在 Spark 1.3.1 for Java 中工作吗?
    • Scala(或 Java)如果你想使用 1.3,但老实说,如果你想要的只是学习/测试,我会推荐 1.5,在独立模式下,Spark 需要很少的配置来处理中小型数据集。
    • 实际上我需要分析一个相当大的数据集,这就是我不得不切换到 Spark 的原因。我想在 Spark 1.3 中我会切换到 Java。
    猜你喜欢
    • 2016-10-18
    • 2015-10-20
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2017-01-08
    • 2018-06-29
    • 2017-04-11
    相关资源
    最近更新 更多