There's more...

The most difficult part of working with matrices in Spark is to getting used to column order versus row order. It is key to remember that Spark ML uses underlying libraries that work better with column stored mechanisms. Here is an example to demonstrate:

  1. Given a matrix definition which defines a 2x2 matrix:
val denseMat3 = Matrices.dense(2,2, Array(10.0, 11.0, 20.0, 30.3))
  1. The matrix is actually stored as :
10.0  20.0 11.0 30.3

You move from left to right in the value set and then from column to column for the placement in the Matrix.

  1. As you can see, the assumption that the matrix is stored row wise is not in alignment with the Spark approach. The following order is not correct from Spark's perspective:
 10.0  11.0 

Get Apache Spark 2.x Machine Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.