Python 中稀疏矩阵的矩阵乘法
我想将稀疏矩阵 A 与以 0、-1 或 1 作为元素的矩阵 B 相乘。为了降低矩阵乘法的复杂性,如果项为 0,我可以忽略它们,或者如果项为 1 或 subs,则继续添加列而不进行乘法。如果是-1。关于此的讨论在这里:
现在我可以继续实现这个技巧,但我想知道如果我使用 Numpy 的乘法函数,它会更快。
有谁知道他们是否优化了此类矩阵的矩阵乘法?或者你能提出一些建议来加速这个过程,因为我有一个 300000x1000 的矩阵。
I want to multiply a sparse matrix A, with a matrix B which has 0, -1, or 1 as elements. To reduce the complexity of the matrix multiplication, I can ignore items if they are 0, or go ahead and add the column without multiplication if the item is 1, or subs. if it's -1. The discussion about this is here:
Random projection algorithm pseudo code
Now I can go ahead and implement this trick but I wonder if I use Numpy's multiplication functions it'll be faster.
Does anyone knows if they optimised matrix multiplication for such matrices? Or can you suggest something to speed this process up since I have a matrix 300000x1000.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你看过
scipy.sparse
吗?在这里重新发明轮子是没有意义的。稀疏矩阵是相当标准的事情。(在示例中,我使用
300000x4
矩阵,以便在乘法后更轻松地打印。不过,300000x1000
矩阵不应该有任何问题。这会快得多假设您有大多数0
元素,而不是将两个密集数组相乘。)这会产生:
Have you looked at
scipy.sparse
? There's no point in re-inventing the wheel, here. Sparse matricies are a fairly standard thing.(In the example, I'm using a
300000x4
matrix for easier printing after the multiplication. A300000x1000
matrix shouldn't be any problem, though. This will be much faster than multiplying two dense arrays, assuming you have a majority of0
elements.)This yields: