С разреженной матрицей вы можете получить улучшение, если плотность составляет менее 10%.Разреженная матрица может быть быстрее, в зависимости от того, включено ли время, необходимое для построения матрицы.
import timeit
setup=\
'''
import numpy as np
dim=1000
A = np.ones((dim, dim)) # the array to modify
B = np.random.random_integers(0, 1, (dim, dim)) # the values to ignore are 0
C = np.array(B, dtype = np.bool)
D = np.random.random((dim, dim)) # the array which will be used to modify A
'''
print('mult '+str(timeit.timeit('A += B * D', setup, number=3)))
print('index '+str(timeit.timeit('A[C] += D[C]', setup, number=3)))
setup3 = setup+\
'''
A = np.ma.array(np.ones((dim, dim)), mask = np.array(B - 1, dtype = np.bool))
'''
print('ma ' + str(timeit.timeit('A += D', setup3, number=3)))
setup4 = setup+\
'''
from scipy import sparse
S = sparse.csr_matrix(C)
DS = S.multiply(D)
'''
print('sparse- '+str(timeit.timeit('A += DS', setup4, number=3)))
setup5 = setup+\
'''
from scipy import sparse
'''
print('sparse+ '+str(timeit.timeit('S = sparse.csr_matrix(C); DS = S.multiply(D); A += DS', setup4, number=3)))
setup6 = setup+\
'''
from scipy import sparse
class Sparsemat(sparse.coo_matrix):
def __iadd__(self, other):
self.data += other.data
return self
A = Sparsemat(sparse.rand(dim, dim, 0.5, 'coo')) # the array to modify
D = np.random.random((dim, dim)) # the array which will be used to modify A
anz = A.nonzero()
'''
stmt6=\
'''
DS = Sparsemat((D[anz[0],anz[1]], anz), shape=A.shape) # new graph based on random weights
A += DS
'''
print('sparse2 '+str(timeit.timeit(stmt6, setup6, number=3)))
Вывод:
mult 0.0248420299535
index 0.32025789431
ma 0.1067024434
sparse- 0.00996273276303
sparse+ 0.228869672266
sparse2 0.105496183846
Изменить: Вы можетеиспользуйте код выше (setup6
) для расширения scipy.sparse.coo_matrix
.Сохраняет разреженный формат.