Search code examples
pythonscipysparse-matrix

Sparse-Dense multiplication in Python


I am using Python 3.23 and I am want to multiply a sparse VECTOR with a dense MATRIX. The idea of first unfolding the sparse vector into a dense one and then multiplying is of course silly from any standpoint except for mem management until the actual unfolding. It will be more expensive with zeros in there...

Also, does any one know of a good way for SciPy to keep one dimensional matrices in sparse mode? The only one (admittedly) i have used is the classical notation of three vectors (x,y,value), so i have had to use np.ones(len(...)) to get it to work.

Well.. comments welcome!


Solution

  • Store the vector using the Scipy sparse matrix classes:

    x = csr_matrix(np.random.rand(1000) > 0.99).T
    print x.shape   # (1000, 1)