recommendProductsForUsers 不适用于 Spark 1.5.0

recommendProductsForUsers not working in Spark 1.5.0

鉴于以下情况:

from pyspark import SparkContext, SparkConf
from pyspark.mllib.recommendation import ALS, Rating
r1 = (1, 1, 1.0)
r2 = (1, 2, 2.0)
r3 = (2, 1, 2.0)
ratings = sc.parallelize([r1, r2, r3])
model = ALS.trainImplicit(ratings, 1, seed=10)

res = model.recommendProductsForUsers(2)

我想为每个用户计算前 k 个产品。一般来说,用户和产品可能很多,创建一个与 recommendProducts 一起使用的 RDD 成本太高。

根据 Spark version 1.5.0 recommendProductsForUsers 应该做的工作。但是,我收到以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-c65e6875ea5b> in <module>()
      7 model = ALS.trainImplicit(ratings, 1, seed=10)
      8 
----> 9 res = model.recommendProductsForUsers(2)

AttributeError: 'MatrixFactorizationModel' object has no attribute 'recommendProductsForUsers'

而且,事实上,在列出 matrixFactorizationModel 的方法时 recommendProductsForUsers 并没有出现:

print dir(model)
['__class__', '__del__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_java_loader_class', '_java_model', '_load_java', '_sc', 'call', 'load', 'predict', 'predictAll', 'productFeatures', 'rank', 'recommendProducts', 'recommendUsers', 'save', 'userFeatures']

您查看的文档有误。一个简单的事实是,某些操作是在 Scala 或 Java API 中实现的,但这并不意味着它会暴露给 PySpark。如果您检查 PySpark 1.5 API docs,您会发现它没有提供请求的方法。

recommendUsersForProducts and recommendProductsForUsers have been introduced in PySpark 1.6 with SPARK-10535.