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