Rebuild an array into a square Python array

I have an array of numbers whose shape is 26*43264 . I would like to remake this into an array of form 208*208 , but in pieces 26*26 .

 [[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10,11,12,13,14,15,16,17,18,19]] 

becomes something like:

 [[0, 1, 2, 3, 4], [10,11,12,13,14], [ 5, 6, 7, 8, 9], [15,16,17,18,19]] 
+5
source share
2 answers

This issue of perestroika arose earlier. But instead of searching, I will quickly demonstrate the numpy approach

create an array of samples:

 In [473]: x=np.arange(20).reshape(2,10) In [474]: x Out[474]: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]) 

Use reseape to break it into blocks of 5

 In [475]: x.reshape(2,2,5) Out[475]: array([[[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9]], [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]]) 

and use transpose to resize and essentially reorder these lines

 In [476]: x.reshape(2,2,5).transpose(1,0,2) Out[476]: array([[[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14]], [[ 5, 6, 7, 8, 9], [15, 16, 17, 18, 19]]]) 

and another form for consolidation of the 1st 2-dimensional size

 In [477]: x.reshape(2,2,5).transpose(1,0,2).reshape(4,5) Out[477]: array([[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9], [15, 16, 17, 18, 19]]) 

If x already a numpy array, these transpose and change operations look cheap (in time). If x was really nested lists, then another solution with list operations would be faster since creating a numpy array has overhead.

+3
source

A bit ugly, but here is a one-line small example that you can change to full-size:

 In [29]: from itertools import chain In [30]: np.array(list(chain(*[np.arange(20).reshape(4,5)[i::2] for i in xrange(2)]))) Out[30]: array([[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9], [15, 16, 17, 18, 19]]) 

EDIT: here's a more generalized version of the function. Uglier, but the function just takes an array and some segments that you would like to get.

 In [57]: def break_arr(arr, chunks): ....: to_take = arr.shape[1]/chunks ....: return np.array(list(chain(*[arr.take(xrange(x*to_take, x*to_take+to_take), axis=1) for x in xrange(chunks)]))) ....: In [58]: arr = np.arange(40).reshape(4,10) In [59]: break_arr(arr, 5) Out[59]: array([[ 0, 1], [10, 11], [20, 21], [30, 31], [ 2, 3], [12, 13], [22, 23], [32, 33], [ 4, 5], [14, 15], [24, 25], [34, 35], [ 6, 7], [16, 17], [26, 27], [36, 37], [ 8, 9], [18, 19], [28, 29], [38, 39]]) In [60]: break_arr(arr, 2) Out[60]: array([[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [20, 21, 22, 23, 24], [30, 31, 32, 33, 34], [ 5, 6, 7, 8, 9], [15, 16, 17, 18, 19], [25, 26, 27, 28, 29], [35, 36, 37, 38, 39]]) 
+1
source

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


All Articles