Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,35 @@ class MatrixFactorizationModel(
predict(usersProducts.rdd.asInstanceOf[RDD[(Int, Int)]]).toJavaRDD()
}

/**
* Recommends similar products
*
* @param user the user to find similar users for
* @param num how many products to return. The number returned may be less than this.
* @return [[Rating]] objects, each of which contains the given user ID, a user ID, and a
* "score" in the rating field. Each represents one recommended user, and they are sorted
* by score, decreasing. The first returned is the one predicted to be most similar
* user to the specified user ID. The score is an opaque value that indicates how strongly
* recommended the user is.
*/
def recommendSimilariUsers(user: Int, num: Int): Array[Rating] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: Similari, also below.

recommend(userFeatures.lookup(user).head, userFeatures, num)
.map(t => Rating(user, t._1, t._2))

/**
* Recommends similar products
*
* @param product the product to find similar products for
* @param num how many products to return. The number returned may be less than this.
* @return [[Rating]] objects, each of which contains the given product ID, a product ID, and a
* "score" in the rating field. Each represents one recommended product, and they are sorted
* by score, decreasing. The first returned is the one predicted to be most similar
* product to the specified product ID. The score is an opaque value that indicates how strongly
* recommended the product is.
*/
def recommendSimilariProducts(product: Int, num: Int): Array[Rating] =
recommend(productFeatures.lookup(product).head, productFeatures, num)
.map(t => Rating(product, t._1, t._2))
/**
* Recommends products to a user.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ public void runRecommend() {
.run(data.rdd());
validateRecommendations(model.recommendProducts(1, 10), 10);
validateRecommendations(model.recommendUsers(1, 20), 20);
validateSimilarRecommendations(model.recommendSimilariProducts(1, 10), 10,1);
validateSimilarRecommendations(model.recommendSimilariUsers(1,10),10,1);
}

private static void validateSimilarRecommendations(Rating[] recommendations, int howMany, int id) {
Assert.assertEquals(howMany, recommendations.length);
for (int i = 1; i < recommendations.length; i++) {
Assert.assertFalse(recommendations[i].product() != id);
Assert.assertTrue(recommendations[i-1].rating() >= recommendations[i].rating());
}
Assert.assertTrue(recommendations[0].rating() > 0.7);
}

private static void validateRecommendations(Rating[] recommendations, int howMany) {
Expand Down