更新时间:2021-09-16 01:48:13
使用新坐标中的1
s创建第二个矩阵并将其添加到现有的矩阵中是一种可行的方法:
Creating a second matrix with 1
s in your new coordinates and adding it to the existing one is a possible way of doing this:
>>> import scipy.sparse as sps
>>> shape = (1000, 2000)
>>> rows, cols = 1000, 2000
>>> sps_acc = sps.coo_matrix((rows, cols)) # empty matrix
>>> for j in xrange(100): # add 100 sets of 100 1's
... r = np.random.randint(rows, size=100)
... c = np.random.randint(cols, size=100)
... d = np.ones((100,))
... sps_acc = sps_acc + sps.coo_matrix((d, (r, c)), shape=(rows, cols))
...
>>> sps_acc
<1000x2000 sparse matrix of type '<type 'numpy.float64'>'
with 9985 stored elements in Compressed Sparse Row format>