Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
a61e434
update mllib's toc
mengxr Apr 13, 2014
137fd1d
re-organize toc
mengxr Apr 15, 2014
182460f
add guide for naive Bayes
mengxr Apr 15, 2014
f399f6c
move linear-algebra to dimensionality-reduction
mengxr Apr 15, 2014
f316ec2
add migration guide
mengxr Apr 15, 2014
53c9552
update dependencies
mengxr Apr 15, 2014
9fca001
add first version of linear algebra guide
mengxr Apr 16, 2014
64f8995
move decision tree guide to a separate file
mengxr Apr 16, 2014
72e4804
one pass over tree guide
mengxr Apr 16, 2014
d1b5cbf
merge master
mengxr Apr 16, 2014
8d1a128
move part of linear algebra to data types and add Java/Python examples
mengxr Apr 16, 2014
86e143a
remove data types section from main page
mengxr Apr 16, 2014
656d416
update mllib-clustering
mengxr Apr 16, 2014
5f0a700
update collaborative filtering
mengxr Apr 16, 2014
906ed0a
add a python example to naive bayes
mengxr Apr 16, 2014
1fc8271
update toc
mengxr Apr 16, 2014
0a40837
first pass over linear methods
mengxr Apr 16, 2014
31660eb
update linear algebra and stat
mengxr Apr 17, 2014
c82ffb4
clean optimization
mengxr Apr 17, 2014
f639674
add python section to migration guide
mengxr Apr 17, 2014
e605dd6
add LIBSVM loader
mengxr Apr 17, 2014
92747b3
make section titles consistent
mengxr Apr 17, 2014
cc604bf
remove classification and regression
mengxr Apr 17, 2014
5760045
claim beta
mengxr Apr 17, 2014
6ce6a6f
add summary statistics to toc
mengxr Apr 17, 2014
8aeaba1
wrap long lines
mengxr Apr 17, 2014
5c1e1b1
minor
mengxr Apr 17, 2014
162ee12
rename section names
mengxr Apr 17, 2014
6568d65
update toc, level up lr and svm
mengxr Apr 17, 2014
186ab07
update section names
mengxr Apr 17, 2014
8cd2441
minor updates
mengxr Apr 17, 2014
14e2287
add sample libsvm data and use it in guide
mengxr Apr 17, 2014
648283e
level down statistics
mengxr Apr 17, 2014
abbbf7e
update ALS argument names
mengxr Apr 17, 2014
053ad8a
add links to go back to the main page
mengxr Apr 17, 2014
7dc95cc
update linear methods
mengxr Apr 17, 2014
369a4d3
Merge branch 'master' into mllib-doc
mengxr Apr 18, 2014
28f40dc
remove localhost:4000
mengxr Apr 22, 2014
c17440d
fix python nb example
mengxr Apr 22, 2014
5bbff49
add imports to labeled point examples
mengxr Apr 22, 2014
928e630
initialization_mode -> initializationMode
mengxr Apr 22, 2014
9474065
add alpha to ALS examples
mengxr Apr 22, 2014
f9fda28
minor
mengxr Apr 22, 2014
944e3a9
merge master
mengxr Apr 22, 2014
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
476 changes: 476 additions & 0 deletions docs/mllib-basics.md

Large diffs are not rendered by default.

568 changes: 0 additions & 568 deletions docs/mllib-classification-regression.md

This file was deleted.

44 changes: 21 additions & 23 deletions docs/mllib-clustering.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
---
layout: global
title: MLlib - Clustering
title: <a href="mllib-guide.html">MLlib</a> - Clustering
---

* Table of contents
{:toc}


# Clustering
## Clustering

Clustering is an unsupervised learning problem whereby we aim to group subsets
of entities with one another based on some notion of similarity. Clustering is
often used for exploratory analysis and/or as a component of a hierarchical
supervised learning pipeline (in which distinct classifiers or regression
models are trained for each cluster). MLlib supports
models are trained for each cluster).

MLlib supports
[k-means](http://en.wikipedia.org/wiki/K-means_clustering) clustering, one of
the most commonly used clustering algorithms that clusters the data points into
predfined number of clusters. The MLlib implementation includes a parallelized
Expand All @@ -31,17 +33,14 @@ a given dataset, the algorithm returns the best clustering result).
* *initializiationSteps* determines the number of steps in the k-means\|\| algorithm.
* *epsilon* determines the distance threshold within which we consider k-means to have converged.

Available algorithms for clustering:

