Python Numpy子矩阵(选定随机索引)计算性能
我试着用Numpy计算子矩阵 矩阵的形状是 A:(15000、100) B:(15000,100) B:(3000,100) C:(100100) 第一个代码是Python Numpy子矩阵(选定随机索引)计算性能,python,numpy,matrix-multiplication,Python,Numpy,Matrix Multiplication,我试着用Numpy计算子矩阵 矩阵的形状是 A:(15000、100) B:(15000,100) B:(3000,100) C:(100100) 第一个代码是 for ki in range(100): self.A[sample_index, k] += B_[:, k] - np.dot(self.A[sample_index, : ], C[:, k]) 它只使用从样本索引切片的子矩阵 第二个代码是 for k in range(100): self.A[:, k] +
for ki in range(100):
self.A[sample_index, k] += B_[:, k] - np.dot(self.A[sample_index, : ], C[:, k])
它只使用从样本索引切片的子矩阵
第二个代码是
for k in range(100):
self.A[:, k] += B[:, k] - np.dot(self.A[:, : ], C[:, k])
使用所有矩阵
但第一个代码的计算时间比第二个代码慢
您知道加速的原因或解决方案吗?您实际上是在复制输入矩阵。如果您只是读取输入,则不必复制它
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) # True
试试
A[:3000,k]
;切片索引比数组索引(基本v高级索引)快。为什么要在k
上查看?为什么不A[:,:]+=B-np.dot(A,C)
?实际上,我想选择随机索引sample\u index=np.random.choice(np.arange(int(15000*0.2))、size=int(15000*0.2)、replace=False)和sample\u index=np.arange(int(15000*0.2))只用于测试,在任何情况下都有一个折衷。索引减少了计算的规模,但本身需要时间。另外,我认为sample\u index=np.random.permutation(int(15000*.2))
将比第一步更快
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) # True