我尝试使用Numpy计算子矩阵。
矩阵的形状是
答:(15000,100)
B:(15000,100)
B_:(3000,100)
C:(100,100)
sample_index = np.random.choice(np.arange(int(15000*0.2)), size=int(int(15000*0.2)), replace=False)第一个代码是
for ki in range(100):
self.A[sample_index, k] += B_[:, k] - np.dot(self.A[sample_index, : ], C[:, k])它只使用从sample_index切片的子矩阵
第二个代码是
for k in range(100):
self.A[:, k] += B[:, k] - np.dot(self.A[:, : ], C[:, k])它使用所有的矩阵。
但是第一个代码的计算时间比第二个代码慢。
你知道任何加速的原因或解决方案吗?
发布于 2017-02-03 14:42:59
您实际上是在复制输入矩阵。如果您只是在读取输入,则不必复制它。
import numpy as np
a = np.random.rand(10000).reshape(100, 100)
b = np.random.rand(10000).reshape(100, 100)
i = list(range(10))
a_sub0 = a[:10] # view
a_sub1 = a[i] # copying
# you can change the original matrix from the view
a_sub0[0, 0] = 100
(a[0, 0] == 100.0) and (a_sub1[0, 0] != 100.0) # Truehttps://stackoverflow.com/questions/42017892
复制相似问题