Split array into windows of equal size

I am trying to divide a numpy.array

length of 40 by a smaller numpy.array

, equal size, in which the number of smaller arrays is user-defined. Some overlap is allowed between smaller arrays, as situations may arise where the total length is divisible only by the partitions, given some form of overlap of the smaller arrays.

If I had an array np.array([range(40)])

AND I had to split it into 37 sub arrays, the list of subarrays should be like this:

[1, 2, 3], [3, 4, 5], [5, 6, 7], ... [38, 39, 40]

      

I tried to use numpy.split

, but this only works when length is divisible by size and numpy.array_split

generates uneven sizes.

Usage example 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

      

And with 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

don't separate them.

Any solution?

+3


source to share


2 answers


What you are describing is called a (sliding) window, not a split.

See this answer: fooobar.com/questions/443976 / ...



You want to use a function window_stack

developed there with width

len(a) - n_splits + 1

.

+2


source


numpy.split()

must generate auxiliary arrays of equal size, exactly the size specified in the parameter indices_or_sections

, which is the second input of the function. How have you tried using split functions? If you want to split the array arr

into 4 subarrays of size 5 use

numpy.split(arr,4)

      



The reason your example fails is because the size of the array must be divisible by the number of arrays required. This makes sense because you can only divide by equal sizes when the number of elements divided by the number of subarrays is an integer.

Now that you have clarified your question, I think there is no easy solution. It is easy to split an array into equal size groups, but if a user requests 37 equal size groups out of a set of 40 with overlap allowed, there is more than one way to do this, and you did not give us criteria for defining such methods to help you. There won't be any simple numpy function for the task you are looking for, you need a search algorithm (DFS?) To figure out how to split a set into 37 (or whatever is requested) groups of the same size.

0


source







All Articles