Som*_*hin 7 python arrays numpy
我试图将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 division
Run Code Online (Sandbox Code Playgroud)
与 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,)
>>>
Run Code Online (Sandbox Code Playgroud)
numpy.array_split
不要平均分配它们。
有什么解决办法吗?
您所描述的称为(滑动)窗口,而不是拆分。
请参阅此答案:https ://stackoverflow.com/a/15722507/7802200
您想要的是使用window_stack
a width
of在那里开发的函数len(a) - n_splits + 1
。
归档时间: |
|
查看次数: |
7517 次 |
最近记录: |