1

I have a csr sparse scipy matrix X which is order (10^4, 10^7) elements, and a numpy array r with 10^7 elements. I need to multiply each column of the X matrix by the corresponding element of r, i.e., I need:

X_new[:,i] = X[:,i] * r[i]

In theory is this done by using X.multiply(r), but the multiply method tries to make X dense and this gives a MemoryError. Doing

for i in range(len(r)):
    X_new[:,i] = X[:,i] * r[i]

Takes impossibly long. I would like to know if anyone has any idea how to do this.

Cleb
  • 25,102
  • 20
  • 116
  • 151
jpjandrade
  • 451
  • 1
  • 4
  • 11
  • 1
    Is `multiply` really trying to make X dense, or is the resulting matrix (which isn't going to be dense) the issue? – Suever Jan 21 '16 at 21:40
  • @Suever The result will be a regular `np.matrix`, not a `scipy.sparse` matrix. – ali_m Jan 21 '16 at 22:52

0 Answers0