Skip to content
Closed
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
220d639
typo in ml's als implementation
BenFradet Dec 19, 2015
5513cbf
started the doc for ml's als
BenFradet Dec 19, 2015
8be8e5f
typo in mllib-collaborative-filtering doc
BenFradet Dec 19, 2015
ddecd67
made [scala|java|python] doc references more consistent in mllib-coll…
BenFradet Dec 19, 2015
1a8e9e2
cleanup of the examples
BenFradet Dec 19, 2015
9695e8a
ALS example in scala
BenFradet Dec 20, 2015
dc2eeed
added links to the collaborative filtering section
BenFradet Dec 20, 2015
813bd34
added a few comments
BenFradet Dec 20, 2015
2a46ffc
rmd case class
BenFradet Dec 20, 2015
d055681
rmd dep on file
BenFradet Dec 20, 2015
5d3f009
fix typing issue in the scala example
BenFradet Dec 20, 2015
fb1fff5
java example
BenFradet Dec 20, 2015
2aac2f8
python example
BenFradet Dec 20, 2015
bf0e8a8
explanation on implicit feedback
BenFradet Dec 21, 2015
dbde40e
updated scala example with the movie lens dataset
BenFradet Dec 21, 2015
05d74a1
renamed parseString in parseRating
BenFradet Dec 22, 2015
ac94d52
reworked the java example to use the movielens dataset
BenFradet Dec 22, 2015
c2aeeeb
reworked the python example to use the movielens dataset
BenFradet Dec 22, 2015
a127c37
updated to ml collaborative filtering guide due to the rework of the …
BenFradet Dec 22, 2015
a29d5f0
removed the original MovieLensALS example
BenFradet Dec 23, 2015
00b8055
removed the sample_movielens_movies dataset as it is not used anymore
BenFradet Dec 23, 2015
a086983
reworked the scala example by using a regression evaluator
BenFradet Dec 24, 2015
5fba2b6
reworked the java example by using a regression evaluator
BenFradet Dec 24, 2015
18bc9e7
reworked the python example by using a regression evaluator
BenFradet Dec 24, 2015
c6f9003
e.g. -> i.e.
BenFradet Feb 9, 2016
9021f36
fixed scalastyle
BenFradet Feb 10, 2016
5af5577
simplified the java example by removing the setters
BenFradet Feb 13, 2016
7e72c60
addressed @srowen 's comments regarding implicit feedback
BenFradet Feb 13, 2016
33135ab
rmd sentence fragment
BenFradet Feb 14, 2016
9b351e9
better explanation of the input data regarding implicit feedback
BenFradet Feb 15, 2016
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
100 changes: 0 additions & 100 deletions data/mllib/als/sample_movielens_movies.txt

This file was deleted.

2 changes: 2 additions & 0 deletions docs/_data/menu-ml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
url: ml-classification-regression.html
- text: Clustering
url: ml-clustering.html
- text: Collaborative filtering
url: ml-collaborative-filtering.html
- text: Advanced topics
url: ml-advanced.html
147 changes: 147 additions & 0 deletions docs/ml-collaborative-filtering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
---
layout: global
title: Collaborative Filtering - spark.ml
displayTitle: Collaborative Filtering - spark.ml
---

* Table of contents
{:toc}

## Collaborative filtering

