设 x 为形状为(A,B)且 y 的矩阵为大小为A + B-1的数组。
for i in range(A):
for j in range(B):
x[i][j] = y[i+j]
如何使用numpy中的函数有效地实现等效代码?
答案 0 :(得分:7)
方法#1 使用Scipy's hankel
-
from scipy.linalg import hankel
x = hankel(y[:A],y[A-1:]
方法#2 使用NumPy broadcasting
-
x = y[np.arange(A)[:,None] + np.arange(B)]
方法#3 使用NumPy strides technique
-
n = y.strides[0]
x = np.lib.stride_tricks.as_strided(y, shape=(A,B), strides=(n,n))
运行时测试 -
In [93]: def original_app(y,A,B):
...: x = np.zeros((A,B))
...: for i in range(A):
...: for j in range(B):
...: x[i][j] = y[i+j]
...: return x
...:
...: def strided_method(y,A,B):
...: n = y.strides[0]
...: return np.lib.stride_tricks.as_strided(y, shape=(A,B), strides=(n,n))
...:
In [94]: # Inputs
...: A,B = 100,100
...: y = np.random.rand(A+B-1)
...:
In [95]: np.allclose(original_app(y,A,B),hankel(y[:A],y[A-1:]))
Out[95]: True
In [96]: np.allclose(original_app(y,A,B),y[np.arange(A)[:,None] + np.arange(B)])
Out[96]: True
In [97]: np.allclose(original_app(y,A,B),strided_method(y,A,B))
Out[97]: True
In [98]: %timeit original_app(y,A,B)
100 loops, best of 3: 5.29 ms per loop
In [99]: %timeit hankel(y[:A],y[A-1:])
10000 loops, best of 3: 114 µs per loop
In [100]: %timeit y[np.arange(A)[:,None] + np.arange(B)]
10000 loops, best of 3: 60.5 µs per loop
In [101]: %timeit strided_method(y,A,B)
10000 loops, best of 3: 22.4 µs per loop
基于strides
-
似乎strides
技术已在少数几个地方使用:extract_patches
和view_as_windows
,用于此类基于图像处理的模块。那么,有了这些,我们还有两种方法 -
from skimage.util.shape import view_as_windows
from sklearn.feature_extraction.image import extract_patches
x = extract_patches(y,(B))
x = view_as_windows(y,(B))
In [151]: np.allclose(original_app(y,A,B),extract_patches(y,(B)))
Out[151]: True
In [152]: np.allclose(original_app(y,A,B),view_as_windows(y,(B)))
Out[152]: True
In [153]: %timeit extract_patches(y,(B))
10000 loops, best of 3: 62.4 µs per loop
In [154]: %timeit view_as_windows(y,(B))
10000 loops, best of 3: 108 µs per loop