Python 以给定步幅/步长从numpy阵列中获取子阵列
假设我有一个Python Numpy数组Python 以给定步幅/步长从numpy阵列中获取子阵列,python,numpy,vectorization,Python,Numpy,Vectorization,假设我有一个Python Numpy数组a a = numpy.array([1,2,3,4,5,6,7,8,9,10,11]) 我想从这个长度为5,步长为3的数组中创建一个子序列矩阵。因此,结果矩阵如下所示: numpy.array([[1,2,3,4,5],[4,5,6,7,8],[7,8,9,10,11]]) 一种可能的实现方法是使用for循环 result_matrix = np.zeros((3, 5)) for i in range(0, len(a), 3): result
a
a = numpy.array([1,2,3,4,5,6,7,8,9,10,11])
我想从这个长度为5,步长为3的数组中创建一个子序列矩阵。因此,结果矩阵如下所示:
numpy.array([[1,2,3,4,5],[4,5,6,7,8],[7,8,9,10,11]])
一种可能的实现方法是使用for循环
result_matrix = np.zeros((3, 5))
for i in range(0, len(a), 3):
result_matrix[i] = a[i:i+5]
在Numpy中有没有更干净的方法来实现这一点?方法1:使用-
方法#2:使用更高效的-
样本运行-
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]])
修改了@Divakar代码的版本,并进行了检查,以确保内存连续且返回的数组无法修改。(为我的DSP应用程序更改了变量名)
从
Numpy 1.20
开始,我们可以使用新的元素滑动/滚动窗口
再加上一个单步的[::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]])
其中,滑动的中间结果为:
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]])
谢谢,我试过这个:X=np.arange(100)Y=stripped_app(X,4,1),它给出了预期的Y,现在:Z=stripped_app(Y,8,4)#我希望Z通过长度为8的移动窗口和步骤4查看Y,但这会导致垃圾。你能纠正一下吗?我以前使用过
,但发现它导致了非常严重的内存泄漏。这对于小型阵列不是问题,但即使在服务器上使用64 GB的RAM,我的python程序也会引发内存错误。强烈推荐使用broadcasting\u应用程序方法。伙计,这太自动化了!。我正在实现Shi Tomasi角点检测算法,我必须为每个像素创建一个窗口并计算一些复杂的东西。这个方法立刻给了我所有的窗口@kkawabat他们只是说我们在使用它时需要小心,理解它的功能。为了安全起见,可以将可写
标志添加到。还有像scikit图像这样的模块。@AndyL。井输入数组是1D,所以n=a.strips[0]
很好。
def frame(a, framelen, frameadv):
"""frame - Frame a 1D array
a - 1D array
framelen - Samples per frame
frameadv - Samples between starts of consecutive frames
Set to framelen for non-overlaping consecutive frames
Modified from Divakar's 10/17/16 11:20 solution:
https://stackoverflow.com/questions/40084931/taking-subarrays-from-numpy-array-with-given-stride-stepsize
CAVEATS:
Assumes array is contiguous
Output is not writable as there are multiple views on the same memory
"""
if not isinstance(a, np.ndarray) or \
not (a.flags['C_CONTIGUOUS'] or a.flags['F_CONTIGUOUS']):
raise ValueError("Input array a must be a contiguous numpy array")
# Output
nrows = ((a.size-framelen)//frameadv)+1
oshape = (nrows, framelen)
# Size of each element in a
n = a.strides[0]
# Indexing in the new object will advance by frameadv * element size
ostrides = (frameadv*n, n)
return np.lib.stride_tricks.as_strided(a, shape=oshape,
strides=ostrides, writeable=False)
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]])
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]])