Commute numpy sparse matrix dot product

别等时光非礼了梦想. 提交于 2019-12-12 16:23:13

问题


To my understanding, numpy.sparse.csr_sparse.dot(other) does multiply other to my sparse matrix from the right:

A = numpy.sparse.csr_sparse(something)
B = numpy.matrix(something)
C = A.dot(B)                     # C = A*B

How do I commute the two matrices to get B*A without losing the benefits of saving my matrix as a sparse one (i.e. .todense() etc.)?


回答1:


A little refresher of matrix multiplication properties:

D = B * A
D.T = A.T * B.T
D = (A.T * B.T).T

Which then leads to the obvious:

D = A.T.dot(B.T).T

Note that transposition of CSR and CSC matrices is very fast, as it simply changes the shape and the type (from CSR to CSC, or from CSC to CSR), leaving the internal data unchanged.



来源:https://stackoverflow.com/questions/19682378/commute-numpy-sparse-matrix-dot-product

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!