1---
2layout: global
3title: Collaborative Filtering
4displayTitle: Collaborative Filtering
5---
6
7* Table of contents
8{:toc}
9
10## Collaborative filtering
11
12[Collaborative filtering](http://en.wikipedia.org/wiki/Recommender_system#Collaborative_filtering)
13is commonly used for recommender systems.  These techniques aim to fill in the
14missing entries of a user-item association matrix.  `spark.ml` currently supports
15model-based collaborative filtering, in which users and products are described
16by a small set of latent factors that can be used to predict missing entries.
17`spark.ml` uses the [alternating least squares
18(ALS)](http://dl.acm.org/citation.cfm?id=1608614)
19algorithm to learn these latent factors. The implementation in `spark.ml` has the
20following parameters:
21
22* *numBlocks* is the number of blocks the users and items will be partitioned into in order to parallelize computation (defaults to 10).
23* *rank* is the number of latent factors in the model (defaults to 10).
24* *maxIter* is the maximum number of iterations to run (defaults to 10).
25* *regParam* specifies the regularization parameter in ALS (defaults to 1.0).
26* *implicitPrefs* specifies whether to use the *explicit feedback* ALS variant or one adapted for
27  *implicit feedback* data (defaults to `false` which means using *explicit feedback*).
28* *alpha* is a parameter applicable to the implicit feedback variant of ALS that governs the
29  *baseline* confidence in preference observations (defaults to 1.0).
30* *nonnegative* specifies whether or not to use nonnegative constraints for least squares (defaults to `false`).
31
32**Note:** The DataFrame-based API for ALS currently only supports integers for user and item ids.
33Other numeric types are supported for the user and item id columns,
34but the ids must be within the integer value range.
35
36### Explicit vs. implicit feedback
37
38The standard approach to matrix factorization based collaborative filtering treats
39the entries in the user-item matrix as *explicit* preferences given by the user to the item,
40for example, users giving ratings to movies.
41
42It is common in many real-world use cases to only have access to *implicit feedback* (e.g. views,
43clicks, purchases, likes, shares etc.). The approach used in `spark.ml` to deal with such data is taken
44from [Collaborative Filtering for Implicit Feedback Datasets](http://dx.doi.org/10.1109/ICDM.2008.22).
45Essentially, instead of trying to model the matrix of ratings directly, this approach treats the data
46as numbers representing the *strength* in observations of user actions (such as the number of clicks,
47or the cumulative duration someone spent viewing a movie). Those numbers are then related to the level of
48confidence in observed user preferences, rather than explicit ratings given to items. The model
49then tries to find latent factors that can be used to predict the expected preference of a user for
50an item.
51
52### Scaling of the regularization parameter
53
54We scale the regularization parameter `regParam` in solving each least squares problem by
55the number of ratings the user generated in updating user factors,
56or the number of ratings the product received in updating product factors.
57This approach is named "ALS-WR" and discussed in the paper
58"[Large-Scale Parallel Collaborative Filtering for the Netflix Prize](http://dx.doi.org/10.1007/978-3-540-68880-8_32)".
59It makes `regParam` less dependent on the scale of the dataset, so we can apply the
60best parameter learned from a sampled subset to the full dataset and expect similar performance.
61
62## Examples
63
64<div class="codetabs">
65<div data-lang="scala" markdown="1">
66
67In the following example, we load ratings data from the
68[MovieLens dataset](http://grouplens.org/datasets/movielens/), each row
69consisting of a user, a movie, a rating and a timestamp.
70We then train an ALS model which assumes, by default, that the ratings are
71explicit (`implicitPrefs` is `false`).
72We evaluate the recommendation model by measuring the root-mean-square error of
73rating prediction.
74
75Refer to the [`ALS` Scala docs](api/scala/index.html#org.apache.spark.ml.recommendation.ALS)
76for more details on the API.
77
78{% include_example scala/org/apache/spark/examples/ml/ALSExample.scala %}
79
80If the rating matrix is derived from another source of information (i.e. it is
81inferred from other signals), you can set `implicitPrefs` to `true` to get
82better results:
83
84{% highlight scala %}
85val als = new ALS()
86  .setMaxIter(5)
87  .setRegParam(0.01)
88  .setImplicitPrefs(true)
89  .setUserCol("userId")
90  .setItemCol("movieId")
91  .setRatingCol("rating")
92{% endhighlight %}
93
94</div>
95
96<div data-lang="java" markdown="1">
97
98In the following example, we load ratings data from the
99[MovieLens dataset](http://grouplens.org/datasets/movielens/), each row
100consisting of a user, a movie, a rating and a timestamp.
101We then train an ALS model which assumes, by default, that the ratings are
102explicit (`implicitPrefs` is `false`).
103We evaluate the recommendation model by measuring the root-mean-square error of
104rating prediction.
105
106Refer to the [`ALS` Java docs](api/java/org/apache/spark/ml/recommendation/ALS.html)
107for more details on the API.
108
109{% include_example java/org/apache/spark/examples/ml/JavaALSExample.java %}
110
111If the rating matrix is derived from another source of information (i.e. it is
112inferred from other signals), you can set `implicitPrefs` to `true` to get
113better results:
114
115{% highlight java %}
116ALS als = new ALS()
117  .setMaxIter(5)
118  .setRegParam(0.01)
119  .setImplicitPrefs(true)
120  .setUserCol("userId")
121  .setItemCol("movieId")
122  .setRatingCol("rating");
123{% endhighlight %}
124
125</div>
126
127<div data-lang="python" markdown="1">
128
129In the following example, we load ratings data from the
130[MovieLens dataset](http://grouplens.org/datasets/movielens/), each row
131consisting of a user, a movie, a rating and a timestamp.
132We then train an ALS model which assumes, by default, that the ratings are
133explicit (`implicitPrefs` is `False`).
134We evaluate the recommendation model by measuring the root-mean-square error of
135rating prediction.
136
137Refer to the [`ALS` Python docs](api/python/pyspark.ml.html#pyspark.ml.recommendation.ALS)
138for more details on the API.
139
140{% include_example python/ml/als_example.py %}
141
142If the rating matrix is derived from another source of information (i.e. it is
143inferred from other signals), you can set `implicitPrefs` to `True` to get
144better results:
145
146{% highlight python %}
147als = ALS(maxIter=5, regParam=0.01, implicitPrefs=True,
148          userCol="userId", itemCol="movieId", ratingCol="rating")
149{% endhighlight %}
150
151</div>
152
153<div data-lang="r" markdown="1">
154
155Refer to the [R API docs](api/R/spark.als.html) for more details.
156
157{% include_example r/ml/als.R %}
158</div>
159
160</div>
161