简体   繁体   English

如何在Scala Spark中将稀疏矢量转换为密集?

[英]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). 我需要将其转换为密集向量(应该能够看到所有453个值)。

How to do that in Scala Spark? 如何在Scala Spark中做到这一点?

It can be done by using SparseVector 's toDense method: 可以使用SparseVectortoDense方法完成:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM