Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/3098#discussion_r27707373
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala
---
@@ -138,14 +141,122 @@ class MatrixFactorizationModel(
}
private def recommend(
- recommendToFeatures: Array[Double],
- recommendableFeatures: RDD[(Int, Array[Double])],
- num: Int): Array[(Int, Double)] = {
- val scored = recommendableFeatures.map { case (id,features) =>
- (id, blas.ddot(features.length, recommendToFeatures, 1, features, 1))
+ recommendToFeatures: Array[Double],
+ recommendableFeatures: RDD[(Int, Array[Double])],
+ num: Int): Array[(Int, Double)] = {
+ val recommendToVector = Vectors.dense(recommendToFeatures)
+ val scored = recommendableFeatures.map {
+ case (id, features) =>
+ (id, BLAS.dot(recommendToVector, Vectors.dense(features)))
}
scored.top(num)(Ordering.by(_._2))
}
+
+ /**
+ * Recommends topK products for all users
+ *
+ * @param num how many products to return for every user.
+ * @return [(Int, Array[Rating])] objects, where every tuple contains a
userID and an array of
+ * rating objects which contains the same userId, recommended productID
and a "score" in the
+ * rating field. Semantics of score is same as recommendProducts API
+ */
+ def recommendProductsForUsers(num: Int): RDD[(Int, Array[Rating])] = {
+ val topK = userFeatures.map { x => (x._1, num) }
--- End diff --
Is it common to use different `num`s? I think it should be sufficient to
set `num` globally.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]