* [KMeans](api/scala/index.html#org.apache.spark.mllib.clustering.KMeans)



# Usage in Scala
## Examples

<div class="codetabs">
<div data-lang="scala" markdown="1">
Following code snippets can be executed in `spark-shell`.

In the following example after loading and parsing data, we use the KMeans object to cluster the data
In the following example after loading and parsing data, we use the
[`KMeans`](api/mllib/index.html#org.apache.spark.mllib.clustering.KMeans) object to cluster the data
into two clusters. The number of desired clusters is passed to the algorithm. We then compute Within
Set Sum of Squared Error (WSSSE). You can reduce this error measure by increasing *k*. In fact the
optimal *k* is usually one where there is an "elbow" in the WSSSE graph.
Expand All @@ -63,22 +62,22 @@ val clusters = KMeans.train(parsedData, numClusters, numIterations)
val WSSSE = clusters.computeCost(parsedData)
println("Within Set Sum of Squared Errors = " + WSSSE)
{% endhighlight %}
</div>


# Usage in Java

<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.
</div>

# Usage in Python
<div data-lang="python" markdown="1">
Following examples can be tested in the PySpark shell.

In the following example after loading and parsing data, we use the KMeans object to cluster the data
into two clusters. The number of desired clusters is passed to the algorithm. We then compute Within
Set Sum of Squared Error (WSSSE). You can reduce this error measure by increasing *k*. In fact the
optimal *k* is usually one where there is an "elbow" in the WSSSE graph.
In the following example after loading and parsing data, we use the KMeans object to cluster the
data into two clusters. The number of desired clusters is passed to the algorithm. We then compute
Within Set Sum of Squared Error (WSSSE). You can reduce this error measure by increasing *k*. In
fact the optimal *k* is usually one where there is an "elbow" in the WSSSE graph.

{% highlight python %}
from pyspark.mllib.clustering import KMeans
Expand All @@ -91,7 +90,7 @@ parsedData = data.map(lambda line: array([float(x) for x in line.split(' ')]))

# Build the model (cluster the data)
clusters = KMeans.train(parsedData, 2, maxIterations=10,
runs=10, initialization_mode="random")
runs=10, initializationMode="random")

# Evaluate clustering by computing Within Set Sum of Squared Errors
def error(point):
Expand All @@ -101,7 +100,6 @@ def error(point):
WSSSE = parsedData.map(lambda point: error(point)).reduce(lambda x, y: x + y)
print("Within Set Sum of Squared Error = " + str(WSSSE))
{% endhighlight %}
</div>

Similarly you can use RidgeRegressionWithSGD and LassoWithSGD and compare training Mean Squared
Errors.

</div>
78 changes: 44 additions & 34 deletions docs/mllib-collaborative-filtering.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
---
layout: global
title: MLlib - Collaborative Filtering
title: <a href="mllib-guide.html">MLlib</a> - Collaborative Filtering
---

* Table of contents
{:toc}

# Collaborative Filtering
## 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. 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.
In particular, we implement the [alternating least squares
(ALS)](http://www2.research.att.com/~volinsky/papers/ieeecomputer.pdf)
(ALS)](http://dl.acm.org/citation.cfm?id=1608614)
algorithm to learn these latent factors. The implementation in MLlib has the
following parameters:

* *numBlocks* is the number of blacks used to parallelize computation (set to -1 to auto-configure).
* *numBlocks* is the number of blocks used to parallelize computation (set to -1 to auto-configure).
* *rank* is the number of latent factors in our model.
* *iterations* is the number of iterations to run.
* *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
* *implicitPrefs* specifies whether to use the *explicit feedback* ALS variant or one adapted for
Copy link
Contributor

Choose a reason for hiding this comment

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

These last two points lack periods, whereas every other point has a period.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

*implicit feedback* data.
* *alpha* is a parameter applicable to the implicit feedback variant of ALS that governs the
*baseline* confidence in preference observations.

## Explicit vs Implicit Feedback
### 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.

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 MLlib to deal with
such data is taken from
[Collaborative Filtering for Implicit Feedback Datasets](http://www2.research.att.com/~yifanhu/PUB/cf.pdf).
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.
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 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.

Available algorithms for collaborative filtering:
## Examples

* [ALS](api/scala/index.html#org.apache.spark.mllib.recommendation.ALS)


# Usage in Scala

Following code snippets can be executed in `spark-shell`.
<div class="codetabs">

<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 ALS.train() method which assumes ratings are explicit. We evaluate the recommendation
model by measuring the Mean Squared Error of rating prediction.
We use the default [ALS.train()](api/mllib/index.html#org.apache.spark.mllib.recommendation.ALS$)
method which assumes ratings are explicit. We evaluate the
recommendation model by measuring the Mean Squared Error of rating prediction.

{% highlight scala %}
import org.apache.spark.mllib.recommendation.ALS
Expand All @@ -64,8 +63,9 @@ val ratings = data.map(_.split(',') match {
})

// Build the recommendation model using ALS
val rank = 10
val numIterations = 20
val model = ALS.train(ratings, 1, 20, 0.01)
val model = ALS.train(ratings, rank, numIterations, 0.01)

// Evaluate the model on rating data
val usersProducts = ratings.map{ case Rating(user, product, rate) => (user, product)}
Expand All @@ -85,19 +85,19 @@ If the rating matrix is derived from other source of information (i.e., it is in
other signals), you can use the trainImplicit method to get better results.

{% highlight scala %}
val model = ALS.trainImplicit(ratings, 1, 20, 0.01)
val alpha = 0.01
val model = ALS.trainImplicit(ratings, rank, numIterations, alpha)
{% endhighlight %}
</div>

# Usage in Java

<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.
</div>

# Usage in Python
Following examples can be tested in the PySpark shell.

<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.
Expand All @@ -111,7 +111,9 @@ data = sc.textFile("mllib/data/als/test.data")
ratings = data.map(lambda line: array([float(x) for x in line.split(',')]))

# Build the recommendation model using Alternating Least Squares
model = ALS.train(ratings, 1, 20)
rank = 10
numIterations = 20
model = ALS.train(ratings, rank, numIterations)

# Evaluate the model on training data
testdata = ratings.map(lambda p: (int(p[0]), int(p[1])))
Expand All @@ -126,5 +128,13 @@ signals), you can use the trainImplicit method to get better results.

{% highlight python %}
# Build the recommendation model using Alternating Least Squares based on implicit ratings
model = ALS.trainImplicit(ratings, 1, 20)
model = ALS.trainImplicit(ratings, rank, numIterations, alpha = 0.01)
{% endhighlight %}
</div>

</div>

## Tutorial

[AMP Camp](http://ampcamp.berkeley.edu/) provides a hands-on tutorial for
[personalized movie recommendation with MLlib](http://ampcamp.berkeley.edu/big-data-mini-course/movie-recommendation-with-mllib.html).
Loading