【发布时间】:2020-04-19 20:32:18
【问题描述】:
这是我正在使用的代码。在没有 data.take 的情况下它运行良好,但在 pyspark python 中使用它时会出错
from pyspark.mllib.recommendation import ALS, MatrixFactorizationModel, Rating
data = sc.textFile("re_u.data")
pData=data.take(2000)
ratings = pData.map(lambda l: l.split(','))\
.map(lambda l: Rating(int(l[0]), int(l[1]), float(l[2])))
给出错误
AttributeError Traceback (most recent call last)
<ipython-input-12-c9c51af1b2e9> in <module>
2 data = sc.textFile("re_u.data")
3 pData=data.take(2000)
----> 4 ratings = pData.map(lambda l: l.split(','))\
5 .map(lambda l: Rating(int(l[0]), int(l[1]), float(l[2])))
AttributeError: 'list' object has no attribute 'map'
更新: 使用您的更改@Hristo Iliev 后,它有所帮助,但遇到了另一个问题,随后是评级作为列表。感谢您的帮助!
from pyspark.mllib.recommendation import ALS, MatrixFactorizationModel, Rating
data = sc.textFile("re_u.data")
ratings = data.map(lambda l: l.split(','))\
.map(lambda l: Rating(int(l[0]), int(l[1]), float(l[2])))\
.take(2000)
rank = 20
numIterations = 20
model = ALS.train(ratings, rank, numIterations)
报错
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-7e35afff970b> in <module>
1 rank = 20
2 numIterations = 20
----> 3 model = ALS.train(ratings, rank, numIterations)
C:\spark\spark-3.0.0-preview2-bin-hadoop2.7\python\pyspark\mllib\recommendation.py in train(cls, ratings, rank, iterations, lambda_, blocks, nonnegative, seed)
271 (default: None)
272 """
--> 273 model = callMLlibFunc("trainALSModel", cls._prepare(ratings), rank, iterations,
274 lambda_, blocks, nonnegative, seed)
275 return MatrixFactorizationModel(model)
C:\spark\spark-3.0.0-preview2-bin-hadoop2.7\python\pyspark\mllib\recommendation.py in _prepare(cls, ratings)
227 else:
228 raise TypeError("Ratings should be represented by either an RDD or a DataFrame, "
--> 229 "but got %s." % type(ratings))
230 first = ratings.first()
231 if isinstance(first, Rating):
TypeError: Ratings should be represented by either an RDD or a DataFrame, but got <class 'list'>.
请帮忙!
【问题讨论】:
-
欢迎来到 Stack Overflow。您最初没有展示您是如何使用
ratings的。请始终提供完整的上下文,因为这会改变答案。 -
请不要在您的原始问题得到回答后通过添加后续问题来更新您的帖子。接受答案,并在新标题下提出新问题。
标签: python-3.x apache-spark pyspark jupyter-notebook rdd