如何为不包括他在使用 spark MLlib ALS 算法的 scala 中评分的电影的用户生成个人推荐?

How to generate personal recommendations for user which excludes movies he rated in scala which is using spark MLlib ALS algorithm?

我目前正计划在 MovieLens 数据集上使用 ALS 算法为用户生成电影推荐,一切正常,但有时 ALS 算法 return 已经评级的电影,我想将它们从推荐中排除我目前尝试生成这样的建议如下。

`val moviesRatedbyUser = ratings.keyBy(_._2.user).lookup(206547)
 println("rated movies are" + moviesRatedbyUser) 
 val candidates = 
 sc.parallelize(movies.keys.filter(!moviesRatedbyUser(_)).toSeq)
 val recommendations = bestModel.get
    .predict(candidates.map((206547, _)))
    .collect()
    .sortBy(- _.rating)
    .take(10)

var i = 1
println("Movies recommended for you:")
recommendations.foreach { r =>
println("%2d".format(i) + ": " + movies(r.product))
i += 1
}`

这里我尝试在 ratings rdd 打印语句中查找用户标识 returned moviesRatedbyUser: Seq[(Long, org.apache.spark.mllib.recommendation.Rating)] = WrappedArray((3,Rating(206547,80,1.0))) 我想知道如何获取 movieid(在本例中为 80)以便我可以将其从生成的推荐中排除

下面是代码

想知道怎么做
val moviesForUser = ratings.keyBy(_._2.user).lookup(206547)
val ratingsformovies =  moviesForUser.toMap.values.map(elem => 
(elem.product)).toSeq // answer I wanted is this line 
val candidates = 
sc.parallelize(movies.keys.filter(!ratingsformovies.contains(_)).toSeq)
val recommendations = bestModel.get
.predict(candidates.map((206547, _)))
.collect()
.sortBy(- _.rating)
.take(10)

var i = 1
println("Movies recommended for you:")
recommendations.foreach { r =>
println("%2d".format(i) + ": " + movies(r.product))
i += 1
}