Home > Net >  Split numpy 2D Array with Unknown Length to 3D Array
Split numpy 2D Array with Unknown Length to 3D Array

Time:09-21

I have a (18, 10525) numpy array. 18 columns with 10525 rows, but the number of rows is not always the same and I must slice the array into 18 columns and groups or windows of 200 rows to feed it to AI.

For example I would like to do

data = np.ones((18, 10525))
data.reshape(-1,18,200)

But 10525 isn't divisible by 200 so I get a ValueError. I would like to get a zero padded array of shape (-1,18,200). I.e. add zeros to data until I can do .reshape(-1,18,200). Thanks in advance.

CodePudding user response:

Assuming you want to fill with zeros here is your solution

data = np.ones((18, 10525))

old_size = np.prod(data.shape)
rounded_up_size = (old_size//(18*200) 1)*18*200

reshaped_arr = np.empty(rounded_up_size)
reshaped_arr[:old_size] = data.reshape(-1)
reshaped_arr[old_size:] = 0

reshaped_arr.reshape(-1,18,200)

Notice that I avoided copying all the data. It's just a view on the old data.

  • Related