Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a sparse vector to dense in Scala Spark?

I have a sparse vector:

(453,[0,4,11,16,39,56,109],[1.0,1.0,1.0,1.0,1.0,1.0,1.0])

and I need to convert it into dense vector (should be able to see all 453 values).

How to do that in Scala Spark?

like image 357
happy_coder Avatar asked Feb 21 '16 05:02

happy_coder


People also ask

What is sparse vector in spark?

A sparse vector is used for storing non-zero entries for saving space. It has two parallel arrays: One for indices. The other for values.

What is sparse and dense vector?

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.

What is sparse vector representation?

A sparse vector is a vector having a relatively small number of nonzero elements. Consider the following as an example of a sparse vector x with n elements, where n is 11, and vector x is: (0.0, 0.0, 1.0, 0.0, 2.0, 3.0, 0.0, 4.0, 0.0, 5.0, 0.0) In Storage.


1 Answers

It can be done by using SparseVector's toDense method:

val sv = Vectors.sparse(5, Array(0, 3), Array(1.5, -1.5))
sv.toDense
// res0: org.apache.spark.mllib.linalg.DenseVector = [1.5,0.0,0.0,-1.5,0.0]
like image 102
happy_coder Avatar answered Oct 22 '22 22:10

happy_coder