Data Types - MLlib
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.
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.
MLlib recognizes the following types as dense vectors:
- 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.
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.
A labeled point is represented by
LabeledPoint
.
Refer to the LabeledPoint
Java docs for details on the API.
A labeled point is represented by
LabeledPoint
.
Refer to the LabeledPoint
Python docs for more details on the API.
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.
MLUtils.loadLibSVMFile
reads training
examples stored in LIBSVM format.
Refer to the MLUtils
Java docs for details on the API.
MLUtils.loadLibSVMFile
reads training
examples stored in LIBSVM format.
Refer to the MLUtils
Python docs for more details on the API.
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.
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.
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.
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. Three 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.
Note
The underlying RDDs of a distributed matrix must be deterministic, because we cache the matrix size. In general the use of non-deterministic RDDs can lead to errors.
RowMatrix
A RowMatrix
is a row-oriented distributed matrix without meaningful row indices, backed by an RDD
of its rows, where each row is a local vector.
Since each row is represented by a local vector, the number of columns is
limited by the integer range but it should be much smaller in practice.
A RowMatrix
can be
created from an RDD[Vector]
instance. Then we can compute its column summary statistics and decompositions.
QR decomposition is of the form A = QR where Q is an orthogonal matrix and R is an upper triangular matrix.
For singular value decomposition (SVD) and principal component analysis (PCA), please refer to Dimensionality reduction.
Refer to the RowMatrix
Scala docs for details on the API.
A RowMatrix
can be
created from a JavaRDD<Vector>
instance. Then we can compute its column summary statistics.
Refer to the RowMatrix
Java docs for details on the API.
A RowMatrix
can be
created from an RDD
of vectors.
Refer to the RowMatrix
Python docs for more details on the API.
IndexedRowMatrix
An IndexedRowMatrix
is similar to a RowMatrix
but with meaningful row indices. It is backed by
an RDD of indexed rows, so that each row is represented by its index (long-typed) and a local
vector.
An
IndexedRowMatrix
can be created from an RDD[IndexedRow]
instance, where
IndexedRow
is a
wrapper over (Long, Vector)
. An IndexedRowMatrix
can be converted to a RowMatrix
by dropping
its row indices.
Refer to the IndexedRowMatrix
Scala docs for details on the API.
An
IndexedRowMatrix
can be created from an JavaRDD<IndexedRow>
instance, where
IndexedRow
is a
wrapper over (long, Vector)
. An IndexedRowMatrix
can be converted to a RowMatrix
by dropping
its row indices.
Refer to the IndexedRowMatrix
Java docs for details on the API.
An IndexedRowMatrix
can be created from an RDD
of IndexedRow
s, where
IndexedRow
is a
wrapper over (long, vector)
. An IndexedRowMatrix
can be converted to a RowMatrix
by dropping
its row indices.
Refer to the IndexedRowMatrix
Python docs for more details on the API.
CoordinateMatrix
A CoordinateMatrix
is a distributed matrix backed by an RDD of its entries. Each entry is a tuple
of (i: Long, j: Long, value: Double)
, where i
is the row index, j
is the column index, and
value
is the entry value. A CoordinateMatrix
should be used only when both
dimensions of the matrix are huge and the matrix is very sparse.
A
CoordinateMatrix
can be created from an RDD[MatrixEntry]
instance, where
MatrixEntry
is a
wrapper over (Long, Long, Double)
. A CoordinateMatrix
can be converted to an IndexedRowMatrix
with sparse rows by calling toIndexedRowMatrix
. Other computations for
CoordinateMatrix
are not currently supported.
Refer to the CoordinateMatrix
Scala docs for details on the API.
A
CoordinateMatrix
can be created from a JavaRDD<MatrixEntry>
instance, where
MatrixEntry
is a
wrapper over (long, long, double)
. A CoordinateMatrix
can be converted to an IndexedRowMatrix
with sparse rows by calling toIndexedRowMatrix
. Other computations for
CoordinateMatrix
are not currently supported.
Refer to the CoordinateMatrix
Java docs for details on the API.
A CoordinateMatrix
can be created from an RDD
of MatrixEntry
entries, where
MatrixEntry
is a
wrapper over (long, long, float)
. A CoordinateMatrix
can be converted to a RowMatrix
by
calling toRowMatrix
, or to an IndexedRowMatrix
with sparse rows by calling toIndexedRowMatrix
.
Refer to the CoordinateMatrix
Python docs for more details on the API.
BlockMatrix
A BlockMatrix
is a distributed matrix backed by an RDD of MatrixBlock
s, where a MatrixBlock
is
a tuple of ((Int, Int), Matrix)
, where the (Int, Int)
is the index of the block, and Matrix
is
the sub-matrix at the given index with size rowsPerBlock
x colsPerBlock
.
BlockMatrix
supports methods such as add
and multiply
with another BlockMatrix
.
BlockMatrix
also has a helper function validate
which can be used to check whether the
BlockMatrix
is set up properly.
A BlockMatrix
can be
most easily created from an IndexedRowMatrix
or CoordinateMatrix
by calling toBlockMatrix
.
toBlockMatrix
creates blocks of size 1024 x 1024 by default.
Users may change the block size by supplying the values through toBlockMatrix(rowsPerBlock, colsPerBlock)
.
Refer to the BlockMatrix
Scala docs for details on the API.
A BlockMatrix
can be
most easily created from an IndexedRowMatrix
or CoordinateMatrix
by calling toBlockMatrix
.
toBlockMatrix
creates blocks of size 1024 x 1024 by default.
Users may change the block size by supplying the values through toBlockMatrix(rowsPerBlock, colsPerBlock)
.
Refer to the BlockMatrix
Java docs for details on the API.
A BlockMatrix
can be created from an RDD
of sub-matrix blocks, where a sub-matrix block is a
((blockRowIndex, blockColIndex), sub-matrix)
tuple.
Refer to the BlockMatrix
Python docs for more details on the API.