假设我有一个名为A
的列表。
A = [1,2,3,4,5,6,7,8,9,10]
我希望创建一个新的列表(称为
B
),其顺序如下所示,基于上述列表。B = [[1,2,3], [3,4,5], [5,6,7], [7,8,9], [9,10,]]
即取前三个数字作为A[0,1,2]
,后三个数字作为A[3,4,5]
等。
我相信在numpy
中有一种函数可以实现这样的操作。
使用Python内置的列表推导与切片即可实现:
>>> A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> size = 3
>>> step = 2
>>> A = [A[i : i + size] for i in range(0, len(A), step)]
这会给你想要的东西:
>>> A
[[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]]
但你需要写几行代码来确保你的程序不会因为未曾预料到的size/step值而出错。
list( zip( *[x[i::2] for i in range(3)]))
。 - hpauljnp.split
进行非重叠拆分。 该示例(在关闭后添加?)重叠,每个子数组跨越一个元素。此外,它用0填充。
如何将列表分成均匀大小的块?有一些很好的列表答案,具有各种形式的生成器或列表推导式,但乍一看,我没有看到任何允许重叠的方法-尽管通过巧妙地使用迭代器(例如iterator.tee
),应该是可能的。这是一个使用这里我的窗口大小为3,即每个拆分列表应具有3个元素,第一个拆分
[1,2,3]
,步长为2,因此第二个拆分应从第3个元素开始,第二个拆分分别为[3,4,5]。
as_strided
的高级解决方案。In [64]: ast=np.lib.index_tricks.as_strided # shorthand
In [65]: A=np.arange(1,12)
In [66]: ast(A,shape=[5,3],strides=(8,4))
Out[66]:
array([[ 1, 2, 3],
[ 3, 4, 5],
[ 5, 6, 7],
[ 7, 8, 9],
[ 9, 10, 11]])
A
的范围,因为我不想处理 0 填充。shape
很容易,5 组 3。选择步幅需要更多有关步幅的知识。In [69]: x.strides
Out[69]: (4,)
as_strided
生成一个视图。因此,在其中更改元素将影响原始数据,并可能更改重叠值。添加.copy()
以进行复制;使用步幅数组进行数学运算也会产生副本。In [82]: ast(A,shape=[4,3],strides=(12,4))
Out[82]:
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 17]])
In [84]: ast(A,shape=[3,3],strides=(16,4))
Out[84]:
array([[ 1, 2, 3],
[ 5, 6, 7],
[ 9, 10, 11]])
新的功能提供了一个更安全的 as_strided
版本。
np.lib.strided_tricks.sliding_window_view(np.arange(1,10),3)[::2]
np.lib.stride_tricks
,你多打了一个D。 - edoelas我写的这个函数可能会对你有所帮助,尽管它只输出长度为 len_chunk
的填充块:
def overlap(array, len_chunk, len_sep=1):
"""Returns a matrix of all full overlapping chunks of the input `array`, with a chunk
length of `len_chunk` and a separation length of `len_sep`. Begins with the first full
chunk in the array. """
n_arrays = np.int(np.ceil((array.size - len_chunk + 1) / len_sep))
array_matrix = np.tile(array, n_arrays).reshape(n_arrays, -1)
columns = np.array(((len_sep*np.arange(0, n_arrays)).reshape(n_arrays, -1) + np.tile(
np.arange(0, len_chunk), n_arrays).reshape(n_arrays, -1)), dtype=np.intp)
rows = np.array((np.arange(n_arrays).reshape(n_arrays, -1) + np.tile(
np.zeros(len_chunk), n_arrays).reshape(n_arrays, -1)), dtype=np.intp)
return array_matrix[rows, columns]
[1,2,3]
,步长为2,因此第二个分割应该从第3个元素开始,第二个分割分别是[3,4,5]。 - Rangooski