0

I am trying to add a numpy ndarray to a sparse matrix and I have been unsuccessful in doing so. I was wondering if there is a way to do so, without transforming my sparse matrix into a dense one.

another question is if adding two sparse matrices is possible.

 x = np.dot(aSparseMatrix, weights)
 y = x + bias

where x is my sparse matrix and bias is the numpy array. The error that I get is currently:

NotImplementedError: adding a scalar to a CSC or CSR matrix is not supported

aSparseMatrix.shape (1, 10063)

weights.shape  (10063L, 2L)

bias.shape  (2L,)
4
  • Wouldn't adding a dense matrix to a sparse one effectively turn it dense? Commented Jan 10, 2013 at 4:37
  • 1
    No. it apparently doesnt. Commented Jan 10, 2013 at 4:39
  • I mean that I don't understand what advantage you get from not doing a x.todense() on your spare matrix before adding them together, since the result is not going to have many non-zero items. Or alternatively, if it does, you could turn your dense matrix into sparse format with csc_matrix(bias) before adding them. It nevertheless looks like bias is not an ndarray but a scalar, have you tried doing np.array(bias) before adding them? Commented Jan 10, 2013 at 4:52
  • I can't turn my sparse matrix into a dense because it is so big that the dense form will not fit into the memory. I also have tried turning bias into a sparse matrix but it still couldn't add them and np.array(bias) didnt work. Commented Jan 10, 2013 at 4:55

2 Answers 2

6

There are different kinds of scipy.sparse matrices: csr_matrix is fast for matrix algebra but slow to update, coo_matrix slow for algebra / fast to update. They're described in scipy.org/SciPyPackages/Sparse.

If a sparse matrix is 99 % 0, sparsematrix + 1 is 99 % ones -- dense.
You can hand-expand e.g.
y = dot( x + bias, npvec )
to dot( x, npvec ) + bias * npvec
wherever y is used later -- possible for short bits of code, but no fun.

I highly recommend IPython for trying things out:

# add some combinations of scipy.sparse matrices + numpy vecs
# see http://www.scipy.org/SciPyPackages/Sparse

from __future__ import division
import numpy as np
from scipy import sparse as sp

npvec = np.tile( [0,0,0,0,1.], 20 )
Acsr = sp.csr_matrix(npvec)
Acoo = Acsr.tocoo()

for A in (Acsr, Acoo, npvec):
    print "\n%s" % type(A)
    for B in (Acsr, Acoo, npvec):
        print "+ %s = " % type(B) ,
        try:
            AplusB = A + B
            print type(AplusB)
        except StandardError, errmsg:
            print "Error", errmsg
Sign up to request clarification or add additional context in comments.

Comments

0

instead of numpy dot product, use simple * product. Python will broadcast and the correct result will be obtained.

the addition doesn't work, because through numpy dot product, the size of the result matrix didn't match the expected and so the addition could not take place between two matrices that have different shapes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.