Skip to content
Snippets Groups Projects
  • Shuai Lin's avatar
    c4e0fde8
    [MINOR][DOC] Fixed some python snippets in mllib data types documentation. · c4e0fde8
    Shuai Lin authored
    ## What changes were proposed in this pull request?
    
    Some python snippets is using scala imports and comments.
    
    ## How was this patch tested?
    
    Generated the docs locally with `SKIP_API=1 jekyll build` and viewed the changes in the browser.
    
    Author: Shuai Lin <linshuai2012@gmail.com>
    
    Closes #12869 from lins05/fix-mllib-python-snippets.
    c4e0fde8
    History
    [MINOR][DOC] Fixed some python snippets in mllib data types documentation.
    Shuai Lin authored
    ## What changes were proposed in this pull request?
    
    Some python snippets is using scala imports and comments.
    
    ## How was this patch tested?
    
    Generated the docs locally with `SKIP_API=1 jekyll build` and viewed the changes in the browser.
    
    Author: Shuai Lin <linshuai2012@gmail.com>
    
    Closes #12869 from lins05/fix-mllib-python-snippets.
mllib-data-types.md 30.34 KiB
layout: global
title: Data Types - MLlib
displayTitle: Data Types - MLlib
  • 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 %}

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:

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