Sta*_*ckd 25 python numpy vectorization
假设我有一个Python Numpy数组a.
a = numpy.array([1,2,3,4,5,6,7,8,9,10,11])
Run Code Online (Sandbox Code Playgroud)
我想从这个长度为5的数组创建一个子序列矩阵,步长为3.结果矩阵因此如下所示:
numpy.array([[1,2,3,4,5],[4,5,6,7,8],[7,8,9,10,11]])
Run Code Online (Sandbox Code Playgroud)
实现这一点的一种可能方式是使用for循环.
result_matrix = np.zeros((3, 5))
for i in range(0, len(a), 3):
result_matrix[i] = a[i:i+5]
Run Code Online (Sandbox Code Playgroud)
有没有更简洁的方法来实现这个Numpy?
Div*_*kar 40
方法#1: 使用broadcasting-
def broadcasting_app(a, L, S ): # Window len = L, Stride len/stepsize = S
nrows = ((a.size-L)//S)+1
return a[S*np.arange(nrows)[:,None] + np.arange(L)]
Run Code Online (Sandbox Code Playgroud)
方法#2:使用更高效NumPy strides-
def strided_app(a, L, S ): # Window len = L, Stride len/stepsize = S
nrows = ((a.size-L)//S)+1
n = a.strides[0]
return np.lib.stride_tricks.as_strided(a, shape=(nrows,L), strides=(S*n,n))
Run Code Online (Sandbox Code Playgroud)
样品运行 -
In [143]: a
Out[143]: array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
In [144]: broadcasting_app(a, L = 5, S = 3)
Out[144]:
array([[ 1, 2, 3, 4, 5],
[ 4, 5, 6, 7, 8],
[ 7, 8, 9, 10, 11]])
In [145]: strided_app(a, L = 5, S = 3)
Out[145]:
array([[ 1, 2, 3, 4, 5],
[ 4, 5, 6, 7, 8],
[ 7, 8, 9, 10, 11]])
Run Code Online (Sandbox Code Playgroud)
Xav*_*hot 11
从 开始Numpy 1.20,我们可以使用新功能sliding_window_view来滑动/滚动元素窗口。
再加上stepping [::3],它就变成了:
from numpy.lib.stride_tricks import sliding_window_view
# values = np.array([1,2,3,4,5,6,7,8,9,10,11])
sliding_window_view(values, window_shape = 5)[::3]
# array([[ 1, 2, 3, 4, 5],
# [ 4, 5, 6, 7, 8],
# [ 7, 8, 9, 10, 11]])
Run Code Online (Sandbox Code Playgroud)
其中滑动的中间结果为:
sliding_window_view(values, window_shape = 5)
# array([[ 1, 2, 3, 4, 5],
# [ 2, 3, 4, 5, 6],
# [ 3, 4, 5, 6, 7],
# [ 4, 5, 6, 7, 8],
# [ 5, 6, 7, 8, 9],
# [ 6, 7, 8, 9, 10],
# [ 7, 8, 9, 10, 11]])
Run Code Online (Sandbox Code Playgroud)