Rare vector vs dense vector

How to create SparseVector and dense vector representations

if DenseVector :

 denseV = np.array([0., 3., 0., 4.]) 

What will be the representation of a sparse vector?

+8
source share
2 answers

If I do not fully understand your doubts, the documentation of the MLlib data type clearly illustrates this:

 import org.apache.spark.mllib.linalg.Vector; import org.apache.spark.mllib.linalg.Vectors; // Create a dense vector (1.0, 0.0, 3.0). Vector dv = Vectors.dense(1.0, 0.0, 3.0); // Create a sparse vector (1.0, 0.0, 3.0) by specifying its indices and values corresponding to nonzero entries. Vector sv = Vectors.sparse(3, new int[] {0, 2}, new double[] {1.0, 3.0}); 

Where the second argument of Vectors.sparse is an array of indices, and the third argument is an array of actual values ​​in these indices.

+16
source

Rare vectors are when you have many values ​​in the vector as zero. While a dense vector is when most of the values ​​in the vector are nonzero.

If you need to create a sparse vector from the specified dense vector, use the following syntax:

 Vector sparseVector = Vectors.sparse(4, new int[] {1, 3}, new double[] {3.0, 4.0}); 
+10
source

All Articles