-
Joseph K. Bradley authored
## What changes were proposed in this pull request? Made DataFrame-based API primary * Spark doc menu bar and other places now link to ml-guide.html, not mllib-guide.html * mllib-guide.html keeps RDD-specific list of features, with a link at the top redirecting people to ml-guide.html * ml-guide.html includes a "maintenance mode" announcement about the RDD-based API * **Reviewers: please check this carefully** * (minor) Titles for DF API no longer include "- spark.ml" suffix. Titles for RDD API have "- RDD-based API" suffix * Moved migration guide to ml-guide from mllib-guide * Also moved past guides from mllib-migration-guides to ml-migration-guides, with a redirect link on mllib-migration-guides * **Reviewers**: I did not change any of the content of the migration guides. Reorganized DataFrame-based guide: * ml-guide.html mimics the old mllib-guide.html page in terms of content: overview, migration guide, etc. * Moved Pipeline description into ml-pipeline.html and moved tuning into ml-tuning.html * **Reviewers**: I did not change the content of these guides, except some intro text. * Sidebar remains the same, but with pipeline and tuning sections added Other: * ml-classification-regression.html: Moved text about linear methods to new section in page ## How was this patch tested? Generated docs locally Author: Joseph K. Bradley <joseph@databricks.com> Closes #14213 from jkbradley/ml-guide-2.0.
Joseph K. Bradley authored## What changes were proposed in this pull request? Made DataFrame-based API primary * Spark doc menu bar and other places now link to ml-guide.html, not mllib-guide.html * mllib-guide.html keeps RDD-specific list of features, with a link at the top redirecting people to ml-guide.html * ml-guide.html includes a "maintenance mode" announcement about the RDD-based API * **Reviewers: please check this carefully** * (minor) Titles for DF API no longer include "- spark.ml" suffix. Titles for RDD API have "- RDD-based API" suffix * Moved migration guide to ml-guide from mllib-guide * Also moved past guides from mllib-migration-guides to ml-migration-guides, with a redirect link on mllib-migration-guides * **Reviewers**: I did not change any of the content of the migration guides. Reorganized DataFrame-based guide: * ml-guide.html mimics the old mllib-guide.html page in terms of content: overview, migration guide, etc. * Moved Pipeline description into ml-pipeline.html and moved tuning into ml-tuning.html * **Reviewers**: I did not change the content of these guides, except some intro text. * Sidebar remains the same, but with pipeline and tuning sections added Other: * ml-classification-regression.html: Moved text about linear methods to new section in page ## How was this patch tested? Generated docs locally Author: Joseph K. Bradley <joseph@databricks.com> Closes #14213 from jkbradley/ml-guide-2.0.
layout: global
title: Data Types - RDD-based API
displayTitle: Data Types - RDD-based API
- Table of contents {:toc}
MLlib supports local vectors and matrices stored on a single machine, as well as distributed matrices backed by one or more RDDs. Local vectors and local matrices are simple data models that serve as public interfaces. The underlying linear algebra operations are provided by Breeze. A training example used in supervised learning is called a "labeled point" in MLlib.
Local vector
A local vector has integer-typed and 0-based indices and double-typed values, stored on a single
machine. MLlib supports two types of local vectors: dense and sparse. A dense vector is backed by
a double array representing its entry values, while a sparse vector is backed by two parallel
arrays: indices and values. For example, a vector (1.0, 0.0, 3.0)
can be represented in dense
format as [1.0, 0.0, 3.0]
or in sparse format as (3, [0, 2], [1.0, 3.0])
, where 3
is the size
of the vector.
The base class of local vectors is
Vector
, and we provide two
implementations: DenseVector
and
SparseVector
. We recommend
using the factory methods implemented in
Vectors
to create local vectors.
Refer to the Vector
Scala docs and Vectors
Scala docs for details on the API.
{% highlight scala %} import org.apache.spark.mllib.linalg.{Vector, Vectors}
// Create a dense vector (1.0, 0.0, 3.0). val dv: Vector = Vectors.dense(1.0, 0.0, 3.0) // Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries. val sv1: Vector = Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0)) // Create a sparse vector (1.0, 0.0, 3.0) by specifying its nonzero entries. val sv2: Vector = Vectors.sparse(3, Seq((0, 1.0), (2, 3.0))) {% endhighlight %}
Note:
Scala imports scala.collection.immutable.Vector
by default, so you have to import
org.apache.spark.mllib.linalg.Vector
explicitly to use MLlib's Vector
.
The base class of local vectors is
Vector
, and we provide two
implementations: DenseVector
and
SparseVector
. We recommend
using the factory methods implemented in
Vectors
to create local vectors.
Refer to the Vector
Java docs and Vectors
Java docs for details on the API.
{% highlight java %} import org.apache.spark.mllib.linalg.Vector; import org.apache.spark.mllib.linalg.Vectors;
// Create a dense vector (1.0, 0.0, 3.0). Vector dv = Vectors.dense(1.0, 0.0, 3.0); // Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries. Vector sv = Vectors.sparse(3, new int[] {0, 2}, new double[] {1.0, 3.0}); {% endhighlight %}
- NumPy's
array
- Python's list, e.g.,
[1, 2, 3]
and the following as sparse vectors:
- MLlib's
SparseVector
. - SciPy's
csc_matrix
with a single column
We recommend using NumPy arrays over lists for efficiency, and using the factory methods implemented
in Vectors
to create sparse vectors.
Refer to the Vectors
Python docs for more details on the API.
{% highlight python %} import numpy as np import scipy.sparse as sps from pyspark.mllib.linalg import Vectors
Use a NumPy array as a dense vector.
dv1 = np.array([1.0, 0.0, 3.0])
Use a Python list as a dense vector.
dv2 = [1.0, 0.0, 3.0]
Create a SparseVector.
sv1 = Vectors.sparse(3, [0, 2], [1.0, 3.0])
Use a single-column SciPy csc_matrix as a sparse vector.
sv2 = sps.csc_matrix((np.array([1.0, 3.0]), np.array([0, 2]), np.array([0, 2])), shape = (3, 1)) {% endhighlight %}
Labeled point
A labeled point is a local vector, either dense or sparse, associated with a label/response.
In MLlib, labeled points are used in supervised learning algorithms.
We use a double to store a label, so we can use labeled points in both regression and classification.
For binary classification, a label should be either 0
(negative) or 1
(positive).
For multiclass classification, labels should be class indices starting from zero: 0, 1, 2, ...
.
A labeled point is represented by the case class
LabeledPoint
.
Refer to the LabeledPoint
Scala docs for details on the API.
{% highlight scala %} import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint
// Create a labeled point with a positive label and a dense feature vector. val pos = LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0))
// Create a labeled point with a negative label and a sparse feature vector. val neg = LabeledPoint(0.0, Vectors.sparse(3, Array(0, 2), Array(1.0, 3.0))) {% endhighlight %}
A labeled point is represented by
LabeledPoint
.
Refer to the LabeledPoint
Java docs for details on the API.
{% highlight java %} import org.apache.spark.mllib.linalg.Vectors; import org.apache.spark.mllib.regression.LabeledPoint;
// Create a labeled point with a positive label and a dense feature vector. LabeledPoint pos = new LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0));
// Create a labeled point with a negative label and a sparse feature vector. LabeledPoint neg = new LabeledPoint(0.0, Vectors.sparse(3, new int[] {0, 2}, new double[] {1.0, 3.0})); {% endhighlight %}
A labeled point is represented by
LabeledPoint
.
Refer to the LabeledPoint
Python docs for more details on the API.
{% highlight python %} from pyspark.mllib.linalg import SparseVector from pyspark.mllib.regression import LabeledPoint
Create a labeled point with a positive label and a dense feature vector.
pos = LabeledPoint(1.0, [1.0, 0.0, 3.0])
Create a labeled point with a negative label and a sparse feature vector.
neg = LabeledPoint(0.0, SparseVector(3, [0, 2], [1.0, 3.0])) {% endhighlight %}
Sparse data
It is very common in practice to have sparse training data. MLlib supports reading training
examples stored in LIBSVM
format, which is the default format used by
LIBSVM
and
LIBLINEAR
. It is a text format in which each line
represents a labeled sparse feature vector using the following format:
label index1:value1 index2:value2 ...
where the indices are one-based and in ascending order. After loading, the feature indices are converted to zero-based.
MLUtils.loadLibSVMFile
reads training
examples stored in LIBSVM format.
Refer to the MLUtils
Scala docs for details on the API.
{% highlight scala %} import org.apache.spark.mllib.regression.LabeledPoint import org.apache.spark.mllib.util.MLUtils import org.apache.spark.rdd.RDD
val examples: RDD[LabeledPoint] = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt") {% endhighlight %}
MLUtils.loadLibSVMFile
reads training
examples stored in LIBSVM format.
Refer to the MLUtils
Java docs for details on the API.
{% highlight java %} import org.apache.spark.mllib.regression.LabeledPoint; import org.apache.spark.mllib.util.MLUtils; import org.apache.spark.api.java.JavaRDD;
JavaRDD examples = MLUtils.loadLibSVMFile(jsc.sc(), "data/mllib/sample_libsvm_data.txt").toJavaRDD(); {% endhighlight %}
MLUtils.loadLibSVMFile
reads training
examples stored in LIBSVM format.
Refer to the MLUtils
Python docs for more details on the API.
{% highlight python %} from pyspark.mllib.util import MLUtils
examples = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt") {% endhighlight %}
Local matrix
A local matrix has integer-typed row and column indices and double-typed values, stored on a single
machine. MLlib supports dense matrices, whose entry values are stored in a single double array in
column-major order, and sparse matrices, whose non-zero entry values are stored in the Compressed Sparse
Column (CSC) format in column-major order. For example, the following dense matrix \[ \begin{pmatrix} 1.0 & 2.0 \\ 3.0 & 4.0 \\ 5.0 & 6.0 \end{pmatrix} \]
is stored in a one-dimensional array [1.0, 3.0, 5.0, 2.0, 4.0, 6.0]
with the matrix size (3, 2)
.
The base class of local matrices is
Matrix
, and we provide two
implementations: DenseMatrix
,
and SparseMatrix
.
We recommend using the factory methods implemented
in Matrices
to create local
matrices. Remember, local matrices in MLlib are stored in column-major order.
Refer to the Matrix
Scala docs and Matrices
Scala docs for details on the API.
{% highlight scala %} import org.apache.spark.mllib.linalg.{Matrix, Matrices}
// Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0)) val dm: Matrix = Matrices.dense(3, 2, Array(1.0, 3.0, 5.0, 2.0, 4.0, 6.0))
// Create a sparse matrix ((9.0, 0.0), (0.0, 8.0), (0.0, 6.0)) val sm: Matrix = Matrices.sparse(3, 2, Array(0, 1, 3), Array(0, 2, 1), Array(9, 6, 8)) {% endhighlight %}
The base class of local matrices is
Matrix
, and we provide two
implementations: DenseMatrix
,
and SparseMatrix
.
We recommend using the factory methods implemented
in Matrices
to create local
matrices. Remember, local matrices in MLlib are stored in column-major order.
Refer to the Matrix
Java docs and Matrices
Java docs for details on the API.
{% highlight java %} import org.apache.spark.mllib.linalg.Matrix; import org.apache.spark.mllib.linalg.Matrices;
// Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0)) Matrix dm = Matrices.dense(3, 2, new double[] {1.0, 3.0, 5.0, 2.0, 4.0, 6.0});
// Create a sparse matrix ((9.0, 0.0), (0.0, 8.0), (0.0, 6.0)) Matrix sm = Matrices.sparse(3, 2, new int[] {0, 1, 3}, new int[] {0, 2, 1}, new double[] {9, 6, 8}); {% endhighlight %}
The base class of local matrices is
Matrix
, and we provide two
implementations: DenseMatrix
,
and SparseMatrix
.
We recommend using the factory methods implemented
in Matrices
to create local
matrices. Remember, local matrices in MLlib are stored in column-major order.
Refer to the Matrix
Python docs and Matrices
Python docs for more details on the API.
{% highlight python %} from pyspark.mllib.linalg import Matrix, Matrices
Create a dense matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0))
dm2 = Matrices.dense(3, 2, [1, 2, 3, 4, 5, 6])
Create a sparse matrix ((9.0, 0.0), (0.0, 8.0), (0.0, 6.0))
sm = Matrices.sparse(3, 2, [0, 1, 3], [0, 2, 1], [9, 6, 8]) {% endhighlight %}
Distributed matrix
A distributed matrix has long-typed row and column indices and double-typed values, stored distributively in one or more RDDs. It is very important to choose the right format to store large and distributed matrices. Converting a distributed matrix to a different format may require a global shuffle, which is quite expensive. Four types of distributed matrices have been implemented so far.
The basic type is called RowMatrix
. A RowMatrix
is a row-oriented distributed
matrix without meaningful row indices, e.g., a collection of feature vectors.
It is backed by an RDD of its rows, where each row is a local vector.
We assume that the number of columns is not huge for a RowMatrix
so that a single
local vector can be reasonably communicated to the driver and can also be stored /
operated on using a single node.
An IndexedRowMatrix
is similar to a RowMatrix
but with row indices,
which can be used for identifying rows and executing joins.
A CoordinateMatrix
is a distributed matrix stored in coordinate list (COO) format,
backed by an RDD of its entries.
A BlockMatrix
is a distributed matrix backed by an RDD of MatrixBlock
which is a tuple of (Int, Int, Matrix)
.