我正在尝试拆分numpy.array
长度为 40 的较小的、大小相等的numpy.array
s,其中较小数组的数量由用户给出。较小数组之间允许有一些重叠,因为在给定较小数组某种形式的重叠的情况下,可能会出现全长只能被分割整除的情况。
如果我有一个数组np.array([range(40)])
我必须将它分成 37 个子数组,子数组列表应该是这样的:
[1, 2, 3], [3, 4, 5], [5, 6, 7], ... [38, 39, 40]
我尝试使用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 division
与numpy.array_split
>>> a = np.random.randint(5,size=(40))
>>> b = np.array_split(a,37)
>>> print len(b)
37
>>> print b[0].shape
(2,)
>>> print b[3].shape
(1,)
>>> print b[5].shape
(1,)
>>> print b[6].shape
(1,)
>>> print b[30].shape
(1,)
>>>
numpy.array_split
不要平均分配它们。
有什么解决办法吗?