【发布时间】:2015-03-21 01:29:10
【问题描述】:
我正在使用 numpy 来做线性代数。我想做快速子集索引 dot 和其他线性操作。
在处理大矩阵时,像A[:,subset].dot(x[subset]) 这样的切片解决方案可能比对整个矩阵进行乘法运算要长。
A = np.random.randn(1000,10000)
x = np.random.randn(10000,1)
subset = np.sort(np.random.randint(0,10000,500))
时间表明,当列在一个块中时,子索引可以更快。
%timeit A.dot(x)
100 loops, best of 3: 4.19 ms per loop
%timeit A[:,subset].dot(x[subset])
100 loops, best of 3: 7.36 ms per loop
%timeit A[:,:500].dot(x[:500])
1000 loops, best of 3: 1.75 ms per loop
仍然不是我所期望的加速(快 20 倍!)。
有谁知道允许通过 numpy 或 scipy 进行此类快速操作的库/模块的想法?
现在我正在使用 cython 通过 cblas 库编写快速的列索引点积。但对于更复杂的操作(伪逆或子索引最小二乘求解),我不保证达到良好的加速度。
谢谢!
【问题讨论】:
-
您为什么期望 20 倍加速?
-
天真地,计算 10000 列的结果应该比计算 500 快 20 倍。
标签: python numpy scipy cython blas