Perform matrix multiplication between two arrays and get result only on masked places

前端 未结 3 790
夕颜
夕颜 2021-01-24 17:18

I have two dense matrices, A [200000,10], B [10,100000]. I need to multiply them to get matrix C. I can\'t do that directly, s

3条回答
  •  灰色年华
    2021-01-24 17:37

    Since a matrix multiplication is just a table of dot products, we can just perform the specific dot products we need, in a vectorized fashion.

    import numpy as np
    import scipy as sp
    
    iX, iY = sp.nonzero(W)
    values = np.sum(A[iX]*B[:, iY].T, axis=-1) #batched dot product
    C = sp.sparse.coo_matrix(values, np.asarray([iX,iY]).T)
    

提交回复
热议问题