How to assign to this sparse matrix with numpy indexing tricks instead of using loops?

Viewed 43

I have

  • M: a floating-point sparse matrix (csr_matrix in scipy) of shape p*n, where p and n are about 1 million each.
  • M': another floating-point sparse matrix of shape p'*n', where p' > p, and n'>n.
  • IndexP: a vector of indexes, of shape 1*p with p unique unsigned integers no larger than p'
  • IndexN: a vector of indexes, of shape 1*n with n unique unsigned integers no larger than n'

How can I transform the following loop into a fast sparse matrix array operation

for all i in range(n) and j in range (p) such that M[i,j]!=0:
   M'[IndexP[i], IndexN[j]]= M[i,j]

Example:

Assume

  • M is

    [ [0 2] [3 0] ]

  • IndexP is [9,100]

  • IndexN is [50,30]

  • M' is a sparse matrix such that M'.shape=(200,200)

Then the loop above will assign M'[9,30]=2, M'[100, 50]=3 (and M' is 0 elsewhere).

1 Answers
M = np.array([ [0, 2], [3, 0] ])
IndexP = np.array([9,100])
IndexN = np.array([50,30])
idxn, idxp = np.where(M)
M2 =np.zeros((200,200))
M2[IndexP[idxp], IndexN[idxn]] = M[idxp, idxn]
Related