0

Currently I work with numpy and have to transform large data sets. Starting point are some one-dimensional arrays. These should be combined to a large 2 dimensional array. I attach a small example how it should look like.

# Returns 16 arrays with four numbers in each array (from 0 to 3)
array = [np.arange(4) for i in range(16)]

# Each of these arrays should be transformed to become a two-dimensional shape=(2,2)
array.reshape((2,2))

1. Step:
# Subsequently, the first of all of them are to be brought together:
[[0,1,0,1,0,1,0,1,0,1, ...,0,1],
[2,3,2,3,2,3,2,3,2,3, ...,2,3]]

2. Step:
# Afterwards, the arrays are to be wrapped on the basis of a length (let's say 8). The result should look like this:
[[0,1,0,1,0,1,0,1],
[2,3,2,3,2,3,2,3],
[0,1,0,1,0,1,0,1],
[2,3,2,3,2,3,2,3]]

This is only a miniature example. I'm actually working with an array with the length of 64 that is to be converted to an array with the shape=(8, 8). And at the end I want to create a 2 dimensional array with the dimensions 416x416.

Edit: So my current question is, how do I get to the first and second step in the example above?

john-mueller
  • 107
  • 2
  • 9

3 Answers3

1

You can use np.pad, with mode='wrap':

final_width = 8
final_height = 8

a = np.arange(4).reshape(2,2)
np.pad(a, ((0, final_height-a.shape[0]),(0, final_width-a.shape[1])), mode='wrap')
a

out:
array([[0, 1, 0, 1, 0, 1, 0, 1],
       [2, 3, 2, 3, 2, 3, 2, 3],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [2, 3, 2, 3, 2, 3, 2, 3],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [2, 3, 2, 3, 2, 3, 2, 3],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [2, 3, 2, 3, 2, 3, 2, 3]])
warped
  • 8,947
  • 3
  • 22
  • 49
1

Well the example you posted explains everything clearly. I don't know what the problem is. An implementation for your 64 to 8*8 transformation would be like:

import numpy as np

a = np.array([i for i in range(64)]) # defines a 64*1 1-D array
# a = array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
#        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
#        34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
#        51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63])
print(a.shape) # (64,)
a = a.reshape((8,8)) # makes "a" an 8*8 2-D array
# array([[ 0,  1,  2,  3,  4,  5,  6,  7],
#        [ 8,  9, 10, 11, 12, 13, 14, 15],
#        [16, 17, 18, 19, 20, 21, 22, 23],
#        [24, 25, 26, 27, 28, 29, 30, 31],
#        [32, 33, 34, 35, 36, 37, 38, 39],
#        [40, 41, 42, 43, 44, 45, 46, 47],
#        [48, 49, 50, 51, 52, 53, 54, 55],
#        [56, 57, 58, 59, 60, 61, 62, 63]])
print(a.shape) # (8, 8)

And the same thing goes for converting a 173056*1 array to a 416*416 one.

Maybe you are confused with the fact that you can use reshape method on a 2-D array. Of course you can!

1

Assuming that you have created array (as you described), try the following code:

chunkSize = 8      # No of columns in the result
# Reshape and bring together
array2 = np.hstack([ arr.reshape(2,2) for arr in array ])
# The final result
result = np.vstack(np.hsplit(array2, [ (n + 1) * chunkSize
    for n in range(int(array2.shape[1] / chunkSize) - 1) ]))
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41