gpt4 book ai didi

python - 如何在 numpy 中有效地实现 x[i][j] = y[i+j] ?

转载 作者:太空狗 更新时间:2023-10-30 02:17:41 25 4
gpt4 key购买 nike

x 是形状为(A,B) 的矩阵,y 是大小为A+B-1 的数组。

for i in range(A):
for j in range(B):
x[i][j] = y[i+j]

如何使用 numpy 中的函数高效地实现等效代码?

最佳答案

方法 #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 技术 -

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_patchesview_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

关于python - 如何在 numpy 中有效地实现 x[i][j] = y[i+j] ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39426690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com