Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Numpy: Divide array

Tags:

python

numpy

I have some data represented in a 1300x1341 matrix. I would like to split this matrix in several pieces (e.g. 9) so that I can loop over and process them. The data needs to stay ordered in the sense that x[0,1] stays below (or above if you like) x[0,0] and besides x[1,1].
Just like if you had imaged the data, you could draw 2 vertical and 2 horizontal lines over the image to illustrate the 9 parts.

If I use numpys reshape (eg. matrix.reshape(9,260,745) or any other combination of 9,260,745) it doesn't yield the required structure since the above mentioned ordering is lost...

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

What other pythonic/numpy way is there to do this?

like image 598
BandGap Avatar asked May 05 '10 13:05

BandGap


People also ask

Can you divide NumPy array?

Splitting NumPy ArraysWe use array_split() for splitting arrays, we pass it the array we want to split and the number of splits.

How do I divide NumPy array elements?

divide(arr1, arr2, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : Array element from first array is divided by elements from second element (all happens element-wise). Both arr1 and arr2 must have same shape and element in arr2 must not be zero; otherwise it will raise an error.

Can I divide arrays in Python?

Python's numpy. divide() computes the element-wise division of array elements. The elements in the first array are divided by the elements in the second array.


1 Answers

Sounds like you need to use numpy.split() which has its documentation here ... or perhaps its sibling numpy.array_split() here. They are for splitting an array into equal subsections without re-arranging the numbers like reshape does,

I haven't tested this but something like:

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

should do the trick.

like image 71
JudoWill Avatar answered Oct 01 '22 20:10

JudoWill