happy_coder
happy_coder

Reputation: 354

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?

Upvotes: 6

Views: 10169

Answers (1)

happy_coder
happy_coder

Reputation: 354

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]

Upvotes: 11

Related Questions