您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python中稀疏矩阵的矩阵乘法

Python中稀疏矩阵的矩阵乘法

你看了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.]]
python 2022/1/1 18:25:24 有520人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