你看过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.]]