Sparse Vector vs Dense Vector

后端 未结 2 1677
醉酒成梦
醉酒成梦 2020-12-28 17:28

How to create SparseVector and dense Vector representations

if the DenseVector is:

d         


        
2条回答
  •  佛祖请我去吃肉
    2020-12-28 18:16

    Unless I have thoroughly misunderstood your doubt, the MLlib data type documentation illustrates this quite clearly:

    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 the indices, and the third argument is the array of the actual values in those indices.

提交回复
热议问题