[Collaborative filtering](http://en.wikipedia.org/wiki/Recommender_system#Collaborative_filtering)
is commonly used for recommender systems. These techniques aim to fill in the
missing entries of a user-item association matrix. `spark.ml` 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.ml` uses the [alternating least squares
(ALS)](http://dl.acm.org/citation.cfm?id=1608614)
algorithm to learn these latent factors. The implementation in `spark.ml` has the
following parameters:

* *numBlocks* is the number of blocks the users and items will be partitioned into in order to parallelize computation (defaults to 10).
* *rank* is the number of latent factors in the model (defaults to 10).
* *maxIter* is the maximum number of iterations to run (defaults to 10).
* *regParam* specifies the regularization parameter in ALS (defaults to 1.0).
* *implicitPrefs* specifies whether to use the *explicit feedback* ALS variant or one adapted for
*implicit feedback* data (defaults to `false` which means using *explicit feedback*).
* *alpha* is a parameter applicable to the implicit feedback variant of ALS that governs the
*baseline* confidence in preference observations (defaults to 1.0).
* *nonnegative* specifies whether or not to use nonnegative constraints for least squares (defaults to `false`).

### Explicit vs. implicit feedback

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.
Copy link
Member

Choose a reason for hiding this comment

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

Worth giving "ratings" as the canonical example of explicit feedback?

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 [Collaborative Filtering for Implicit Feedback Datasets](http://dx.doi.org/10.1109/ICDM.2008.22).
Essentially, instead of trying to model the matrix of ratings directly, this approach treats the data
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@srowen tried to take your remarks into account, I don't know if it's clearer now though.

as the number of observations of user actions. Those numbers are then related to the level of
confidence in observed user preferences, rather than explicit ratings given to items. The model
then tries to find latent factors that can be used to predict the expected preference of a user for
an item.

### Scaling of the regularization parameter

We scale the regularization parameter `regParam` 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
"[Large-Scale Parallel Collaborative Filtering for the Netflix Prize](http://dx.doi.org/10.1007/978-3-540-68880-8_32)".
It makes `regParam` 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.

## Examples

<div class="codetabs">
<div data-lang="scala" markdown="1">

In the following example, we load rating data from the
[MovieLens dataset](http://grouplens.org/datasets/movielens/), each row
Copy link
Member

Choose a reason for hiding this comment

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

Do people need to download this now? which file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, it's in the data folder, it's just to tell people where we got the dataset from.

consisting of a user, a movie, a rating and a timestamp.
We then train an ALS model which assumes, by default, that the ratings are
explicit (`implicitPrefs` is `false`).
We evaluate the recommendation model by measuring the root-mean-square error of
rating prediction.

Refer to the [`ALS` Scala docs](api/scala/index.html#org.apache.spark.ml.recommendation.ALS)
for more details on the API.

{% include_example scala/org/apache/spark/examples/ml/ALSExample.scala %}

If the rating matrix is derived from another source of information (i.e. it is
inferred from other signals), you can set `implicitPrefs` to `true` to get
better results:

{% highlight scala %}
val als = new ALS()
.setMaxIter(5)
.setRegParam(0.01)
.setImplicitPrefs(true)
.setUserCol("userId")
.setItemCol("movieId")
.setRatingCol("rating")
{% endhighlight %}

</div>

<div data-lang="java" markdown="1">

In the following example, we load rating data from the
[MovieLens dataset](http://grouplens.org/datasets/movielens/), each row
consisting of a user, a movie, a rating and a timestamp.
We then train an ALS model which assumes, by default, that the ratings are
explicit (`implicitPrefs` is `false`).
We evaluate the recommendation model by measuring the root-mean-square error of
rating prediction.

Refer to the [`ALS` Java docs](api/java/org/apache/spark/ml/recommendation/ALS.html)
for more details on the API.

{% include_example java/org/apache/spark/examples/ml/JavaALSExample.java %}

If the rating matrix is derived from another source of information (i.e. it is
inferred from other signals), you can set `implicitPrefs` to `true` to get
better results:

{% highlight java %}
ALS als = new ALS()
.setMaxIter(5)
.setRegParam(0.01)
.setImplicitPrefs(true)
.setUserCol("userId")
.setItemCol("movieId")
.setRatingCol("rating");
{% endhighlight %}

</div>

<div data-lang="python" markdown="1">

In the following example, we load rating data from the
[MovieLens dataset](http://grouplens.org/datasets/movielens/), each row
consisting of a user, a movie, a rating and a timestamp.
We then train an ALS model which assumes, by default, that the ratings are
explicit (`implicitPrefs` is `False`).
We evaluate the recommendation model by measuring the root-mean-square error of
rating prediction.

Refer to the [`ALS` Python docs](api/python/pyspark.ml.html#pyspark.ml.recommendation.ALS)
for more details on the API.

{% include_example python/ml/als_example.py %}

If the rating matrix is derived from another source of information (i.e. it is
inferred from other signals), you can set `implicitPrefs` to `True` to get
better results:

{% highlight python %}
als = ALS(maxIter=5, regParam=0.01, implicitPrefs=True,
userCol="userId", itemCol="movieId", ratingCol="rating")
{% endhighlight %}

</div>
</div>
27 changes: 13 additions & 14 deletions docs/mllib-collaborative-filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ following parameters:

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
[Collaborative Filtering for Implicit Feedback Datasets](http://dx.doi.org/10.1109/ICDM.2008.22).
Essentially instead of trying to model the matrix of ratings directly, this approach treats the data
as a combination of binary preferences and *confidence values*. The ratings are then related to the
level of confidence in observed user preferences, rather than explicit ratings given to items. The
model then tries to find latent factors that can be used to predict the expected preference of a
user for an item.
from [Collaborative Filtering for Implicit Feedback Datasets](http://dx.doi.org/10.1109/ICDM.2008.22).
Essentially, instead of trying to model the matrix of ratings directly, this approach treats the data
as the number of observations of user actions. Those numbers are then related to the level of
confidence in observed user preferences, rather than explicit ratings given to items. The model
then tries to find latent factors that can be used to predict the expected preference of a user for
an item.

### Scaling of the regularization parameter

Expand All @@ -50,9 +50,8 @@ 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
"[Large-Scale Parallel Collaborative Filtering for the Netflix Prize](http://dx.doi.org/10.1007/978-3-540-68880-8_32)".
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.
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.

## Examples

Expand All @@ -64,11 +63,11 @@ We use the default [ALS.train()](api/scala/index.html#org.apache.spark.mllib.rec
method which assumes ratings are explicit. We evaluate the
recommendation model by measuring the Mean Squared Error of rating prediction.

Refer to the [`ALS` Scala docs](api/scala/index.html#org.apache.spark.mllib.recommendation.ALS) for details on the API.
Refer to the [`ALS` Scala docs](api/scala/index.html#org.apache.spark.mllib.recommendation.ALS) 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 (e.g., it is inferred from
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 %}
Expand All @@ -85,7 +84,7 @@ Spark Java API uses a separate `JavaRDD` class. You can convert a Java RDD to a
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 [`ALS` Java docs](api/java/org/apache/spark/mllib/recommendation/ALS.html) for details on the API.
Refer to the [`ALS` Java docs](api/java/org/apache/spark/mllib/recommendation/ALS.html) for more details on the API.

{% include_example java/org/apache/spark/examples/mllib/JavaRecommendationExample.java %}
</div>
Expand All @@ -99,7 +98,7 @@ Refer to the [`ALS` Python docs](api/python/pyspark.mllib.html#pyspark.mllib.rec

{% include_example python/mllib/recommendation_example.py %}

If the rating matrix is derived from other source of information (i.e., it is inferred from other
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 %}
Expand Down
1 change: 1 addition & 0 deletions docs/mllib-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ We list major functionality from both below, with links to detailed guides.
* [Extracting, transforming and selecting features](ml-features.html)
* [Classification and regression](ml-classification-regression.html)
* [Clustering](ml-clustering.html)
* [Collaborative filtering](ml-collaborative-filtering.html)
* [Advanced topics](ml-advanced.html)

Some techniques are not available yet in spark.ml, most notably dimensionality reduction
Expand Down
Loading