Kardu
Kardu

Reputation: 885

pyspark ml recommendation - All recomendation

Hy,

I'm new in Spark and I'm trying using ML recommendation.

My Code

df = sqlContext.createDataFrame(
[(0, 0, 4.0), (0, 1, 2.0), (1, 1, 3.0), (1, 2, 4.0), (2, 1, 1.0), (2, 2, 5.0)],
["user", "item", "rating"])

als = ALS(rank=10, maxIter=5)

model = als.fit(df)

model.userFactors.orderBy("id").collect()

How can I obtain 2 recommendation for all users for all movies?

thanks for you time.

Upvotes: 4

Views: 1060

Answers (1)

zero323
zero323

Reputation: 330073

It is not directly possible with ml.recommendation.ALSModel. You could use transform method

users = df.select("user").distinct()
items = df.select("item").distinct()

model.transform(users.join(items))

and filter the results afterwards but it is extremely inefficient. As far as I can tell it would be better to simply use mllib.recommendation.ALS here:

from pyspark.mllib.recommendation import ALS, Rating

model = ALS.train(df.rdd.map(lambda r: Rating(*r)), 10, 5)
model.recommendProductsForUsers(2)

Upvotes: 5

Related Questions