Spark 2.0 ALS Recommendation how to recommend to a user

6.8k views Asked by At

I have followed the guide given in the link http://ampcamp.berkeley.edu/big-data-mini-course/movie-recommendation-with-mllib.html

But this is outdated as it uses spark Mlib RDD approach. The New Spark 2.0 has DataFrame approach. Now My problem is I have got the updated code

val ratings = spark.read.textFile("data/mllib/als/sample_movielens_ratings.txt")
  .map(parseRating)
  .toDF()
val Array(training, test) = ratings.randomSplit(Array(0.8, 0.2))

// Build the recommendation model using ALS on the training data
val als = new ALS()
  .setMaxIter(5)
  .setRegParam(0.01)
  .setUserCol("userId")
  .setItemCol("movieId")
  .setRatingCol("rating")
val model = als.fit(training)
// Evaluate the model by computing the RMSE on the test data
val predictions = model.transform(test)

Now Here is the problem, In the old code the model that was obtained was a MatrixFactorizationModel, Now it has its own model(ALSModel)

In MatrixFactorizationModel you could directly do

val recommendations = bestModel.get
  .predict(userID)

Which will give the list of products with highest probability of user liking them.

But Now there is no .predict method. Any Idea how to recommend a list of products given a user Id

3

There are 3 answers

2
T. Gawęda On BEST ANSWER

Use transform method on model:

import spark.implicits._
val dataFrameToPredict = sparkContext.parallelize(Seq((111, 222)))
    .toDF("userId", "productId")
val predictionsOfProducts = model.transform (dataFrameToPredict)

There's a jira ticket to implement recommend(User|Product) method, but it's not yet on default branch

Now you have DataFrame with score for user

You can simply use orderBy and limit to show N recommended products:

// where is for case when we have big DataFrame with many users
model.transform (dataFrameToPredict.where('userId === givenUserId))
    .select ('productId, 'prediction)
    .orderBy('prediction.desc)
    .limit(N)
    .map { case Row (productId: Int, prediction: Double) => (productId, prediction) }
    .collect()

DataFrame dataFrameToPredict can be some large user-product DataFrame, for example all users x all products

0
Joshua Cook On

The ALS Model in Spark contains the following helpful methods:

  • recommendForAllItems(int numUsers)

    Returns top numUsers users recommended for each item, for all items.

  • recommendForAllUsers(int numItems)

    Returns top numItems items recommended for each user, for all users.

  • recommendForItemSubset(Dataset<?> dataset, int numUsers)

    Returns top numUsers users recommended for each item id in the input data set.

  • recommendForUserSubset(Dataset<?> dataset, int numItems)

    Returns top numItems items recommended for each user id in the input data set.


e.g. Python

from pyspark.ml.recommendation import ALS
from pyspark.sql.functions import explode

alsEstimator = ALS()

(alsEstimator.setRank(1)
  .setUserCol("user_id")
  .setItemCol("product_id")
  .setRatingCol("rating")
  .setMaxIter(20)
  .setColdStartStrategy("drop"))

alsModel = alsEstimator.fit(productRatings)

recommendForSubsetDF = alsModel.recommendForUserSubset(TargetUsers, 40)

recommendationsDF = (recommendForSubsetDF
  .select("user_id", explode("recommendations")
  .alias("recommendation"))
  .select("user_id", "recommendation.*")
)

display(recommendationsDF)

e.g. Scala:

import org.apache.spark.ml.recommendation.ALS
import org.apache.spark.sql.functions.explode 

val alsEstimator = new ALS().setRank(1)
  .setUserCol("user_id")
  .setItemCol("product_id")
  .setRatingCol("rating")
  .setMaxIter(20)
  .setColdStartStrategy("drop")

val alsModel = alsEstimator.fit(productRatings)

val recommendForSubsetDF = alsModel.recommendForUserSubset(sampleTargetUsers, 40)

val recommendationsDF = recommendForSubsetDF
  .select($"user_id", explode($"recommendations").alias("recommendation"))
  .select($"user_id", $"recommendation.*")

display(recommendationsDF)
0
James Ward On

Here is what I did to get recommendations for a specific user with spark.ml:

import com.github.fommil.netlib.BLAS.{getInstance => blas}

userFactors.lookup(userId).headOption.fold(Map.empty[String, Float]) { user =>

  val ratings = itemFactors.map { case (id, features) =>
    val rating = blas.sdot(features.length, user, 1, features, 1)
    (id, rating)
  }

  ratings.sortBy(_._2).take(numResults).toMap
}

Both userFactors and itemFactors in my case are RDD[(String, Array[Float])] but you should be able to do something similar with DataFrames.