Project: Apache Spark (App Definition and Development / Streaming & Messaging)
File: mllib-collaborative-filtering.md

layout: global title: Collaborative Filtering - RDD-based API displayTitle: Collaborative Filtering - RDD-based API license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Table of contents {:toc} is commonly used for recommender systems. These techniques aim to fill in the missing entries of a user-item association matrix. `spark.mllib` currently supports model-based collaborative filtering, in which users and products are described by a small set of latent factors that can be used to predict missing entries. `spark.mllib` uses the [alternating least squares (ALS)](http://dl.acm.org/citation.cfm?id=1608614) algorithm to learn these latent factors. The implementation in `spark.mllib` has the following parameters: numBlocks* is the number of blocks used to parallelize computation (set to -1 to auto-configure). rank* is the number of features to use (also referred to as the number of latent factors). iterations* is the number of iterations of ALS to run. ALS typically converges to a reasonable solution in 20 iterations or less. lambda* specifies the regularization parameter in ALS. implicitPrefs specifies whether to use the explicit feedback* ALS variant or one adapted for implicit feedback data. alpha* is a parameter applicable to the implicit feedback variant of ALS that governs the baseline confidence in preference observations. The standard approach to matrix factorization-based collaborative filtering treats the entries in the user-item matrix as explicit preferences given by the user to the item, for example, users giving ratings to movies. It is common in many real-world use cases to only have access to implicit feedback (e.g. views, clicks, purchases, likes, shares etc.). The approach used in `spark.mllib` to deal with such data is taken from . Essentially, instead of trying to model the matrix of ratings directly, this approach treats the data as numbers representing the strength in observations of user actions (such as the number of clicks, or the cumulative duration someone spent viewing a movie). Those numbers are then related to the level of confidence in observed user preferences, rather than explicit ratings given to

The model then tries to find latent factors that can be used to predict the expected preference of a user for an item. Since v1.1, we scale the regularization parameter `lambda` in solving each least squares problem by the number of ratings the user generated in updating user factors, or the number of ratings the product received in updating product factors. This approach is named "ALS-WR" and discussed in the paper "". It makes `lambda` less dependent on the scale of the dataset, so we can apply the best parameter learned from a sampled subset to the full dataset and expect similar performance. <div class="codetabs"> <div data-lang="python" markdown="1"> In the following example we load rating data. Each row consists of a user, a product and a rating. We use the default ALS.train() method which assumes ratings are explicit. We evaluate the recommendation by measuring the Mean Squared Error of rating prediction. Refer to the for more details on the API. {% includeexample python/mllib/recommendationexample.py %} If the rating matrix is derived from other source of information (i.e. it is inferred from other signals), you can use the trainImplicit method to get better results. {% highlight python %} model = ALS.trainImplicit(ratings, rank, numIterations, alpha=0.01) {% endhighlight %} </div> <div data-lang="scala" markdown="1"> In the following example, we load rating data. Each row consists of a user, a product and a rating. We use the default method which assumes ratings are explicit. We evaluate the recommendation model by measuring the Mean Squared Error of rating prediction. Refer to the for more details on the API. {% include_example scala/org/apache/spark/examples/mllib/RecommendationExample.scala %} If the rating matrix is derived from another source of information (i.e. it is inferred from other signals), you can use the `trainImplicit` method to get better results. {% highlight scala %} val alpha = 0.01 val lambda = 0.01 val model = ALS.trainImplicit(ratings, rank, numIterations, lambda, alpha) {% endhighlight %} </div> <div data-lang="java" markdown="1"> All of MLlib's methods use Java-friendly types, so you can import and call them there the same way you do in Scala. The only caveat is that the methods take Scala RDD objects, while the Spark Java API uses a separate `JavaRDD` class. You can convert a Java RDD to a Scala one by calling `.rdd()` on your `JavaRDD` object. A self-contained application example that is equivalent to the provided example in Scala is given below: Refer to the for more details on the API. {% include_example java/org/apache/spark/examples/mllib/JavaRecommendationExample.java %} </div> </div> In order to run the above application, follow the instructions provided in the section of the Spark Quick Start guide. Be sure to also include spark-mllib to your build file as a dependency. The from the Spark Summit 2014 include a hands-on tutorial for .