Python / Numpy: split an array

I have some data presented in a 1300x1341 matrix. I would like to break this matrix into several parts (for example, 9) so that I can iterate and process them. The data should remain ordered in the sense that x [0,1] remains lower (or higher, if you want) x [0,0] and other than x [1,1].
Just as if you were displaying data, you could draw 2 vertical and 2 horizontal lines above the image to illustrate 9 parts.

If I use numpys reshape (e.g. matrix.reshape (9,260,745) or any other combination of 9,260,745), it does not give the required structure as the above ordering is lost ...

I misunderstood the reshape method or can it be done this way?

What other pythonic / numpy way to do this?

+4
source share
2 answers

It sounds like you need to use numpy.split() , which has the documentation here ... or maybe its brother numpy.array_split() here . They are designed to split the array into equal sub-sections without re-arranging numbers such as reshape,

I have not tested this, but something like:

 numpy.array_split(numpy.zeros((1300,1341)), 9) 

gotta do the trick.

+4
source

change the form, quote its docs ,

Gives a new form to an array without changing its data.

In other words, it does not move the data of the array at all - it just affects the size of the array. On the other hand, it seems to you that slicing is required; quoting again:

You can cut and step arrays to extract arrays of the same number of dimensions, but different sizes than the original. Slicing and stepping work exactly the same way for lists and tuples except that they can also be applied to several sizes.

So, for example, thearray[0:260, 0:745] is the "upper left part", thearray[260:520, 0:745] upper left part of the center, etc. You can have links to various parts in the list (or dict with the corresponding keys) for processing them separately.

+2
source

Source: https://habr.com/ru/post/1308887/


All Articles