如何在numpy中有效地实现x [i] [j] = y [i + j]? [英] How can I implement x[i][j] = y[i+j] efficiently in numpy?

查看:200
本文介绍了如何在numpy中有效地实现x [i] [j] = y [i + j]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let x be a matrix with a shape of (A,B) and y be an array with a size of A+B-1.

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

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

How can I implement equivalent code efficiently using functions in numpy?

推荐答案

方法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))


运行时测试-


Runtime test -

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技术似乎已在几个地方使用:在此类基于图像处理的模块中使用的view_as_windows .因此,有了这些,我们还有另外两种方法-

It seems strides technique has been used at few places : extract_patches and view_as_windows that are being used in such image-processing based modules. So, with those, we have two more approaches -

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

这篇关于如何在numpy中有效地实现x [i] [j] = y [i + j]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