我试图将numpy.array
长度为 40 的numpy.array
s拆分为较小的、大小相等的s,其中较小数组的数量由用户给出。允许在较小的阵列之间有一些重叠,因为在给定较小阵列的某种形式的重叠的情况下,可能发生全长只能被分裂整除的情况。
如果我有一个数组np.array([range(40)])
并且必须将其拆分为 37 个子数组,则子数组列表应该是这样的:
[1, 2, 3], [3, 4, 5], [5, 6, 7], ... [38, 39, 40]
Run Code Online (Sandbox Code Playgroud)
我尝试使用,numpy.split
但这仅在长度可被尺寸整除时才有效,并且会numpy.array_split
产生不均匀的尺寸。
使用示例 numpy.split
>> import numpy as np
>>> a = np.random.randint(6,size=(40))
>>> b = np.split(a,37)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/numpy/lib/shape_base.py", line 508, in split
'array split does not result in an equal division')
ValueError: array split does not result in an equal …
Run Code Online (Sandbox Code Playgroud)