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

Scalable Data Science

prepared by Raazesh Sainudiin and Sivanand Sivaram

supported by and

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


Data Types - MLlib Programming Guide

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.

Distributed matrix in Scala

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.

  1. 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.


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.

Remark: there is a huge difference in the orders of magnitude between the maximum size of local versus distributed matrices!

print(Long.MaxValue.toDouble, Int.MaxValue.toDouble, Long.MaxValue.toDouble / Int.MaxValue.toDouble) // index ranges and ratio for local and distributed matrices

Scalable Data Science

prepared by Raazesh Sainudiin and Sivanand Sivaram

supported by and