1.

What are Sparse Vectors? How are they different from dense vectors?

Answer»

Sparse vectors consist of two parallel arrays where one array is for storing indices and the other for storing VALUES. These vectors are used to store non-zero values for saving space.

VAL sparseVec: Vector = Vectors.sparse(5, Array(0, 4), Array(1.0, 2.0))
  • In the above example, we have the vector of size 5, but the non-zero values are there only at indices 0 and 4.
  • Sparse vectors are particularly useful when there are very few non-zero values. If there are cases that have only a few zero values, then it is recommended to use dense vectors as usage of sparse vectors would introduce the overhead of indices which COULD impact the performance.
  • Dense vectors can be defines as follows:
val denseVec = Vectors.dense(4405d,260100d,400d,5.0,4.0,198.0,9070d,1.0,1.0,2.0,0.0)
  • Usage of sparse or dense vectors does not impact the results of calculations but when used inappropriately, they impact the memory consumed and the speed of CALCULATION.


Discussion

No Comment Found