// Databricks notebook source exported at Sun, 19 Jun 2016 03:00:13 UTC

# Scalable Data Science

### prepared by Raazesh Sainudiin and Sivanand Sivaram

This is an elaboration of the Apache Spark 1.6 mllib-progamming-guide on mllib-data-types.

# Overview

## Data Types - MLlib Programming Guide

- Local vector and URL
- Labeled point and URL
- Local matrix and URL
- Distributed matrix and URL
- RowMatrix and URL
- IndexedRowMatrix and URL
- CoordinateMatrix and URL
- BlockMatrix and URL

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 and jblas. A training example used in supervised learning is called a “labeled point” in MLlib.

## Local vector in Scala

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.

```
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)))
```

** 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`

.Local Vector in Python ——

**python**: 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.

```
%py
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))
```

```
%py
print dv1
print dv2
print sv1
print sv2
```