Python 中稀疏矩阵的矩阵乘法

发布于 2024-12-05 10:00:48 字数 354 浏览 0 评论 0原文

我想将稀疏矩阵 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生漫 2024-12-12 10:00:48

你看过scipy.sparse吗?在这里重新发明轮子是没有意义的。稀疏矩阵是相当标准的事情。

(在示例中,我使用 300000x4 矩阵,以便在乘法后更轻松地打印。不过,300000x1000 矩阵不应该有任何问题。这会快得多假设您有大多数 0 元素,而不是将两个密集数组相乘。)

import scipy.sparse
import numpy as np

# Make the result reproducible...
np.random.seed(1977)

def generate_random_sparse_array(nrows, ncols, numdense):
    """Generate a random sparse array with -1 or 1 in the non-zero portions"""
    i = np.random.randint(0, nrows-1, numdense)
    j = np.random.randint(0, ncols-1, numdense)
    data = np.random.random(numdense)
    data[data <= 0.5] = -1
    data[data > 0.5] = 1
    ij = np.vstack((i,j))
    return scipy.sparse.coo_matrix((data, ij), shape=(nrows, ncols))

A = generate_random_sparse_array(4, 300000, 1000)
B = generate_random_sparse_array(300000, 5, 1000)

C = A * B

print C.todense()

这会产生:

[[ 0.  1.  0.  0.  0.]
 [ 0.  2. -1.  0.  0.]
 [ 1. -1.  0.  0.  0.]
 [ 0.  0.  0.  0.  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. A 300000x1000 matrix shouldn't be any problem, though. This will be much faster than multiplying two dense arrays, assuming you have a majority of 0 elements.)

import scipy.sparse
import numpy as np

# Make the result reproducible...
np.random.seed(1977)

def generate_random_sparse_array(nrows, ncols, numdense):
    """Generate a random sparse array with -1 or 1 in the non-zero portions"""
    i = np.random.randint(0, nrows-1, numdense)
    j = np.random.randint(0, ncols-1, numdense)
    data = np.random.random(numdense)
    data[data <= 0.5] = -1
    data[data > 0.5] = 1
    ij = np.vstack((i,j))
    return scipy.sparse.coo_matrix((data, ij), shape=(nrows, ncols))

A = generate_random_sparse_array(4, 300000, 1000)
B = generate_random_sparse_array(300000, 5, 1000)

C = A * B

print C.todense()

This yields:

[[ 0.  1.  0.  0.  0.]
 [ 0.  2. -1.  0.  0.]
 [ 1. -1.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文