Database Reference
In-Depth Information
As we can see, this model predicts a rating of
3.12
for user
789
and movie
123
.
Tip
Note that you might see different results than those shown in this section because the
ALS
model is initialized randomly. So, different runs of the model will lead to different solu-
tions.
The
predict
method can also take an RDD of
(user, item)
IDs as the input and
will generate predictions for each of these. We can use this method to make predictions
for many users and items at the same time.
To generate the
top-K
recommended items for a user,
MatrixFactorizationModel
provides a convenience method called
recommendProducts
. This takes two argu-
ments:
user
and
num
, where
user
is the user ID, and
num
is the number of items to re-
commend.
It returns the top
num
items ranked in the order of the predicted score. Here, the scores are
computed as the dot product between the user-factor vector and each item-factor vector.
Let's generate the top
10
recommended items for user
789
:
val userId = 789
val K = 10
val topKRecs = model.recommendProducts(userId, K)
We now have a set of predicted ratings for each movie for user
789
. If we print this out,
we could inspect the top 10 recommendations for this user:
println(topKRecs.mkString("\n"))
You should see the following output on your console:
Rating(789,715,5.931851273771102)
Rating(789,12,5.582301095666215)
Rating(789,959,5.516272981542168)
Rating(789,42,5.458065302395629)
Rating(789,584,5.449949837103569)
Rating(789,750,5.348768847643657)
Rating(789,663,5.30832117499004)